@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) {
- Java|虚拟现实+眼动追踪精选实战分享:认知“新”健康
- 小米科技|小米12s通过认证,7月份发布,小米12跌价800元
- Java|国家出手整治资本!人民日报三揭马云真面目,马云他到底干了啥?
- Java|当下屏幕最好的五款手机,和“周冬雨”说再见,数码博主都推荐
- Java|美媒:阿里、华为、中科院已经“突围”,芯片市场风向变了
- Java|美团这次硬气了!
- Java|百度的概念车,遭人嘲笑
- Java|花呗提前还款的“坏处”正式被确认,你有注意到吗?
- javascript|Web前端:什么是Node js? 什么是React? 有什么区别
- 电热水器|扁桶电热水器成时尚,该怎么选?通过这四款来找找异同