如何在启用“OAUTH2”的 spring-boot 应用程序中禁用身份验证? 这通常是测试或构建阶段所必需的。
最佳答案
当我们想要进行测试/构建并且没有即时可用的 OAuth2 token 时,这是一个有效的场景。 以下是应遵循的步骤:
创建一个不带 OAuth2 配置的 YAML 文件(即
application-{profile_name}.yml
)并在其中添加以下属性:Security.ignored = /** security.basic.enable=false
添加一个绕过 HTTP/S 请求授权的类。注意:此类应具有相同的配置文件名称(即
{profile_name}
)。@Profile({"profile_name"}) public class DisableOAuth2Config { @Bean public ResourceServerConfigurer resourceServerConfigurer() { return new ResourceServerConfigurerAdapter() { @Override public void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/**").permitAll(); } }; } }
在我们仍希望获得安全性的
SecurityConfiguration
类中提供Profile
。@Profile({"local","dev","aws"})
,这将区分启用/禁用安全性的配置文件。注意:请检查注解
@EnableResourceServe
、@EnableWebSecurity
和@EnableGlobalMethodSecurity(prePostEnabled = true)
。它们应该仅在SecurityConfiguration
类中可用。不是多个地方,也不在SpringBoot
类中。
关于spring - 在 “OAUTH2” 启用的启动应用程序中禁用身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48090555/