博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot实现请求拦截(@Aspect切面类和自定义拦截器)
阅读量:5327 次
发布时间:2019-06-14

本文共 4358 字,大约阅读时间需要 14 分钟。

第一种(@Aspect切面类)

 

package com.gx.interceptor;import lombok.AllArgsConstructor;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import java.lang.reflect.Method;@Aspect@Component@AllArgsConstructor@Slf4jpublic class AuthorityHandlerAspect {    @Pointcut("execution(* com.gx.controller.*.*(..))")    public void executeService() {    }    @Around("executeService()")    public Object authorityHandler(ProceedingJoinPoint pjp) throws Throwable {        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();        Method method = methodSignature.getMethod();             /判断方法上是否有此注解     if (method.isAnnotationPresent(WorkSheetAnnotation.class)) {}     //判断类上是否有次注解     if(pjp.getTarget().getClass().isAnnotationPresent(WorkSheetAnnotation.class)){}     //获取方法上的注解值       method.getAnnotation(NoAuth.class)     log.info("method{}",method); System.out.println("!!!!!!!!!!!!!!!!!已被拦截");   return pjp.proceed();    } }

 

@Target({METHOD, FIELD})@Retention(RUNTIME)@Documentedpublic @interface NoAuth{    String message() default "";}

 

 

 

/** * @Description 工单类注解 * @Author GX * @Date 2019/5/7 10:02 * @Version V1.0 **/@Target(TYPE)@Retention(RUNTIME)@Documentedpublic @interface WorkSheetAnnotation {}

 

//获取当前请求的URIHttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();String uri = request.getRequestURI()

 

 

 

第二种(自定义拦截器类)

第一步 package com.chitic.bank.web.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.http.HttpHeaders;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;public class CrossDomainInterceptor extends HandlerInterceptorAdapter{      /**   *在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
  */    @Override    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)            throws Exception {    }    @Override    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)            throws Exception {    }   /**   *请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
  */    @Override    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,            ModelAndView modelAndView) throws Exception {    }    /**
    *在请求处理之前进行调用(Controller方法调用之前)
   **/    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)            throws Exception {                    if (request.getHeader(HttpHeaders.ORIGIN) != null) {                                response.setHeader("Access-Control-Allow-Origin", "*");                response.setHeader("Access-Control-Allow-Methods", "POST, GET");                response.setHeader("Access-Control-Allow-Headers", "token,Content-Type");                response.setHeader("Access-Control-Expose-Headers", "token");            }           return true;    }        } 第二步:注册自定义拦截器
package com.chitic.bank.web.config; import com.chitic.bank.web.interceptor.CrossDomainInterceptor; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class CustomWebConfig extends WebMvcConfigurerAdapter {
@Order(1) //作用,值越小,越优先执行 @Bean @ConditionalOnMissingBean(CrossDomainInterceptor.class) public CrossDomainInterceptor crossDomainInterceptor() {
return new CrossDomainInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry); registry.addInterceptor(crossDomainInterceptor()); } }
 

 

转载于:https://www.cnblogs.com/gaomanito/p/10683906.html

你可能感兴趣的文章
MyBatis
查看>>
Sklearn实现逻辑回归
查看>>
SuperMap iServerJava 6R扩展领域开发及压力测试---判断点在那个面内(1)
查看>>
Week03-面向对象入门
查看>>
MongoDB 状态监控、备份复制及自动分片
查看>>
《大道至简》阅读笔记一
查看>>
一个控制台程序,模拟机器人对话
查看>>
web.xml 中加载顺序
查看>>
mysql学习之安装(一)
查看>>
[数据库]关于主键与外键
查看>>
pycharm激活地址
查看>>
hdu 1207 四柱汉诺塔
查看>>
Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(上篇——纯前端多页面)
查看>>
5年内的暴风骤雨:12诱因统领软件行业大革命【转载】
查看>>
display:none与visible:hidden的区别
查看>>
我的PHP学习之路
查看>>
【题解】luogu p2340 奶牛会展
查看>>
2019牛客暑期多校训练营(第五场)
查看>>
wnmp安装配置的坑
查看>>
【Django】执行manage.py test报数据库错误
查看>>