2016-08-24 90 views
0

我有使用spring-boot的swagger UI。我有一個無狀態的身份驗證設置,用於我的spring rest API,它基於每個API路徑的角色進行限制。限制對Swager UI的訪問

但是,我不確定我怎樣才能將<server_url>/swagger-ui.html放在基本身份驗證之後。

UPDATE

我有以下websecurity通過WebSecurityConfig

@Override 
protected void configure(HttpSecurity httpSecurity) throws Exception { 
    httpSecurity 
      .csrf().disable() 
      .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() 
      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() 
      .authorizeRequests() 
      .antMatchers("/sysadmin/**").hasRole("SYSADMIN") 
      .antMatchers("/admin/**").hasRole("ADMIN") 
      .antMatchers("/siteadmin/**").hasRole("SITEADMIN") 
      .antMatchers("/api/**").hasRole("USER") 
      .anyRequest().permitAll(); 

    // Custom JWT based security filter 
    httpSecurity 
      .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); 

} 
+0

一般而言,這是值得包括如何您目前有你的安全規定,使人們可以提供,對於設置工作的解決方案。有很多方法可以做到,但瞭解更多關於你的實施會有所幫助。 –

+0

更新了我的問題 –

回答

0

一個建議配置不知道更多關於你的配置是這太問題。

https://stackoverflow.com/a/24920752/1499549

在此處與自己更新的問題的細節是可以添加什麼一個例子:

@Override 
protected void configure(HttpSecurity httpSecurity) throws Exception { 
    httpSecurity 
      .csrf().disable() 
      .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() 
      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() 
      .authorizeRequests() 
      .antMatchers("/sysadmin/**").hasRole("SYSADMIN") 
      .antMatchers("/admin/**").hasRole("ADMIN") 
      .antMatchers("/siteadmin/**").hasRole("SITEADMIN") 
      .antMatchers("/api/**").hasRole("USER") 
      // add the specific swagger page to the security 
      .antMatchers("/swagger-ui.html").hasRole("USER") 
      .anyRequest().permitAll(); 

    // Custom JWT based security filter 
    httpSecurity 
      .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); 

} 

這樣做的問題是,它不僅保護了揚鞭UI頁面,而不是API規範的一部分,從該UI頁面加載爲.json文件。

更好的辦法是把招搖文件的路徑下,這樣就可以只添加antMatchers("/swagger/**").hasRole("USER")