简体   繁体   English

弹簧过滤器在初始化功能后丢失设置变量

[英]Spring filter losing set variables after init function

I'm having a weird problem with a simple spring filter I'm using. 我在使用一个简单的弹簧过滤器时遇到了一个奇怪的问题。 In the init function I'm setting a variable this.test = "TEST1234" but for some reason when reaching then doFilter function this variable is reverted to null again. init函数中,我设置了一个变量this.test = "TEST1234"但是由于某种原因,当到达doFilter函数时,该变量再次恢复为null。

My filter: 我的过滤器:

@Component
public class TestFilter implements Filter {
    private String test;

    public void init(FilterConfig cfg) {
        this.test = "TEST1234";
        System.out.println("TEST: " + this.test);

    }

    public void doFilter(
        ServletRequest request,
        ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
        System.out.println("TEST: " + this.test);
    }

    public void destroy() {}
}

SecurityConfig: SecurityConfig:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .addFilterBefore(
                    new TestFilter(), 
                    AbstractPreAuthenticatedProcessingFilter.class
                );
    }
}

Console output: 控制台输出:

init: TEST: TEST1234 doFilter: TEST: null 初始化: TEST: TEST1234 doFilter: TEST: null

Why is this variable reverted to null? 为什么将此变量恢复为null? Am I missing something? 我想念什么吗? Is it garbage collected? 是垃圾收集吗?

I'm running my application on Java 1.8.0_171 我在Java 1.8.0_171上运行我的应用程序

Dependencies: 依存关系:

  • Spring boot starter web: 2.1.4.RELEASE Spring Boot Starter网站: 2.1.4.RELEASE
  • Spring security jwt: 1.0.10.RELEASE Spring Security 1.0.10.RELEASE1.0.10.RELEASE
  • Spring security oauth2: 2.2.1.RELEASE Spring Security oauth2: 2.2.1.RELEASE
  • Javax servlet: 2.5 Javax Servlet: 2.5

Solved it thanks to the comment from @dur . 通过@dur的评论解决了它。

First I removed the @Component annotation so my filter doesn't get instantiated twice. 首先,我删除了@Component批注,以使我的过滤器不会实例化两次。

Filter: 过滤:

public class TestFilter implements Filter {
    private String test;

    public void init(FilterConfig cfg) {
        this.test = "TEST1234";
        System.out.println("TEST: " + this.test);

    }

    public void doFilter(
        ServletRequest request,
        ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
        System.out.println("TEST: " + this.test);
    }

    public void destroy() {}
}

Also I have created the filter instance as a bean instead of calling newFilter() directly in the addFilterBefore() function. 另外,我已经将过滤器实例创建为bean而不是直接在addFilterBefore()函数中调用newFilter()

SecurityConfig: SecurityConfig:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public TestFilter testFilter() {
        return new TestFilter();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .addFilterBefore(
                        testFilter(),
                        AbstractPreAuthenticatedProcessingFilter.class
                );
    }
}

Console output (now as expected): 控制台输出(现在如预期):

init: TEST: TEST1234 doFilter: TEST: TEST1234 初始化: TEST: TEST1234 doFilter: TEST: TEST1234

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 restTemplate 之后设置弹簧过滤器 - set spring filter after restTemplate 设置属性后,Spring并延迟Bean Init - Spring and delaying Bean Init to after properties have been set 为什么Spring Boot Filter init函数以jar和war包的不同顺序执行 - Why Spring boot Filter init function execute in different order pack as jar and war package Spring 数据 JPA 使用@query 或 function 名称过滤枚举值集 - Spring Data JPA filter set of enum values with @query or by function name 嵌入式Jetty是否能够设置过滤器的init-params? - Does embedded Jetty have the ability to set the init-params of a filter? 如何为春季由组件扫描自动创建的bean设置Init方法 - How to set Init method for beans automatically created by Component Scanning in spring 如何使用 Spring 应用程序的文件常量设置初始化方法? - How to set up init method with file constants for Spring Application? 如何在Spring 4中通过注释设置默认的bean初始化方法? - how to set default beans init-method by annotations in spring 4? 函数调用后最终对象丢失属性值 - Final object losing attribute value after function call 功能中设置的变量不断变化 - Variables set in function are constantly changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM