spring boot -- 过滤器

1.定义一个自己的过滤器,实现Filter接口,使用@WebFilter(filterName = "myFilter", urlPatterns = "/*")注解来注册命名过滤器,指定过滤地址。
package com.huike.base.servlet;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@WebFilter(filterName = "myFilter")
public class PagesSecurityFilter implements Filter {

	@Qualifier("tokenUserInfoService")
	@Autowired(required = true)
	private TokenUserInfoService tokenUserInfoService;

	@Qualifier("publickeyService")
	@Autowired(required = true)
	private PublickeyService publickeyService;

	private static Log log = LogFactory.getLog(PagesSecurityFilter.class);

	private static final String X_REQUESTED_WITH = "X-Requested-With";

	public PagesSecurityFilter() {

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		// 设置字符集
		request.setCharacterEncoding("UTF-8");
		chain.doFilter(request, response);
	}

}

spring boot还提供了FilterRegistrationBean来进行filter的配置

package com.huike.base.servlet;

import javax.servlet.Filter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StarsFilterConfiguration {
	/* FilterRegistrationBean 用来配置urlpattern 来确定哪些路径触发filter */
	private static Log log = LogFactory.getLog(StarsFilterConfiguration.class);
	@Bean
	public FilterRegistrationBean someFilterRegistration() {
		log.info("StarsFilterConfiguration..........");
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(AuthFilter());
		registration.setEnabled(true); // 设置是否可用 
		registration.addUrlPatterns("/api/app/rest/*","/api/web/rest/*"); // 需要进行过滤的url
//		registration.addUrlPatterns("/api/app/rest/*");
		registration.setOrder(1); // 设置过滤器的顺序,可选
		return registration;
	}

	/* 使用annotation tag来取代<bean></bean> */
	@Bean()
	public Filter AuthFilter() {
		return new PagesSecurityFilter();
	}
}
3.有一个地方要注意,如果@WebFilter(filterName = "myFilter", urlPatterns = "/*")中配置的过滤地址优先级大于在FilterRegistrationBean中配置的过滤地址。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zerlinda_Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值