SpringBoot 自定义拦截器 HandlerInterceptor 方法没有生效
·
在springBoot 中我自定义了一个拦截器,通过实现:HandlerInterceptor 接口
package com.abs.system.filter;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import com.abs.system.util.AbsSessionHelper;
import com.abs.system.util.BuildJsonOfObject;
import com.abs.system.util.MSG;
import com.abs.system.util.StrUtil;
/**
* 拦截器
*
* @author wdg
*
*/
public class AbsHandlerInterceptor implements HandlerInterceptor {
private Logger logger = LoggerFactory.getLogger(AbsHandlerInterceptor.class);
long start = System.currentTimeMillis();
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod h = (HandlerMethod) handler;
logger.info("当前请求接口:{}",h.getMethod().getName());
if (h.hasMethodAnnotation(NoNeedLogin.class)) {
return true;
} else {
String token = httpServletRequest.getHeader("Authorization");
if (StrUtil.isNotBlank(AbsSessionHelper.getCurrentUserGuid(token))) {
return true;
} else {
httpServletResponse.setContentType("application/json");
PrintWriter writer=httpServletResponse.getWriter();
writer.write(BuildJsonOfObject.getJsonString(MSG.fail, MSG.NOLOGIN).toJSONString());
return false;
}
}
} else if (handler instanceof ResourceHttpRequestHandler) {
return true;
} else {
return true;
}
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object handler, ModelAndView modelAndView) throws Exception {
System.out.println(start);
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object o, Exception e) throws Exception {
System.out.println("***********");
}
}
什么原因,原来我为了解决跨域和资源的访问,实现了:WebMvcConfigurationSupport
package com.abs.system.filter;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class AbsWebMvcConfigurerAdapter extends WebMvcConfigurationSupport {
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "canvas/index.html");
registry.addRedirectViewController("/index", "canvas/index.html");
registry.addRedirectViewController("/index.html", "canvas/index.html");
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/canvas/**").addResourceLocations("classpath:/templates/");
registry.addResourceHandler("/www/**").addResourceLocations("classpath:/www/");
registry.addResourceHandler("/db/**").addResourceLocations("classpath:/db/");
registry.addResourceHandler("/font/**").addResourceLocations("classpath:/font/");
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
super.addArgumentResolvers(argumentResolvers);
argumentResolvers.add(new AbsHandlerMethodArgumentResolver());
}
@Override
public void addCorsMappings(CorsRegistry registry) {
// 添加映射路径
registry.addMapping("/**")
// 是否发送Cookie
.allowCredentials(true)
// 设置放行哪些原始域 SpringBoot2.4.4下低版本使用.allowedOrigins("*")
.allowedOrigins("*")
// 放行哪些请求方式
.allowedMethods(new String[] { "GET", "POST", "PUT", "DELETE" })
// .allowedMethods("*") //或者放行全部
// 放行哪些原始请求头部信息
.allowedHeaders("*");
}
}
为了能走拦截器,我需要把我的拦截器加入到SpringBoot 的拦截器列表中
需要在AbsWebMvcConfigurerAdapter 重新 :addInterceptors 方法
如下:
package com.abs.system.filter;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class AbsWebMvcConfigurerAdapter extends WebMvcConfigurationSupport {
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "canvas/index.html");
registry.addRedirectViewController("/index", "canvas/index.html");
registry.addRedirectViewController("/index.html", "canvas/index.html");
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/canvas/**").addResourceLocations("classpath:/templates/");
registry.addResourceHandler("/www/**").addResourceLocations("classpath:/www/");
registry.addResourceHandler("/db/**").addResourceLocations("classpath:/db/");
registry.addResourceHandler("/font/**").addResourceLocations("classpath:/font/");
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
super.addArgumentResolvers(argumentResolvers);
argumentResolvers.add(new AbsHandlerMethodArgumentResolver());
}
@Override
public void addCorsMappings(CorsRegistry registry) {
// 添加映射路径
registry.addMapping("/**")
// 是否发送Cookie
.allowCredentials(true)
// 设置放行哪些原始域 SpringBoot2.4.4下低版本使用.allowedOrigins("*")
.allowedOrigins("*")
// 放行哪些请求方式
.allowedMethods(new String[] { "GET", "POST", "PUT", "DELETE" })
// .allowedMethods("*") //或者放行全部
// 放行哪些原始请求头部信息
.allowedHeaders("*");
}
@Override
public void addInterceptors(InterceptorRegistry interceptor) {
interceptor.addInterceptor(new AbsHandlerInterceptor()).addPathPatterns("/**");
}
}
这样再次走接口的时候,就会走拦截器了
希望对你有多帮助
更多推荐
已为社区贡献4条内容
所有评论(0)