定义自定义注解
1@Retention(RetentionPolicy.RUNTIME)
2@Target(ElementType.PARAMETER)
3public @interface Logined {
4}
注解处理器
1@Component
2@Slf4j
3public class LoginedArgumentResolver implements HandlerMethodArgumentResolver {
4 @Override
5 public boolean supportsParameter(MethodParameter parameter) {
6 return CurrentRequest.class.isAssignableFrom(parameter.getParameterType());
7 }
8
9 @Override
10 public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
11 HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
12 CurrentRequest currentRequest = (CurrentRequest) request.getAttribute(CurrentRequest.requestAttributeName());
13 return currentRequest;
14 }
15}
使用
1 @RequestMapping(value = {"..."})
2 public Object create(HttpServletRequest request,
3 @Logined CurrentRequest user) {
4 ......
5 }
评论