码迷,mamicode.com
首页 > 编程语言 > 详细

基于springBoot使用aop与mvc拦截器实现登陆校验

时间:2020-07-04 16:55:54      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:style   编写   cep   const   ken   wechat   登陆   exe   redirect   

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();
}
}
}

基于springBoot使用aop与mvc拦截器实现登陆校验

标签:style   编写   cep   const   ken   wechat   登陆   exe   redirect   

原文地址:https://www.cnblogs.com/shouyaya/p/13235306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!