One, add a comment
/** * The mark annotation used to bind the currently logged-in user object in the interface parameter * @author yanghui * @date 2020/12/17 **/ @Retention(RetentionPolicy.RUNTIME) @Target(value = {ElementType.PARAMETER}) @Documented public @interface LoginUser { }
2. Create a parser for loginUser annotation
/** * Custom parameter parser * Role: Get the logged-in user object and bind it to the parameters of the method * @author yanghui * @date 2020/12/17 **/ public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter methodParameter) { return methodParameter.hasParameterAnnotation(LoginUser.class); } @Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception { if (nativeWebRequest.getNativeRequest() instanceof HttpServletRequest) { HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); return request.getAttribute("LOGIN_USER"); } return null; } }
Three, custom interceptor
public class WebInterceptor implements HandlerInterceptor { @Autowired private RedisUtil redisUtil; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //Take out your logged-in users and put them in the request by going to redis. In the second step, get the user information of our request and put them in the comments. Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); SimpleUser simpleUser = new SimpleUser(); if (authentication != null && authentication.getDetails() != null) { if (authentication.getDetails() instanceof OAuth2AuthenticationDetails) { OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails(); Object obj = redisUtil.get(details.getTokenValue()); if (obj != null) { simpleUser = JSON.parseObject(obj.toString(), SimpleUser.class); }else { String userName=(String)authentication.getPrincipal(); simpleUser.setUsername(userName); } } else if (authentication instanceof UsernamePasswordAuthenticationToken) { simpleUser = (SimpleUser) authentication.getPrincipal(); } else if (authentication.getPrincipal() != null) { simpleUser.setUsername(authentication.getPrincipal().toString()); } } /** Pass in the login object, use it in the custom parameter parser*/ request.setAttribute("LOGIN_USER", simpleUser); return true; }
Fourth, create a configuration class, add our custom interceptor and parameter parser
@Configuration public class WebInterceptorConfig implements WebMvcConfigurer { /** * Custom parameter parser * Role: Get the logged-in user object and bind it to the parameters of the method * @author yanghui * @date 2019/12/17 * @return LoginUserHandlerMethodArgumentResolver **/ @Bean public LoginUserHandlerMethodArgumentResolver getLoginUserHandlerMethodArgumentResolver() { return new LoginUserHandlerMethodArgumentResolver(); } /** * Login verification interceptor * * @return */ @Bean public WebInterceptor loginRequiredInterceptor() { return new WebInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { //Add a request that needs to verify user operation permissions registry.addInterceptor(loginRequiredInterceptor()) .addPathPatterns("/**"); } /** * Add @LoginUser parameter parser * @author yanghui * @date 2020/12/17 * @return void **/ @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(getLoginUserHandlerMethodArgumentResolver()); } }
5. Add the @LoginUser SimpleUser user parameter to our Controller interface parameter to use it.