2

我已经开发了一个弹簧启动和oauth2演示应用程序。我有三个应用程序如下。Swagger UI没有加载Oauth2

  1. 资源api:这将有我们需要保护的所有方法。
  2. 验证服务器:这是一个提供令牌的oauth2服务器
  3. UI:在通过验证服务器成功登录后,它将能够访问资源。

我有很少的资源(控制器)可以公开访问。但是当我试图访问这个swagger UI时,它会要求我进行完整的身份验证。当我添加下面的代码时,swagger页面即将到来,但不仅仅是整个页面中的一个下拉菜单,而且也被打破。

http 
.anonymous() //allow anonymous access 
.and() 
.authorizeRequests() 
.antMatchers("/shouldbepublic/**", "/swagger-ui**/**").permitAll().and() //this should be public resources 
.formLogin().loginPage("/login").permitAll() 
.and() 
.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access") 
.and() 
.authorizeRequests().anyRequest().authenticated(); 
+0

你是否使用springfox-swagger-ui作为依赖项? –

回答

0

这里是为我工作的配置:

.antMatchers("/swagger-ui.html","/swagger-resources/**", "/v2/api-docs/**").permitAll()'.antMatchers("/swagger-ui.html","/swagger-resources/**", "/v2/api-docs/**").permitAll()

没有了招摇,ui.html无法显示

完整ResourceServiceConfiguration可以在这里找到:

@Configuration 
@EnableResourceServer 
public static class ResourceServiceConfiguration extends ResourceServerConfigurerAdapter { 

    @Override 
    public void configure(final HttpSecurity http) throws Exception { 
    // @formatter:off 
    http.csrf().disable().authorizeRequests() 
    // This is needed to enable swagger-ui interface. 
    .antMatchers("/swagger-ui.html","/swagger-resources/**", "/v2/api-docs/**").permitAll() 
    .antMatchers("/**").hasAuthority("ADMIN"); 
    // @formatter:on 
    } 
} 
0

另一种选择(如果它是你安全的)是有春季安全完全忽略/swagger-ui路径。如果您正在使用Swagger2尝试允许

@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring().antMatchers(HttpMethod.GET, "/swagger-ui/**"); 
    super.configure(web); 
} 
+0

感谢Marco,我会尽力让你知道! – Sam

0

在您的安全@Configuration文件扩展WebSecurityConfigurerAdapter,并添加以下

http.authorizeRequests().antMatchers("/configuration/**","/swagger**","/webjars/**","/v2/**").permitAll(); 

得到它的工作。