[size=x-large][color=black][b]前言[/b][/color][/size]
传统的javaEE增加Filter是在web.xml中配置,如以下代码:
然而spring-boot中很明显不能这样实现,那怎么办呢?看完下面的教程,答案自然知道了
[size=x-large][color=black][b]老方法(新方法请直接下拉)[/b][/color][/size]
[size=x-large][color=black][b]1.创建自定义Filter[/b][/color][/size]
[size=x-large][color=black][b]2.在ApplicationConfiguration.java中增加一个@bean[/b][/color][/size]
[size=x-large][color=black][b]3.启动项目[/b][/color][/size]
你会看到控制台打印如下代码:
[img]http://upload-images.jianshu.io/upload_images/2432809-0cb292a517120a80.png?imageMogr2/auto-orient/strip%7CimageView2/2[/img]
[size=x-large][color=black][b]4.访问项目[/b][/color][/size]
最后我们访问以下http://127.0.0.1:8080/test
如果你看到控制台打印出:TestFilter
[img]http://upload-images.jianshu.io/upload_images/2432809-474c8a5519da268f.png?imageMogr2/auto-orient/strip%7CimageView2/2[/img]
恭喜你,配置成功!
参考项目:[url]https://github.com/bigbeef/cppba-spring-boot[/url]
github地址:[url]https://github.com/bigbeef[/url]
个人博客:[url]http://www.cppba.com[/url]
[size=x-large][color=black][b]2017-04-20 最新spring-boot增加Filter方法[/b][/color][/size]
首先定义一个Filter
比较核心的代码是自定义类上面加上@WebFilter,其中@Order注解表示执行过滤顺序,值越小,越先执行
我们在spring-boot的入口处加上如下注解@ServletComponentScan:
这种方法效果和上面版本一样,但是用起来更加方便!
[url]http://www.jianshu.com/p/05c8be17c80a[/url]
[size=large][color=red][b]SpringBoot初始教程之Servlet、Filter、Listener配置(七)[/b][/color][/size]
[url]http://blog.csdn.net/king_is_everyone/article/details/53116744[/url]
[size=large][color=green][b] Spring Boot 拦截器[/b][/color][/size]
[url]http://blog.csdn.net/catoop/article/details/50501696[/url]
[size=large][color=green][b] Spring Boot实战之Filter实现简单的Http Basic认证[/b][/color][/size]
[url]http://blog.csdn.net/sun_t89/article/details/51916834[/url]
传统的javaEE增加Filter是在web.xml中配置,如以下代码:
<filter>
<filter-name>TestFilter</filter-name>
<filter-class>com.cppba.filter.TestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-pattern>/*</url-pattern>
<init-param>
<param-name>paramName</param-name>
<param-value>paramValue</param-value>
</init-param>
</filter-mapping>
然而spring-boot中很明显不能这样实现,那怎么办呢?看完下面的教程,答案自然知道了
[size=x-large][color=black][b]老方法(新方法请直接下拉)[/b][/color][/size]
[size=x-large][color=black][b]1.创建自定义Filter[/b][/color][/size]
package com.cppba.filter;
import javax.servlet.*;
import java.io.IOException;
public class TestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
System.out.println("TestFilter");
}
@Override
public void destroy() {
}
}
[size=x-large][color=black][b]2.在ApplicationConfiguration.java中增加一个@bean[/b][/color][/size]
@Bean
public FilterRegistrationBean testFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new TestFilter());
registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("testFilter");
registration.setOrder(1);
return registration;
}
[size=x-large][color=black][b]3.启动项目[/b][/color][/size]
你会看到控制台打印如下代码:
[img]http://upload-images.jianshu.io/upload_images/2432809-0cb292a517120a80.png?imageMogr2/auto-orient/strip%7CimageView2/2[/img]
[size=x-large][color=black][b]4.访问项目[/b][/color][/size]
最后我们访问以下http://127.0.0.1:8080/test
如果你看到控制台打印出:TestFilter
[img]http://upload-images.jianshu.io/upload_images/2432809-474c8a5519da268f.png?imageMogr2/auto-orient/strip%7CimageView2/2[/img]
恭喜你,配置成功!
参考项目:[url]https://github.com/bigbeef/cppba-spring-boot[/url]
github地址:[url]https://github.com/bigbeef[/url]
个人博客:[url]http://www.cppba.com[/url]
[size=x-large][color=black][b]2017-04-20 最新spring-boot增加Filter方法[/b][/color][/size]
首先定义一个Filter
@Order(1)
//重点
@WebFilter(filterName = "testFilter1", urlPatterns = "/*")
public class TestFilterFirst implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
System.out.println("TestFilter1");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
比较核心的代码是自定义类上面加上@WebFilter,其中@Order注解表示执行过滤顺序,值越小,越先执行
我们在spring-boot的入口处加上如下注解@ServletComponentScan:
@SpringBootApplication(scanBasePackages = "com.cppba")
//重点
@ServletComponentScan
public class Application {
public static void main(String[] args) throws UnknownHostException {
SpringApplication app = new SpringApplication(Application.class);
Environment environment = app.run(args).getEnvironment();
}
}
这种方法效果和上面版本一样,但是用起来更加方便!
[url]http://www.jianshu.com/p/05c8be17c80a[/url]
[size=large][color=red][b]SpringBoot初始教程之Servlet、Filter、Listener配置(七)[/b][/color][/size]
[url]http://blog.csdn.net/king_is_everyone/article/details/53116744[/url]
[size=large][color=green][b] Spring Boot 拦截器[/b][/color][/size]
[url]http://blog.csdn.net/catoop/article/details/50501696[/url]
[size=large][color=green][b] Spring Boot实战之Filter实现简单的Http Basic认证[/b][/color][/size]
[url]http://blog.csdn.net/sun_t89/article/details/51916834[/url]