新建注解
/**
* 在Controller的方法上使用此注解,该方法在映射时会对用户进行身份验证,验证失败返回401错误
* 也可以直接在Controller上使用,代表该Controller的所有方法均需要身份验证
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Authorization {
String clientId() default "";
}
在注解上使用参数:
@Authorization(clientId = ClientIdConst.BC21)
获取主街上的参数:
/**
* 处理用户权限
*
* @param request
* @param response
* @return
* @throws IOException
*/
@Nullable
private Boolean handleUserPermission(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
Authorization authorization =
//获取请求方法的bean
handlerMethod.getBeanType().getAnnotation(Authorization.class);
AuthDto authDto = new AuthDto();
boolean commonAuth = commonAuth(request, response,authDto);
//基础鉴权不通过
if (!commonAuth) {
return commonAuth;
}
Boolean x = null;
switch (authorization.clientId()) {
case ClientIdConst.BC21:
x = authorizeBC21(request, response, handlerMethod, authDto);
if (x != null) return x;
break;
case ClientIdConst.SP258:
x = authorizeSP258(request, response, handlerMethod,authDto);
if (x != null) return x;
break;
default:
break;
}
return false;
}