Spring security 如何開(kāi)放 Swagger 訪問(wèn)權(quán)限
Spring security 開(kāi)放 Swagger 訪問(wèn)權(quán)限
開(kāi)放這四個(gè)目錄
搞定
.antMatchers("/swagger-ui.html").permitAll() .antMatchers("/webjars/**").permitAll() .antMatchers("/v2/**").permitAll() .antMatchers("/swagger-resources/**").permitAll()
spring boot 加入攔截器后swagger不能訪問(wèn)
spring boot 加入攔截器后swagger不能訪問(wèn)問(wèn)題
未加入攔截器時(shí),swagger可以正常訪問(wèn)接口信息,但是加入攔截器之后swagger就不能訪問(wèn)了
原因分析
不能訪問(wèn)的原因的swagger的內(nèi)置接口被攔截器攔下來(lái)了
圖片中可以看到swagger的所有請(qǐng)求的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)上找的資料中大部分只說(shuō)添加這個(gè)
@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ú)數(shù)次的實(shí)驗(yàn)發(fā)現(xiàn)這兩個(gè)方法都需要重寫(xiě),只加任何一個(gè)都無(wú)法生效。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java配置dbcp連接池(數(shù)據(jù)庫(kù)連接池)示例分享
java配置dbcp連接池示例分享,大家參考使用吧2013-12-12Myeclipse部署Tomcat_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章給大家介紹了Myeclipse部署Tomcat的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07Spring/SpringBoot?@RequestParam注解無(wú)法讀取application/json格式數(shù)據(jù)問(wèn)題
RequestParam用于將指定的請(qǐng)求參數(shù)賦值給方法中的形參,可以接受簡(jiǎn)單類(lèi)型屬性,也可以接受對(duì)象類(lèi)型,一般用于GET請(qǐng)求,下面這篇文章主要給大家介紹了關(guān)于Spring/SpringBoot?@RequestParam注解無(wú)法讀取application/json格式數(shù)據(jù)問(wèn)題解決的相關(guān)資料,需要的朋友可以參考下2022-10-10mybatis如何在一個(gè)update標(biāo)簽中寫(xiě)多條update語(yǔ)句
這篇文章主要介紹了mybatis如何在一個(gè)update標(biāo)簽中寫(xiě)多條update語(yǔ)句問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08java學(xué)生信息管理系統(tǒng)MVC架構(gòu)詳解
這篇文章主要為大家詳細(xì)介紹了java學(xué)生信息管理系統(tǒng)MVC架構(gòu)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Java多線程之ReentrantReadWriteLock源碼解析
這篇文章主要介紹了Java多線程之ReentrantReadWriteLock源碼解析,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05Java令牌Token登錄與退出的實(shí)現(xiàn)
這篇文章主要介紹了Java令牌Token登錄與退出的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05SpringMVC用JsonSerialize日期轉(zhuǎn)換方法
下面小編就為大家?guī)?lái)一篇SpringMVC用JsonSerialize日期轉(zhuǎn)換方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起 小編過(guò)來(lái)看看吧2016-11-11