Spring security怎么开放Swagger访问权限

发布时间:2021-09-06 17:27:51 作者:小新
来源:亿速云 阅读:327

这篇文章将为大家详细讲解有关Spring security怎么开放Swagger访问权限,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

Spring security 开放 Swagger 访问权限

开放这四个目录

搞定

.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信息,只要把他们加到拦截器的排除列表中即可

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/");
    }
}
网上找的资料中大部分只说添加这个
@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/"); 
}

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

关于“Spring security怎么开放Swagger访问权限”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. 10道面试官喜欢问的微服务面试题Spring Cloud+Spring Boot
  2. 从Spring Boot 1.5升级到2.0

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring security swagger

上一篇:网站导航怎么做SEO优化

下一篇:XP系统设置无线网络以及及查看无线网络配置的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》