Spring Boot项目——自定义HandlerMethodArgumentResolver获取当前登陆用户
思路
- 自定义注解:指定参数
- 创建登陆用户类:保存登陆用户信息
- 自定义登陆用户参数解析器:获取登陆token,解析为登陆用户对象信息
- 配置MVC:新增登陆用户参数解析器
代码
- 自定义LoginUser注解
package com.canaan.manager.token; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface LoginUser { }
- 创建CurrentUser类
package com.canaan.manager.token; import java.util.Date; /* * @Description jwtToken 用户 * @Author jingguoliang * @Date 2022/8/3 11:27 * @Ver 1 **/ public class CurrentUser { //id private String userId; //用户名 private String userName; //密码 private String password; //过期时间 private Date expiration; public CurrentUser() { } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getExpiration() { return expiration; } public void setExpiration(Date expiration) { this.expiration = expiration; } @Override public String toString() { return "CurrentUser{" + "userId='" + userId + '\'' + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", expiration=" + expiration + '}'; } }
- 定义CurrentUser参数解析器
package com.canaan.manager.token; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.springframework.core.MethodParameter; import org.springframework.stereotype.Component; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; /* * @Description @Current接口参数解析器 * @Author jingguoliang * @Date 2022/8/3 11:31 * @Ver 1 **/ @Component public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver { public CurrentUserMethodArgumentResolver() { } public boolean supportsParameter(MethodParameter parameter) { return parameter.getParameterType().isAssignableFrom(CurrentUser.class) && parameter.hasParameterAnnotation(LoginUser.class); } /** * 解析参数 * * @param parameter * @param mavContainer * @param webRequest * @param binderFactory * @return */ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { return getCurrentUser((HttpServletRequest) webRequest.getNativeRequest(HttpServletRequest.class)); } /** * 获得当前登陆用户 * * @param webRequest * @return */ public CurrentUser getCurrentUser(HttpServletRequest webRequest) { String token = webRequest.getHeader("token"); return StringUtils.isBlank(token) ? new CurrentUser() : TokenUtil.getCurrentUserFromToken(token); } }
- 配置MVC
package com.canaan.manager.token; import org.springframework.context.annotation.Configuration; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import javax.annotation.Resource; import java.util.List; /* * @Description MVC配置 * @Author jingguoliang * @Date 2022/8/3 14:39 * @Ver 1 **/ @Configuration public class WebConfigConfiguration implements WebMvcConfigurer { @Resource private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver; /** * 添加参数解析器 * * @param resolvers */ @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { resolvers.add(currentUserMethodArgumentResolver); } }
代码下载
- 详细代码可下载:文件token.zip 。