Spring security 如何開放 Swagger 訪問權限
更新時間:2021年09月06日 14:52:54 作者:李昊軒的博客
這篇文章主要介紹了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的內置接口被攔截器攔下來了

圖片中可以看到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/");
}
無數次的實驗發(fā)現這兩個方法都需要重寫,只加任何一個都無法生效。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Myeclipse部署Tomcat_動力節(jié)點Java學院整理
這篇文章給大家介紹了Myeclipse部署Tomcat的相關知識,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-07-07
Spring/SpringBoot?@RequestParam注解無法讀取application/json格式數據問題
RequestParam用于將指定的請求參數賦值給方法中的形參,可以接受簡單類型屬性,也可以接受對象類型,一般用于GET請求,下面這篇文章主要給大家介紹了關于Spring/SpringBoot?@RequestParam注解無法讀取application/json格式數據問題解決的相關資料,需要的朋友可以參考下2022-10-10
mybatis如何在一個update標簽中寫多條update語句
這篇文章主要介紹了mybatis如何在一個update標簽中寫多條update語句問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Java多線程之ReentrantReadWriteLock源碼解析
這篇文章主要介紹了Java多線程之ReentrantReadWriteLock源碼解析,文中有非常詳細的代碼示例,對正在學習java基礎的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05

