Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh Configuration Beans #504

Closed
freedombird9 opened this issue Sep 20, 2016 · 7 comments
Closed

Refresh Configuration Beans #504

freedombird9 opened this issue Sep 20, 2016 · 7 comments

Comments

@freedombird9
Copy link

I have a web security config class that looks like:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private GatewayRememberMeServices rememberMeServices;

    @Autowired
    private BasicAuthenticationProvider basicAuthenticationProvider;

    @Autowired
    private BasicAuthenticationEntrypoint basicAuthenticationEntrypoint;

    @Autowired
    private Config config;

    @Bean
    public BasicAuthenticationFilter basicAuthenticationFilter(){
        BasicAuthenticationFilter basicAuthenticationFilter = new BasicAuthenticationFilter(new ProviderManager(Arrays.asList(basicAuthenticationProvider)), basicAuthenticationEntrypoint);
        return basicAuthenticationFilter;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.authorizeRequests().antMatchers("/**").permitAll()
                .anyRequest().authenticated().and()
                .rememberMe()
                .rememberMeServices(rememberMeServices)
                .key("springRocks")
                .rememberMeCookieName("jsSessionCookie");

        http.addFilter(basicAuthenticationFilter());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        if (config == null || config.getPath() == null) {
            return;
        }
        for (String allow: config.getPath()) {
            web.ignoring().antMatchers(allow + "**");
        }
    }
}

The Config is:

@Component
@ConfigurationProperties("ignored")
@RefreshScope
public class Config {
    private List<String> path;

    public List<String> getPath() {
        return path;
    }

    public void setPath(List<String> path) {
        this.path = path;
    }
}

So basically, whenever I change the ignored property, the Config bean will be refreshed. It won't happen, however, to the SecurityConfig bean. Since @RefreshScope doesn't work with @configuration, I wonder how to refresh the config bean? Specially, I want to have the method

@Override
    public void configure(WebSecurity web) throws Exception {
        if (config == null || config.getPath() == null) {
            return;
        }
        for (String allow: config.getPath()) {
            web.ignoring().antMatchers(allow + "**");
        }
    }

re-run to reflect the changes. Right now, it cannot pick up any change I made to the property file.

@spencergibb
Copy link
Member

Remove @RefreshScope from Config since @ConfigurationProperties classes are already refreshed. Where do you create Config? Where do you have @EnableConfigurationProperties?

@spencergibb
Copy link
Member

To reconfigure security (if it can be reconfigured) you would have to listen for an EnvironmentChangeEvent

@freedombird9
Copy link
Author

@spencergibb Config is autowired into SecurityConfig bean. And I didn't use @EnableConfigurationProperties anywhere.

@freedombird9
Copy link
Author

@spencergibb You mean there is no clean way to reconfigure security?

@spencergibb
Copy link
Member

@EnableConfigurationProperties is required for @ConfigurationProperties to work. I have no idea if security can by dynamically reconfigured. Ping @rwinch.

@freedombird9
Copy link
Author

@spencergibb Good to know. But the above code did work for me. I've tested it many many times. I think @EnableConfigurationProperties is the default setting now.

@spring-projects-issues
Copy link

Closing due to age of the question. If you would like us to look at this issue, please comment and we will look at re-opening the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants