401未经授权的页面招摇?

时间:2018-04-04 19:58:11

标签: spring-boot swagger swagger-ui swagger-2.0

我通过@ 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

任何想法,伙计们?

4 个答案:

答案 0 :(得分:2)

认为您可以添加自己的WebSecurityConfig extends WebSecurityConfigurerAdapter,而不是覆盖configure(WebSecurity web)方法,并将web.ignoring().antMatchers("/swagger-ui.html") ofc用@Configuration

注释该类

答案 1 :(得分:1)

我有相同的错误,并将此代码添加到类 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);
    }

答案 2 :(得分:0)

正如Georgi Stoyanov回答的那样,添加了许多代码,从而删除了 Whitelabel错误页面错误,但是我的招摇的UI主页是空白的,因为加载某些css和js文件时出现401问题。 Swagger-ui with Spring security

此外,我想提到的重要一点是,我的招摇过的UI在没有上述代码的情况下即可用于Weblogic部署(仅使用HttpSecurity覆盖就足够了),并且仅在嵌入式tomcat中运行应用程序时才遇到问题。

Spring Boot版本:1.5.2。发布

SpringFox版本2.8.0

因此,我不得不进行代码更改as answered by me in linked question才能同时加载所有CSS和JS文件。

答案 3 :(得分:0)

那时我在项目中遇到了同样的问题, 首先将配置文件添加到项目中

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/**");
}

我的问题已解决