springboot2.1中添加过滤器配置
2020-12-13 03:31
标签:ret mic 注入 ack get 使用 tor imm link 1:构造一个实现 Filter 接口的过滤器,并在类上添加@component注释: notice1:若不添加,则需在spring中注入该bean,不然会报错。 2:编写过滤器配置类,并在类上添加@Configuration注释,告诉spring声明这是一个配置类: 3:编写一个controller进行测试: 4:使用psotMan进行测试:
5:控制台如下:
输出该日志是因为过滤器拦截到了 /dev 路径下的请求才输出的。可自行测试哦~~~ springboot2.1中添加过滤器配置 标签:ret mic 注入 ack get 使用 tor imm link 原文地址:https://www.cnblogs.com/zgq7/p/11077426.htmlpackage 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
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
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