gpt4 book ai didi

Spring security 如何开放 Swagger 访问权限

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Spring security 如何开放 Swagger 访问权限由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

Spring security 开放 Swagger 访问权限

开放这四个目录

搞定 。

?
1
2
3
4
.antMatchers( "/swagger-ui.html" ).permitAll()
.antMatchers( "/webjars/**" ).permitAll()
.antMatchers( "/v2/**" ).permitAll()
.antMatchers( "/swagger-resources/**" ).permitAll()

spring boot 加入拦截器后swagger不能访问

spring boot 加入拦截器后swagger不能访问问题 。

未加入拦截器时,swagger可以正常访问接口信息,但是加入拦截器之后swagger就不能访问了 。

原因分析

不能访问的原因的swagger的内置接口被拦截器拦下来了 。

Spring security 如何开放 Swagger 访问权限

图片中可以看到swagger的所有请求的url信息,只要把他们加到拦截器的排除列表中即可 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.trimps928.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
  * @author liubing
  * @version 2018-06-26
  * 拦截器配置
  **/
@Configuration
public class MyWebAppConfig extends WebMvcConfigurationSupport {
     @Bean
     LoginInterceptor localInterceptor() {
         return new LoginInterceptor();
     }
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(localInterceptor())
                 .addPathPatterns( "/**" )
                 .excludePathPatterns( "/user/login" )
                 .excludePathPatterns( "/swagger-resources/**" , "/webjars/**" , "/v2/**" , "/swagger-ui.html/**" );
     }
     @Override
     protected void addResourceHandlers(ResourceHandlerRegistry registry) {
         registry.addResourceHandler( "swagger-ui.html" )
                 .addResourceLocations( "classpath:/META-INF/resources/" );
         registry.addResourceHandler( "/webjars/**" )
                 .addResourceLocations( "classpath:/META-INF/resources/webjars/" );
     }
}

网上找的资料中大部分只说添加这个

?
1
2
3
4
5
6
7
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localInterceptor())
.addPathPatterns( "/**" )
.excludePathPatterns( "/user/login" )
.excludePathPatterns( "/swagger-resources/**" , "/webjars/**" , "/v2/**" , "/swagger-ui.html/**" );
}

或者只添加

?
1
2
3
4
5
6
7
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler( "swagger-ui.html" )
.addResourceLocations( "classpath:/META-INF/resources/" );
registry.addResourceHandler( "/webjars/**" )
.addResourceLocations( "classpath:/META-INF/resources/webjars/" );
}

无数次的实验发现这两个方法都需要重写,只加任何一个都无法生效.

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.

原文链接:https://haoxuanli.blog.csdn.net/article/details/104422027 。

最后此篇关于Spring security 如何开放 Swagger 访问权限的文章就讲到这里了,如果你想了解更多关于Spring security 如何开放 Swagger 访问权限的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com