zoukankan      html  css  js  c++  java
  • 基于springBoot使用aop与mvc拦截器实现登陆校验

    1.编写切面类

    @Component
    @Aspect
    @Slf4j
    public class SellerAuthorizeAspect {
    
        @Autowired
        StringRedisTemplate stringRedisTemplate;
    
        @Pointcut(value = "execution(* com.yzy.sell.Controller.Seller*.*(..))"+
        "&&!execution(* com.yzy.sell.Controller.SellerUserController.*(..))")
        public void verify(){}//配置切面,上面设置了切面的范围
    
        @Before(value = "verify()")//配置前置通知,beforeAdvice
        public void doVerify(){
            ServletRequestAttributes requestAttributes =
                    (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            Cookie cookie = CookieUtil.get(request, CookieConstant.name);
            //在cookie查询是否含有token的cookie
            if(cookie==null){
                log.warn("登陆校验失败,查询不到cookie");
                throw new SellerAuthorizeException(); //抛出异常,让拦截器捕获,实现登陆拦截
            }
            String tokenValue = stringRedisTemplate.opsForValue().get
                    (String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
            if(StringUtils.isEmpty(tokenValue)){
                log.warn("登陆校验失败,在redis查询不到相应的token");
                throw new SellerAuthorizeException();
            }
        }
    }

    2.编写handle拦截器捕获登陆校验产生的异常

    @ControllerAdvice//使用 @ControllerAdvice 实现全局异常处理
    public class SellExceptionHandler {
        @Autowired
        private ProjectUrlConfig projectUrlConfig;
    
        //拦截登陆的异常
        @ExceptionHandler(SellerAuthorizeException.class)
        public String handlerSellException(){
            return "redirect:".concat(projectUrlConfig.getSell())
                    .concat("/sell/wechat/qrAuthorize");//跳转会登陆页面
        }
    }

    补充:@controlleradvice注解作用:https://www.cnblogs.com/lenve/p/10748453.html

    @Component
    @Aspect
    @Slf4j
    public class SellerAuthorizeAspect {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Pointcut(value = "execution(* com.yzy.sell.Controller.Seller*.*(..))"+
    "&&!execution(* com.yzy.sell.Controller.SellerUserController.*(..))")
    public void verify(){}//配置切面,上面设置了切面的范围

    @Before(value = "verify()")//配置前置通知,beforeAdvice
    public void doVerify(){
    ServletRequestAttributes requestAttributes =
    (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = requestAttributes.getRequest();
    Cookie cookie = CookieUtil.get(request, CookieConstant.name);
    //cookie查询是否含有tokencookie
    if(cookie==null){
    log.warn("登陆校验失败,查询不到cookie");
    throw new SellerAuthorizeException();
    }
    String tokenValue = stringRedisTemplate.opsForValue().get
    (String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
    if(StringUtils.isEmpty(tokenValue)){
    log.warn("登陆校验失败,redis查询不到相应的token");
    throw new SellerAuthorizeException();
    }
    }
    }
  • 相关阅读:
    Oracle的序列、视图、索引和表空间
    MySQL存储过程
    MySQL触发器
    MySQL索引和视图
    完整性约束
    Mybatis的核心对象及运行流程
    Mybatis中配置连接池
    IDEA中创建Maven工程整合Mybatis
    Idea中创建JavaWeb工程
    实现整数集合的并、交、差运算
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13235306.html
Copyright © 2011-2022 走看看