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)文章
java配置dbcp連接池(數(shù)據(jù)庫連接池)示例分享
java配置dbcp連接池示例分享,大家參考使用吧2013-12-12Myeclipse部署Tomcat_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章給大家介紹了Myeclipse部署Tomcat的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-07-07Spring/SpringBoot?@RequestParam注解無法讀取application/json格式數(shù)據(jù)問題
RequestParam用于將指定的請求參數(shù)賦值給方法中的形參,可以接受簡單類型屬性,也可以接受對象類型,一般用于GET請求,下面這篇文章主要給大家介紹了關(guān)于Spring/SpringBoot?@RequestParam注解無法讀取application/json格式數(shù)據(jù)問題解決的相關(guān)資料,需要的朋友可以參考下2022-10-10mybatis如何在一個update標(biāo)簽中寫多條update語句
這篇文章主要介紹了mybatis如何在一個update標(biāo)簽中寫多條update語句問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08java學(xué)生信息管理系統(tǒng)MVC架構(gòu)詳解
這篇文章主要為大家詳細(xì)介紹了java學(xué)生信息管理系統(tǒng)MVC架構(gòu)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11Java多線程之ReentrantReadWriteLock源碼解析
這篇文章主要介紹了Java多線程之ReentrantReadWriteLock源碼解析,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05Java令牌Token登錄與退出的實(shí)現(xiàn)
這篇文章主要介紹了Java令牌Token登錄與退出的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05SpringMVC用JsonSerialize日期轉(zhuǎn)換方法
下面小編就為大家?guī)硪黄猄pringMVC用JsonSerialize日期轉(zhuǎn)換方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起 小編過來看看吧2016-11-11