主要校验是前端时候在请求的时候带来的Header 认证码是否正确 来判断请求是否有效
1/自定义认证拦截注解
/**
* 定义全局
* @auth LZQ
* @date 2018-08-13 上午 10:12
*/
public class ApplicationConst {
public static final String AUTH_TOKEN = "auth_token";
}
2/定义配置参数
/**
* 定义全局
* @auth LZQ
* @date 2018-08-13 上午 10:12
*/
public class ApplicationConst {
public static final String AUTH_TOKEN = "auth_token";
}
3/创建拦截切面进行注解捕捉
/**
* 认证拦截
* @auth LZQ
* 请求认证切面,验证自定义请求header的authtoken是否合法
* @date 2018-08-13 上午 10:00
*/
@Aspect
@Component
public class AuthorizedAspect {
@Resource
private AuthTokenService authTokenService;
@Resource
private ExceptionManager exceptionManager;
/**
* 某个方法执行前进行请求合法性认证 注入Authorized注解 (先)
*/
@Before("@annotation(authorized)")
public void doBefore(JoinPoint joinPoint, Authorized authorized) throws Exception {
Class type = joinPoint.getSignature().getDeclaringType();
Annotation[] annotations = type.getAnnotationsByType(Authorized.class);
if (annotations != null && annotations.length > 0) {
return;
}
//获取当前http请求
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String token = request.getHeader(ApplicationConst.AUTH_TOKEN);
/*此处进行TOKEN 码的认证*/
Boolean checkResult = authTokenService.checkTonkenValidate(token);
if (!checkResult) {
throw exceptionManager.create("AUTH_000");
}
}
}
以上是进行方法的进行AOP拦截,也可以进行类级别的拦截
参考于:https://www.cnblogs.com/jeffwongishandsome/archive/2018/06/08/9090374.html