Java|java通过注解和拦截器实现token鉴权校验( 二 )



@Override
public void afterCompletion(HttpServletRequest httpServletRequest HttpServletResponse httpServletResponse Object o Exception e) throws Exception {


第四步:重写HandlerMethodArgumentResolver
package com.abby.interceptor;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
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 com.abby.model.vo.LoginUser;
public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver{
private Logger logger = LoggerFactory.getLogger(CurrentUserMethodArgumentResolver.class);
@Override
public boolean supportsParameter(MethodParameter parameter) {
//判断是否支持使用@CurrentUser注解的参数; 如果该参数注解有@CurrentUser且参数类型是User
return parameter.getParameterAnnotation(CurrentUser.class) != null && parameter.getParameterType() == LoginUser.class ;

@Override
public Object resolveArgument(MethodParameter parameter ModelAndViewContainer mavContainer
NativeWebRequest webRequest WebDataBinderFactory binderFactory) throws Exception
{
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();//注入参数值
logger.info(\"get attribute currentUser ----\" + request.getAttribute(\"currentUser\"));
return request.getAttribute(\"currentUser\");


第五步:拦截器注册
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.abby.interceptor.AuthenticationInterceptor;
import com.abby.interceptor.CurrentUserMethodArgumentResolver;
/***
* 新建Token拦截器
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authenticationInterceptor())
.addPathPatterns(\"/**\"); // 拦截所有请求 , 通过判断是否有 @LoginRequired 注解 决定是否需要登录

@Bean
public HandlerInterceptor authenticationInterceptor() {
return new AuthenticationInterceptor();// 自己写的拦截器

@Bean
public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver(){
return new CurrentUserMethodArgumentResolver();

@Override
public void addArgumentResolvers(List argumentResolvers) {
argumentResolvers.add(currentUserMethodArgumentResolver());

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping(\"/**\")
.allowedOrigins(\"*\")
.allowedMethods(\"GET\" \"HEAD\" \"POST\" \"PUT\" \"DELETE\" \"OPTIONS\")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders(\"*\");


第六步:contreller中使用
@UserLoginToken
@GetMapping(\"/logout\")
@ApiOperation(value=https://mparticle.uc.cn/"用户退出\")
@ApiImplicitParam(paramType=\"header\" name = \"token\" value = https://mparticle.uc.cn/"用户登录token\" required = true dataType = \"String\")
public ResultVO logout(@CurrentUser LoginUser loginUser) {