SpringBoot集成Security,Swagger遇到的坑

1.在使用SpringBoot开发项目的过程中同时集成Spring Security和Swagger 3.0.0版本

1.1 在pox文件中添加依赖

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

1.2 访问 http://localhost:8080/swagger-ui/,可能出现下面的问题

image.png

或者
image.png

2.出现上面的情况都是因为spring Security把Swagger需要请求的内容拦截了。需要在SecurityConfig中的configure拦截规则中添加以下代码

.antMatchers("/swagger-ui/**").anonymous()
.antMatchers("/swagger-resources/**").anonymous()
.antMatchers("/profile/**").anonymous()
.antMatchers("/profile/**").anonymous()
.antMatchers("/v3/**").anonymous()

3. 应该就可以正常的访问了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。