• 12.18 aop身份验证


    对所有卖家页面进行身份验证,采用aop编程

    步骤:1.获得request 

    2.查询cookie

    3.查询redis

    4.查询不通过时,采用抛出异常,捕捉异常,再异常里加入跳转到登陆页面的方法

    准备工作:

    创建异常方法,可以不添加内容

    public class SellerAuthorizeException extends RuntimeException {
    }
    

      aop编程

    @Aspect
    @Component
    @Slf4j
    public class SellerAuthorizeAspect {
    
        @Autowired
        private StringRedisTemplate redisTemplate;
    
        @Pointcut("execution(public * com.imooc.controller.Seller*.*(..))" +
        "&& !execution(public * com.imooc.controller.SellerUserController.*(..))")
        public void verify() {}//定义切面范围包含与不包含的
    
        @Before("verify()")
        public void doVerify() {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
    
            //查询cookie
            Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
            if (cookie == null) {
                log.warn("【登录校验】Cookie中查不到token");
                throw new SellerAuthorizeException();
            }
    
            //去redis里查询
            String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));
            if (StringUtils.isEmpty(tokenValue)) {
                log.warn("【登录校验】Redis中查不到token");
                throw new SellerAuthorizeException();
            }
        }
    }
    

      登陆异常异常捕捉并加入跳转方法

    @ControllerAdvice
    public class SellExceptionHandler {
    
        @Autowired
        private ProjectUrlConfig projectUrlConfig;
    
        //拦截登录异常
        //http://sell.natapp4.cc/sell/wechat/qrAuthorize?returnUrl=http://sell.natapp4.cc/sell/seller/login
        @ExceptionHandler(value = SellerAuthorizeException.class)
        public ModelAndView handlerAuthorizeException() {
            return new ModelAndView("redirect:"
            .concat(projectUrlConfig.getWechatOpenAuthorize())
            .concat("/sell/wechat/qrAuthorize")
            .concat("?returnUrl=")
            .concat(projectUrlConfig.getSell())
            .concat("/sell/seller/login"));
        }
    }
    

      

  • 相关阅读:
    46、Spark SQL工作原理剖析以及性能优化
    45、sparkSQL UDF&UDAF
    44、开窗函数及案例
    43、内置函数及每日uv、销售额统计案例
    42、JDBC数据源案例
    41、Hive数据源复杂综合案例
    40、JSON数据源综合案例实战
    39、Parquet数据源之自动分区推断&合并元数据
    Java 的String类
    Java学习之旅基础知识篇:面向对象之封装、继承及多态
  • 原文地址:https://www.cnblogs.com/tanghao666/p/8060162.html
Copyright © 2020-2023  润新知