微信点餐系统

权限校验

在每次页面请求之前都要校验用户是否已经登陆,可以HttpServletRequest中取Cookie,从Cookie中取“token”的值,再从redis去判断是否有该值对于的key,并验证用户唯一标识。-------通过AOP完成。


1、先实现一个切面:
@Aspect
@Slf4j
@Component
public class SellerAuthorizeAspect {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Pointcut("execution(public * com.njupt.sell.controller.Seller*.*(..))"+
            "&&!execution(public * com.njupt.sell.controller.SellerInfoController.*(..))")//排除用户注册的Controller
    public void vertify(){}

    @Before("vertify()")
    public void doVerify() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //查询Cookie
        Cookie cookie = CookieUtil.get(request, CookieConstrant.TOKEN);
        if (cookie==null) {
            log.error("【登陆校验】cookie中查询不到token");
            throw new SellAuthorizeException();
        }
        //查询redis
        String tokenValue = stringRedisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
        //如果redis中没有该tokenValue值
        if (StringUtils.isEmpty(tokenValue)){
            log.warn("【登陆校验】Cookie中查不到token");
            throw new SellAuthorizeException();
        }
    }
}


2、其中抛出的一个异常为:
public class SellAuthorizeException extends RuntimeException {
}

3、最后对所有的异常统一做页面跳转处理,表示登陆失败:

@ControllerAdvice
public class SellerExceptionHandler {

    @Autowired
    private ProjectConfig projectConfig;

    @ExceptionHandler(value = SellAuthorizeException.class)
    public ModelAndView handlerAuthorizeException() {
        return new ModelAndView("jsp/login");
    }
}


@ControllerAdvice是Spring 3.2新增的注解,主要是用来Controller的一些公共的需求的低侵入性增强提供辅助,在@ControllerAdvice注解内部我们
可以使用@ExceptionHandler、@InitBinder、             @ModelAttribute共3个注解,使用方式如下:
@ControllerAdvice    
public class ControllerAdviceTest {    

    @ModelAttribute    
    public User newUser() {    
        System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前把返回值放入Model");    
        return new User();    
    }    

    @InitBinder    
    public void initBinder(WebDataBinder binder) {    
        System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器");    
    }    

    @ExceptionHandler(UnauthenticatedException.class)    
    @ResponseStatus(HttpStatus.UNAUTHORIZED)    
    public String processUnauthenticatedException(NativeWebRequest request, UnauthenticatedException e) {    
        System.out.println("===========应用到所有@RequestMapping注解的方法,在其抛出UnauthenticatedException异常时执行");    
        return "viewName"; //返回一个逻辑视图名    
    }    
}   



@ExceptionHandler   自定义的错误处理,这个注解则表示Controller中任何一个方法发生异常,则会被注解了@ExceptionHandler的方法拦截到。对应
的异常类执行对应的方法,如果都没有匹配到异常类,则采用近亲匹配的方式。
 以上代码可以看出@ModelAttribute、@initBinder和@ExceptionHandler对所有使用@RequestMapping的方法起作用,但@ModelAttribute和@initBinder在设置全局数据时比较有用,使用较少,                               
 @ExceptionHandler异常处理器,当注解的方法发生定义的异常时产生作用,使用较多。例如遇    到RuntimeException时做JSON的异常处理。 
文章目录