【问题标题】:Enabling Spring Security makes Swagger output text/plain instead of HTML启用 Spring Security 使 Swagger 输出 text/plain 而不是 HTML
【发布时间】:2015-04-03 09:17:45
【问题描述】:

招摇有效!我可以与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());
    }
}

此时,如果我访问以前工作的相同 URL,我现在会得到一个“text/plain”的响应类型,而不是一个漂亮的 HTML 浏览器,我看到的是源代码。

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

如何让 Spring Security 和 Swagger 发挥出色?我做错了什么。

【问题讨论】:

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


    【解决方案1】:

    我怀疑这是由于 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 文档并进行查看。恶意用户可能会创建一个也是有效 JavaScript 文件的 postscript 文档并使用它执行 XSS 攻击。

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

    @EnableWebSecurity
    @Configuration
    public class WebSecurityConfig extends
       WebSecurityConfigurerAdapter {
    
      @Override
      protected void configure(HttpSecurity http) throws Exception {
        http
          // ...
          .headers()
            .contentTypeOptions();
      }
    }
    

    【讨论】:

    • 太好了,感谢您的反馈和添加的信息。我希望它也能帮助其他人。
    【解决方案2】:

    哇,我认为它是这样的。非常感谢

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

    .headers()
        .disable()
    

    我将默认的 contentTypeOptions 缩小到..

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

    【讨论】:

      猜你喜欢
      • 2021-08-04
      • 1970-01-01
      • 2012-05-19
      • 2016-03-10
      • 2012-09-11
      • 2015-07-20
      • 2019-03-08
      • 2017-03-05
      • 2012-06-02
      相关资源
      最近更新 更多