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();
    }
    }
    }
  • 相关阅读:
    跑Linux内存占用率的shell脚本
    Mysql数据导入导出功能(设置及使用)
    Python刷CSDN阅读数(仅供娱乐)
    SSH、SCP命令及使用说明
    2020.6.7 时间记录
    3年经验,中级Java开发招聘信息
    中级Java程序员要求
    提升自身内部核心竞争力要素
    三级联动页面
    大公司和小公司程序员
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13235306.html
Copyright © 2011-2022 走看看