启用S​​pring Security使Swagger输出text / plain而不是HTML

时间:2015-02-04 00:31:56

标签: spring-security spring-boot swagger swagger-ui swagger-maven-plugin

Swagger工作!我可以与http://localhost:8090/sdoc.jsp互动,一切都很好。

我将以下内容添加到pom.xml ...

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

我还添加了以下两个文件:

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String name = authentication.getName();
        String password = authentication.getCredentials().toString();

        if( !Authenticate.authenticate(name, password) )
            return null;

        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
        return auth;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
                .disable()

            .authorizeRequests()
                .anyRequest().permitAll()
                .antMatchers("/**").authenticated().and()
                .formLogin().loginPage("/login").permitAll().and()
                .httpBasic()
                ;
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new CustomAuthenticationProvider());
    }
}

此时,如果我访问之前工作的相同网址,我现在改为获得“text / plain”的响应类型,而不是看起来很漂亮的HTML浏览器,我看到了源代码。

如果我还原更改并从项目中删除这两个文件并删除JAR文件,它将再次起作用。

如何让Spring Security和Swagger玩得很好?我做错了什么。

2 个答案:

答案 0 :(得分:3)

我怀疑这是由于Spring-Security对内容类型标题(http://docs.spring.io/spring-security/site/docs/3.2.0.CI-SNAPSHOT/reference/html/headers.html#headers-content-type-options)的影响。

来自文档 -

  

历史上,浏览器(包括Internet Explorer)会尝试使用内容嗅探来猜测请求的内容类型。这允许浏览器通过猜测未指定内容类型的资源上的内容类型来改善用户体验。例如,如果浏览器遇到未指定内容类型的JavaScript文件,则可以猜测内容类型然后执行它。

     

内容嗅探的问题在于,这允许恶意用户使用多字符(即,作为多种内容类型有效的文件)来执行XSS攻击。例如,某些站点可能允许用户向网站提交有效的postscript文档并进行查看。恶意用户可能会创建一个postscript文档,该文档也是一个有效的JavaScript文件,并使用它执行XSS攻击。

再次,从文档中,为了覆盖默认值 -

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
   WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      // ...
      .headers()
        .contentTypeOptions();
  }
}

答案 1 :(得分:1)

哇,我认为这是沿着这些方向发展的。非常感谢

当我尝试这个并开始工作时

.headers()
    .disable()

我将默认的contentTypeOptions缩小为..

.headers()
        //.contentTypeOptions()   // If this is uncommented it fails.
        .xssProtection()
        .cacheControl()
        .httpStrictTransportSecurity()
        .frameOptions()
        .and()