@RefreshScope和@Order

时间:2018-11-12 11:58:06

标签: spring-boot spring-cloud-config

我正在尝试将@RefreshScope@Order一起使用。引导运行失败并出现错误

@Order on WebSecurityConfigurers must be unique. Order of 2147483636 was already used on com.company.rest.config.ActuatorSecurityConf$$EnhancerBySpringCGLIB$$1e7c9a55@6559faf1, so it cannot be used on com.company.rest.config.ActuatorSecurityConf$$EnhancerBySpringCGLIB$$1e7c9a55@6559faf1 too.

@RefreshScope使bean多次初始化,因为它使用代理。可以将@RefreshScope@Order一起使用吗?

@Configuration
@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)
@RefreshScope
public class ActuatorSecurityConf extends WebSecurityConfigurerAdapter {

    private ApplicationProperties properties;

    public ActuatorSecurityConf(ApplicationProperties properties) {
        this.properties = properties;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // @formatter:off
        auth.inMemoryAuthentication()
            .withUser(properties.getActuatorUsername())
            .password(properties.getActuatorUserPassword())
            .roles("ACTUATOR");
        // @formatter:on
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.antMatcher("/manage/**")
                .csrf().disable()
                .cors()
            .and()
                .authorizeRequests()
                .antMatchers("/manage/info").permitAll()
                .anyRequest().hasRole("ACTUATOR")
            .and()
                .httpBasic();
        // @formatter:on
    }

}

更新:

根据建议从@RefreshScope中移除了@Configuration,但仍然发生错误。

@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)必须放置在应用程序中另一个WebSecurityConfigurerAdapter之前, @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)

@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)
public class ActuatorSecurityConf extends WebSecurityConfigurerAdapter {
    private ApplicationProperties properties;

    @Autowired
    public ActuatorSecurityConf(ApplicationProperties properties) {
        this.properties = properties;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // same
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // same
    }
}

@Configuration
class ApplicationConfig {
    @Bean
    @RefreshScope
    public ApplicationProperties applicationProperties() {
        return new ApplicationProperties();
    }

    @Bean
    @RefreshScope
    public ActuatorSecurityConf actuatorSecurityConf() {
        return new ActuatorSecurityConf(applicationProperties());
    }
}

使用Spring Boot 1.5.4.RELEASE

0 个答案:

没有答案