在“OAUTH2”启用的启动应用程序中禁用身份验证

Sun*_*aha 5 spring spring-security oauth-2.0 spring-boot

如何在 \xe2\x80\x9cOAUTH2\xe2\x80\x9d 启用的 spring-boot 应用程序中禁用身份验证?\n这通常是测试或构建阶段所必需的。

\n

Sun*_*aha 3

当我们想要进行测试/构建并且我们没有\xe2\x80\x99t 即时可用的 OAuth2 令牌时,这是一个有效的场景。\n以下是要遵循的步骤:

\n\n
    \n
  1. 创建一个没有 OAuth2 配置的 YAML 文件(即application-{profile_name}.yml)并在其中添加以下属性:

    \n\n
    Security.ignored = /** \nsecurity.basic.enable=false\n
    Run Code Online (Sandbox Code Playgroud)
  2. \n
  3. 添加一个绕过 HTTP/S 请求授权的类。注意:此类应具有相同的配置文件名称(即{profile_name})。

    \n\n
    @Profile({"profile_name"})\npublic class DisableOAuth2Config {\n\n    @Bean\n    public ResourceServerConfigurer resourceServerConfigurer() {\n        return new ResourceServerConfigurerAdapter() {\n            @Override\n            public void configure(HttpSecurity http) throws Exception {\n                http\n                    .authorizeRequests()\n                        .antMatchers("/**").permitAll();\n            }\n        };\n    }\n}\n
    Run Code Online (Sandbox Code Playgroud)
  4. \n
  5. Profile在课堂上提供SecurityConfiguration我们仍然想要安全的地方。@Profile({"local","dev","aws"}),这将区分启用/禁用安全性的配置文件。

  6. \n
  7. 注:请检查注释@EnableResourceServe@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)。它们应该只在SecurityConfiguration课堂上可用。不是多个地方,也不是在SpringBoot课堂上。

  8. \n
\n