Spring security 如何開放 Swagger 訪問權(quán)限
Spring security 開放 Swagger 訪問權(quán)限
開放這四個目錄
搞定
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/webjars/**").permitAll()
.antMatchers("/v2/**").permitAll()
.antMatchers("/swagger-resources/**").permitAll()
spring boot 加入攔截器后swagger不能訪問
spring boot 加入攔截器后swagger不能訪問問題
未加入攔截器時,swagger可以正常訪問接口信息,但是加入攔截器之后swagger就不能訪問了
原因分析
不能訪問的原因的swagger的內(nèi)置接口被攔截器攔下來了

圖片中可以看到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/");
}
}
網(wǎng)上找的資料中大部分只說添加這個
@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/");
}
無數(shù)次的實(shí)驗(yàn)發(fā)現(xiàn)這兩個方法都需要重寫,只加任何一個都無法生效。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項(xiàng)目防止XSS攻擊和sql注入方式
這篇文章主要介紹了springboot項(xiàng)目防止XSS攻擊和sql注入方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
JavaWeb三大組件之監(jiān)聽器Listener詳解
這篇文章主要介紹了JavaWeb三大組件之監(jiān)聽器Listener詳解,在JavaWeb應(yīng)用程序中,Listener監(jiān)聽器是一種機(jī)制,用于監(jiān)聽和響應(yīng)特定的事件,它可以感知并響應(yīng)與應(yīng)用程序相關(guān)的事件,從而執(zhí)行相應(yīng)的邏輯處理,需要的朋友可以參考下2023-10-10
Java使用RandomAccessFile類對文件進(jìn)行讀寫
本篇文章主要介紹了Java使用RandomAccessFile類對文件進(jìn)行讀寫,詳細(xì)的介紹了RandomAccessFile類的使用技巧和實(shí)例應(yīng)用,有興趣的可以了解一下2017-04-04
Java JVM字節(jié)碼指令集總結(jié)整理與介紹
本節(jié)將會著重介紹一下JVM中的指令集、Java是如何跨平臺的、JVM指令集參考手冊等內(nèi)容。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(64)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-09-09

