spring-security - 启用 Spring Security 使 Swagger 输出文本/纯文本而不是 HTML

标签 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());
    }
}

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

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

如何让 Spring Security 和 Swagger 和谐相处?我做错了什么。

最佳答案

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

来自文档 -

Historically browsers, including Internet Explorer, would try to guess the content type of a request using content sniffing. This allowed browsers to improve the user experience by guessing the content type on resources that had not specified the content type. For example, if a browser encountered a JavaScript file that did not have the content type specified, it would be able to guess the content type and then execute it.

The problem with content sniffing is that this allowed malicious users to use polyglots (i.e. a file that is valid as multiple content types) to execute XSS attacks. For example, some sites may allow users to submit a valid postscript document to a website and view it. A malicious user might create a postscript document that is also a valid JavaScript file and execute a XSS attack with it.

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

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
   WebSecurityConfigurerAdapter {

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

关于spring-security - 启用 Spring Security 使 Swagger 输出文本/纯文本而不是 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28311423/

相关文章:

java - Spring 保护 Web 应用程序和 Rest 应用程序

scala - 资源 url 中的 Swagger (spray) 路径参数

c# - Swagger - 如何显示更复杂的响应示例 - ASP.net Core Web API

Spring Boot - 来自依赖项的多个过滤器链

spring-security - spring security : In 3. 1,仅针对 'GET'请求绕过安全过滤器

Spring Boot、Hibernate 搜索属性

SpringBoot : how to inject two classes having same name

java - Swagger 的 Jersey REST API 不起作用

web-services - 如何使用Spring Security保护Grails Web服务

java - 响应中没有 namespace 的 SOAP(Spring Boot)