我在OAuth2中使用。我不想禁用健康端点的安全性。
我可以完全禁用安全性,或者编写自己的WebSecurityConfigurerAdapter
实现,并禁用自动配置的实现。
但是如何修改WebSecurityConfigurerAdapter
(OAuth2SsoDefaultConfiguration
)的现有实现呢?
我试图在不禁用自动配置的情况下创建自己的配置,但由于Order
冲突,这是不可能的。
以下是错误消息:
Caused by: java.lang.IllegalStateException: @Order on WebSecurityConfigurers must be unique.
Order of 100 was already used on SecurityConfiguration$$EnhancerBySpringCGLIB$$9505fc58@13f182b9,
so it cannot be used on
org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2SsoDefaultConfiguration$$EnhancerBySpringCGLIB$$dc290e2b@5ee0cf64 too.
此外,我试图为我自己的安全配置显式设置更高的顺序,但看起来像自动配置的一个覆盖了我的配置。
那么,如何在不重新实现整个配置的情况下覆盖特定的安全规则?
发布于 2018-09-02 17:42:19
您需要在
@SpringBootApplication
类
@SpringBootApplication
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class BusinessLogicServiceApplication extends ResourceServerConfigurerAdapter {
public static void main(String[] args) throws IOException {
ConfigurableApplicationContext context =
SpringApplication.run(BusinessLogicServiceApplication.class, args);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/health").permitAll().anyRequest().authenticated();
}
}
发布于 2018-09-02 20:26:04
@Configuration
@EnableOAuth2Sso
class MyConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/actuator/health")
.permitAll()
.anyRequest()
.authenticated();
}
}
确保您是在WebSecurityConfigurerAdapter
类上使用WebSecurityConfigurerAdapter
。这很重要,因为它将包括OAuth2SsoCustomConfiguration
,它基本上复制了OAuth2SsoDefaultConfiguration#configure
的功能。
您还可能希望显示完整的健康详细信息:
management:
endpoint:
health:
show-details: always
发布于 2018-09-02 16:44:57
以下是可能的支票。
解决方案1:确保您正在使用
org.springframework.core.annotation.Order
而不是
org.apache.logging.log4j.core.config.Order
因为Spring没有解析正确的注释,所以它假设这两种配置的默认值为100。
解决方案2:
也许您已经用@EnableWebSecurity注释了另一个类。请注意,只有一个类可以实现此注释。
解决方案3:请参阅此https://stackoverflow.com/a/44076087/6572971
解决方案4:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class DemoConfigurer extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception{
http.authorizeRequests().antMatchers("/health").permitAll();
super.configure(http);
}
}
https://stackoverflow.com/questions/52138679
复制相似问题