401 swagger 未经授权的页面?

问题描述 投票:0回答:5

我通过@EnableSwagger2启用了swagger2。但是,当我尝试点击“/swagger-ui.html”时,它首先点击我的身份验证过滤器。然后,我编写了以下代码来绕过身份验证检查

String resourcePath = new UrlPathHelper().getPathWithinApplication(httpRequest);
if ("/swagger-ui.html".equalsIgnoreCase(resourcePath)) {
     filterChain.doFilter(request, response);
}

我可以看到

filterChain.doFilter(request, response);
被击中了。但是,当我停止调试时,它会返回一个包含以下信息的页面

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Apr 04 15:41:50 EDT 2018
There was an unexpected error (type=Unauthorized, status=401).
No message available

大家有什么想法吗?

spring-boot swagger swagger-ui swagger-2.0
5个回答
9
投票

我在项目中遇到了同样的问题并发现了这个解决方案,所以首先将此 config 文件添加到项目中

package bla.bla.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors
                        .basePackage("bla.bla.controllers"))
                .paths(PathSelectors.any())
                .build();
    }

}

然后将此代码块添加到您WebSecurityConfig

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().mvcMatchers(HttpMethod.OPTIONS, "/**");
    web.ignoring().mvcMatchers("/swagger-ui.html/**", "/configuration/**", "/swagger-resources/**", "/v2/api-docs","/webjars/**");
}

我的问题已解决

source
swagger.io


6
投票

认为您可以添加自己的

WebSecurityConfig extends WebSecurityConfigurerAdapter
,而不是覆盖
configure(WebSecurity web) 
方法,然后将
web.ignoring().antMatchers("/swagger-ui.html")
ofc用
@Configuration

注释该类
    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Override
        public void configure(WebSecurity web) throws Exception {
            super.configure(web);
            web.ignoring().antMatchers("swagger-ui/**", "swagger-ui**", "/v3/api-docs/**", "/v3/api-docs**");
        }

2
投票

我有同样的错误,我在类中添加了此代码websecurityConfig

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests().antMatchers("/api/auth/**").permitAll() 
                .antMatchers("/api/test/**").permitAll() // permit the class of test
                .antMatchers("/**").permitAll() // permit all the routers after swagger-ui.html
                .anyRequest().authenticated();

        http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
    }

0
投票

正如 Georgi Stoyanov 所回答的,添加那么多代码删除了 Whitelabel Error Page 错误,但我的 swagger UI 主页是空白的,因为加载一些 css 和 js 文件时出现 401 问题。 Swagger-ui 与 Spring 安全性

另外,我想提的重要一点是,我的 swagger UI 无需上述代码即可用于 Weblogic 部署(仅

HttpSecurity
覆盖就足够了),并且仅在嵌入式 tomcat 中运行应用程序时我才遇到问题。

Spring Boot 版本:1.5.2.RELEASE

SpringFox 版本 2.8.0

所以我必须更改代码如我在链接问题中回答的那样也加载所有CSS和JS文件。


0
投票

我遇到了同样的问题,这就是我的解决方案。

如果你有 Spring 安全配置,你必须对 swagger 需要的所有 url 进行授权

@Override
protected void configure(HttpSecurity http) throws Exception {

    http
        .csrf()
        .disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.POST,"/api/loggin").permitAll()
        .antMatchers(HttpMethod.GET,"/swagger-resources/**").permitAll()
        .antMatchers(HttpMethod.GET,"/swagger-ui/**").permitAll()
        .antMatchers(HttpMethod.GET,"/v2/api-docs").permitAll()
        .anyRequest()
        .authenticated();           
}

并且在你的主课中你应该添加这个符号

@EnableSwagger2

最后在你的 pom.xml 中添加这个依赖项。

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>

使用http://localhost:8090/swagger-ui/

© www.soinside.com 2019 - 2024. All rights reserved.