【发布时间】: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