1.如题,一次过来的请求在loginfilter里执行了两次,第一次获取header为空,然后又进入了一次loginfilter,却可以获得header的一个参数
2.filter类
public class LoginFilter implements Filter {
@Autowired
UserService userService;
@Value("errorPage")
String errorPage = "/auth/error.htm";
//Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void init(FilterConfig filterConfig) throws ServletException {
ServletContext servletContext = filterConfig.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
userService = (UserService)ctx.getBean("userService");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
String path = ((HttpServletRequest)servletRequest).getServletPath();
String tempUid = ((HttpServletRequest)servletRequest).getHeader("tempUid");
//System.out.println(JSON.toJSONString(req.getHeaderNames()));
//logger.info("path=" + path + " and tempUid = " + tempUid);
if (path.indexOf("/auth/configValue") >= 0 ||
path.indexOf("/user/code") >= 0) {
// Filter 只是链式处理,请求依然转发到目的地址。
filterChain.doFilter(servletRequest, servletResponse);
} else if (StringUtils.hasLength(tempUid)){
userService.checkAuth(tempUid);
// Filter 只是链式处理,请求依然转发到目的地址。
filterChain.doFilter(servletRequest, servletResponse);
} else if (path.indexOf(errorPage) >= 0){
// Filter 只是链式处理,请求依然转发到目的地址。
filterChain.doFilter(servletRequest, servletResponse);
} else {
//logger.error("除获取前端签名和用户信息,所有接口都必须带tempUid!");
//return;
servletRequest.getRequestDispatcher(errorPage).forward(servletRequest, servletResponse);
}
}
@Override
public void destroy() {
}
}
//filter配置
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new LoginFilter());
registration.addUrlPatterns("/*");
registration.setName("loginFilter");
registration.setOrder(1);
return registration;
}
}
3.请求进来的时候第一次获取tempUid为null,第二次有值
MYYA
蝴蝶刀刀
波斯汪
HUX布斯
慕斯王
相关分类