1、在使用springBoot+springSecurity整合Swagger2的过程中,swagger每次打开都会弹出登录框,然后我并不想让它弹出,如果关闭登录弹框
2、如果修改swagger访问路径
如何关闭security+swagger登录弹出框
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- m0_54204465 2023-01-12 12:06关注
关闭 Spring Security 中的登录弹窗:
如果你不想在访问 Swagger 文档时弹出登录框,可以在 Spring Security 配置类中配置一个新的 HttpSecurity 配置对 Swagger 的路径进行特殊处理。首先在 Spring Security 配置类中配置所有路径都需要进行身份认证
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .and() .httpBasic(); } //... }
然后,针对 Swagger 的路径进行特殊处理,在配置中添加
.antMatchers("/swagger-ui.html").permitAll() .antMatchers("/swagger-resources/").permitAll() .antMatchers("/v2/api-docs").permitAll() .antMatchers("/webjars/").permitAll() @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/swagger-ui.html").permitAll() .antMatchers("/swagger-resources/**").permitAll() .antMatchers("/v2/api-docs").permitAll() .antMatchers("/webjars/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .and() .httpBasic(); } //... }
修改swagger访问路径
你可以使用 .pathProvider(new RelativePathProvider(servletContext)) 修改 swagger 访问路径@Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .pathProvider(new RelativePathProvider(servletContext)) .select() .apis(RequestHandlerSelectors.basePackage("com.example")) .paths(PathSelectors.any()) .build(); }
或者 .useDefaultResponseMessages(false) 来隐藏swagger的默认访问路径
@Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .useDefaultResponseMessages(false) .select() .apis(RequestHandlerSelectors.basePackage("com.example")) .paths(PathSelectors.any()) .build(); }
或者在 application.properties 或者 application.yml 文件中配置默认路径
swagger.path=/yourpath
注意: 上述所有路径设置都可以根据需要调整,来满足你的需求。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
- ¥500 52810做蓝牙接受端
- ¥15 基于PLC的三轴机械手程序
- ¥15 多址通信方式的抗噪声性能和系统容量对比
- ¥15 winform的chart曲线生成时有凸起
- ¥15 msix packaging tool打包问题
- ¥15 finalshell节点的搭建代码和那个端口代码教程
- ¥15 Centos / PETSc / PETGEM
- ¥15 centos7.9 IPv6端口telnet和端口监控问题
- ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作