标签:ret mic 注入 ack get 使用 tor imm link
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 路径下的请求才输出的。可自行测试哦~~~
标签:ret mic 注入 ack get 使用 tor imm link
原文地址:https://www.cnblogs.com/zgq7/p/11077426.html