I. Overview
Second, the use of Spring
web.xml
The proxy class of the filter is added in the file, targetBeanName
and the value is the value of the filter id
. That is @Component("authFilter")
in authFilter
.
<!-- 统一认证过滤器的代理 -->
<filter>
<filter-name>DelegatingFilterProxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>authFilter</param-value>
</init-param>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>DelegatingFilterProxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
@Component()
If there is no parameter, the default value is the first letter of the class name in lowercase.
/**
* 统一认证过滤器
*/
@Component("authFilter")
public class AuthFilter implements Filter {
protected static final Logger logger = LoggerFactory.getLogger(AuthFilter.class);
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse)response;
HttpSession session = httpRequest.getSession();
}
}
3. Use of SpringBoot
1. Method 1
@WebFilter(filterName = "dataFilter", urlPatterns = "/api/*")
@Order(1)
public class DataFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
@ServletComponentScan
Add annotations to the startup class
//@ServletComponentScan({"com.scy.frame", "com.scy.app"})
@ServletComponentScan
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(VideoScreenApplication.class, args);
}
}
@ServletComponentScan works
on SpringBootApplication. After using the @ServletComponentScan annotation on SpringBootApplication,
the Servlet can be automatically registered directly through the @WebServlet annotation.
The Filter can be automatically registered directly through the @WebFilter annotation.
The Listener can be automatically registered directly through the @WebListener annotation.
2. Method 2
Do not use @WebFilter
the way to declare the filter, use FilterRegistrationBean
the filter to inject in the configuration class
@Configuration
public class FilterConfiguration {
@Bean
public FilterRegistrationBean dataFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new DataFilter());
bean.setUrlPatterns(Arrays.asList("/api/*"));
bean.setOrder(6);//优先级,越低越优先
return bean;
}
}
3. Method 3 (problems)
Do not add annotations to the startup class anymore @ServletComponentScan
, use @Component
or @Configuration
cause urlPatterns
the specified interception path to fail to take effect, and all interfaces will be intercepted.
@Component
@WebFilter(filterName = "dataFilter", urlPatterns = "/api/*")
@Order(1)
public class DataFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, response);
}
}