【问题标题】:Spring: Using Oauth2 and HttpBasicAuth togetherSpring:同时使用 Oauth2 和 HttpBasicAuth
【发布时间】:2018-09-15 05:41:33
【问题描述】:

我使用的是 Spring Boot 2.0.0

为了保护我的 REST API,我将 Oauth2 与 JWT 结合使用,效果非常好。

问题是:

我也在使用 Springfox Swagger,它应该由 BasicAuth 保护。这样如果用户将浏览器指向 /swagger-ui.html 就会受到挑战 因此我得到了两个配置文件:

安全配置

@Configuration
@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {

    @Throws(Exception::class)
    override fun configure(web: WebSecurity) {
        web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**")
    }

    @Throws(Exception::class)
    override fun configure(auth: AuthenticationManagerBuilder) {
        auth.inMemoryAuthentication()
                //user: "user", password: "Passw0rd!"
                .withUser("user")
                .password("\$2a\$04\$DDYoNw1VAYt64.zU.NsUpOdvjZ3OVrGXJAyARkraaS00h322eL2iy")
                .roles("ADMIN")
    }
}

资源服务器配置

@Configuration
@EnableResourceServer
class ResourceServerConfig : ResourceServerConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        super.configure(http)
        http.httpBasic().and().cors().and().csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .antMatcher("/swagger-ui.html**")
                .authorizeRequests().anyRequest().hasRole("ADMIN")

    }
}

我认为这里不需要 OAuth2AuthorizationServerConfig

显示的配置(当然)不起作用,所以问题是: 是否可以混合使用 BasicAuth 和 Oauth2?

【问题讨论】:

    标签: spring spring-boot swagger springfox


    【解决方案1】:

    好的,我或多或少找到了我的问题的答案:

    安全配置

    @Configuration
    @EnableWebSecurity
    class SecurityConfig : WebSecurityConfigurerAdapter() {
    
        @Throws(Exception::class)
        override fun configure(auth: AuthenticationManagerBuilder) {
            auth.inMemoryAuthentication()
                    //user: "user", password: "Passw0rd!"
                    .withUser("user")
                    .password("...")
                    .roles("ADMIN")
        }
    
        override fun configure(http: HttpSecurity) {
            http.csrf()
                    .and()
                    .httpBasic()
                    .and()
                    .authorizeRequests()
                    .antMatchers(
                            "/swagger-ui.html**").hasRole("ADMIN")
                    .antMatchers(
                            "/v2/api-docs",
                            "/swagger-resources/**",
                            "/configuration/security", "/webjars/**"
                    ).permitAll()
        }
    }
    

    资源服务器配置

    @Configuration
    @EnableResourceServer
    class ResourceServerConfig : ResourceServerConfigurerAdapter() {
    
        override fun configure(http: HttpSecurity) {
            http.cors().and()
                    .antMatcher("/api")
                    .authorizeRequests().antMatchers("/api/**").authenticated()
        }
    }
    

    我在 ResourceServerConfig 中使用 antMatcher 仅通过 oauth2 保护 /api/** 路径。

    BasicAuth 部分发生在 SecurityConfig 中。 当前解决方案仅将 BasicAuth 应用于 /swagger-ui.html 端点。其他招摇资源是公开可见的。

    有人知道同时保护 /v2/api-docs 的方法吗?

    【讨论】:

      【解决方案2】:

      这就是将安全配置匹配到不同的应用程序上下文路径。请在 Run a Spring Boot oAuth2 application as resource server AND serving web content 上查看我的回答 - 我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2022-12-10
        • 2013-01-13
        • 2019-01-09
        • 2014-09-15
        • 2014-05-11
        • 2018-01-05
        • 2022-01-21
        • 2020-06-11
        • 2016-04-15
        相关资源
        最近更新 更多