springboot2.1中添加过滤器配置
Posted zgq7
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot2.1中添加过滤器配置相关的知识,希望对你有一定的参考价值。
1:构造一个实现 Filter 接口的过滤器,并在类上添加@component注释:
notice1:若不添加,则需在spring中注入该bean,不然会报错。
package com.dev.filter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; /** * Created by zgq7 on 2019/6/6. */ @Component public class BaseFilter implements Filter private static final Logger log = LoggerFactory.getLogger(BaseFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException Map<Object, Object> map = new LinkedHashMap<>(10); map.put("filterName", filterConfig.getFilterName()); log.info("filter config indclude :", map); @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String uri = request.getRequestURI(); Map<Object, Object> map = new HashMap<>(10); map.put("url", request.getRequestURL()); map.put("uri", uri); map.put("requestType", request.getMethod()); log.info("", map); filterChain.doFilter(request, response); @Override public void destroy() log.info("filter destroyed ...");
2:编写过滤器配置类,并在类上添加@Configuration注释,告诉spring声明这是一个配置类:
package com.dev.config; import com.dev.filter.BaseFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.*; /** * Created by zgq7 on 2019/6/6. */ @Configuration public class SpringConfig @Bean public FilterRegistrationBean<BaseFilter> filterFilterRegistrationBean(BaseFilter baseFilter) FilterRegistrationBean<BaseFilter> filterFilterRegistrationBean = new FilterRegistrationBean<>(); //拦截路径配置 List<String> uriList = new ArrayList<>(10); uriList.add("/dev/*"); filterFilterRegistrationBean.setFilter(baseFilter); filterFilterRegistrationBean.setEnabled(true); filterFilterRegistrationBean.setUrlPatterns(uriList); filterFilterRegistrationBean.setName("baseFilter"); filterFilterRegistrationBean.setOrder(1); return filterFilterRegistrationBean;
3:编写一个controller进行测试:
package com.dev.controller; import com.google.common.collect.ImmutableMap; import org.springframework.web.bind.annotation.*; import java.util.Map; /** * Created by zgq7 on 2019/6/6. */ @RestController @RequestMapping(value = "/dev") public class BaseController @GetMapping(value = "") public Map<Object, Object> get() return ImmutableMap.of("code", "get"); @PutMapping(value = "t") public Map<Object, Object> t() return ImmutableMap.of("code", "put"); @PostMapping(value = "s") public Map<Object, Object> s() return ImmutableMap.of("code", "post");
4:使用psotMan进行测试:
5:控制台如下:
输出该日志是因为过滤器拦截到了 /dev 路径下的请求才输出的。可自行测试哦~~~
以上是关于springboot2.1中添加过滤器配置的主要内容,如果未能解决你的问题,请参考以下文章
使用 @EnableAuthorizationServer 时如何在 HTTP BasicAuthenticationFilter 之后添加过滤器