springboot自定义Filter

Posted 沐风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot自定义Filter相关的知识,希望对你有一定的参考价值。

自定义Filter

  我们常常在项目中会使用filters用于录调用日志、排除有XSS威胁的字符、执行权限验证等等。

  Spring Boot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,并且我们可以自定义Filter。

两个步骤:

  实现Filter接口,实现Filter方法

  添加@Configurationz 注解,将自定义Filter加入过滤链

package cn.cnki.ref.filter;

import org.apache.catalina.filters.RemoteIpFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Configuration
public class WebConfiguration {
    @Bean
    public RemoteIpFilter remoteIpFilter() {
        return new RemoteIpFilter();
    }

    @Bean
    public FilterRegistrationBean testFilterRegistration() {

        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("MyFilter");
        registration.setOrder(1);
        return registration;
    }

    public class MyFilter implements Filter {
        @Override
        public void destroy() {
            // TODO Auto-generated method stub
        }

        @Override
        public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)
                throws IOException, ServletException {
            // TODO Auto-generated method stub
            HttpServletRequest request = (HttpServletRequest) srequest;
            System.out.println("this is MyFilter,url :"+request.getRequestURI());
            filterChain.doFilter(srequest, sresponse);
        }

        @Override
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
        }
    }
}

 测试结果

自定义Property

  在web开发的过程中,我经常需要自定义一些配置文件,如何使用呢

  配置在application.properties中

cn.mf.name=mf
cn.mf.description=博客园

自定义配置类

package cn.cnki.ref.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyProperties {
    @Value("${cn.mf.name}")
    private String name;

    @Value("${cn.mf.description}")
    private String description;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

测试

package cn.cnki.ref.config;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyPropertiesTest {
    @Autowired
    MyProperties  myProperties;

    @Test
    public void getNamegetDescription() {
        System.out.println(myProperties.getName());
        System.out.println(myProperties.getDescription());
    }

}

解决Spring Boot自定义属性乱码问题

 

 

以上是关于springboot自定义Filter的主要内容,如果未能解决你的问题,请参考以下文章

springboot初步认识---自定义filter

springboot自定义Filter

SpringBoot自定义拦截器和跨域配置冲突

Spring Boot:考虑到用户角色,如何编写自定义 Pre Filter?

Spring Boot 实战:如何自定义 Servlet Filter

VSCode自定义代码片段——CSS选择器