一、首先创建一个LoginUser的自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.Documented;
/**
* @Author: leemeea
* @Date: 2021/7/9 9:52
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoginUser {
}
二、然后创建一个Resolver,代码如下
import com.common.enums.BaseErrorCode;
import com.common.utils.BearerUtil;
import com.common.utils.ServiceAssert;
import com.console.pojo.dto.user.LoginUserDTO;
import org.springframework.core.MethodParameter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
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;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* @Author: leemeea
* @Date: 2021/7/9 10:04
*/
@Service("loginUserResolver")
public class LoginUserResolver implements HandlerMethodArgumentResolver {
@Resource
public RedisTemplate redisTemplate;
private static final String bearerKeyPrefix = "bearer-ebi-user-";
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return methodParameter.hasParameterAnnotation(LoginUser.class)
&& methodParameter.getParameterType().isAssignableFrom(LoginUserDTO.class);
}
@Override
public Object resolveArgument(MethodParameter methodParameter,
ModelAndViewContainer modelAndViewContainer,
NativeWebRequest nativeWebRequest,
WebDataBinderFactory webDataBinderFactory) {
// 获取request
HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
// request为空,抛出异常
ServiceAssert.notNull(request, BaseErrorCode.NOT_AUTHORIZATION);
// 从request获取鉴权信息
String bearer = BearerUtil.getBearer(request);
// 从redis获取鉴权信息对应的登录用户dto
LoginUserDTO loginUserDTO = (LoginUserDTO) redisTemplate.opsForValue().get(bearerKeyPrefix + bearer);
// 如果登录用户dto为空,抛出异常
ServiceAssert.notNull(loginUserDTO, BaseErrorCode.NOT_AUTHORIZATION);
// 返回
return loginUserDTO;
}
}
三、将创建的Resolver注入Spring的WebMvcConfigurer中,由WebMvcConfigurer接管。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: leemeea
* @Date: 2021/7/9 11:12
*/
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Resource
private LoginUserResolver loginUserResolver;
public void MyWebMvcConfigurer() {
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(loginUserResolver);
}
}
四、最后调用的位置,在Controller层传入参数添加LoginUserDTO userDTO,并在其前面添加@LoginUser的注解
/**
* 查询列表
*/
@Log(name = "/query/list", description = "查询列表")
@PostMapping(value = "/list")
public BaseResponse list(@LoginUser LoginUserDTO userDTO, @RequestBody ArticleApproveQueryRequest req) {
req.setLoginUserId(userDTO.getUserId());
req.setLoginOrganizationId(1L);
PageResponse<articleApproveQueryResponse> response = articleApproveService.list(req);
return Results.success(response);
}
完结–