Spring security 開(kāi)放 Swagger 訪問(wèn)權(quán)限
開(kāi)放這四個(gè)目錄
搞定
1
2
3
4
|
.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信息,只要把他們加到攔截器的排除列表中即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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è)
1
2
3
4
5
6
7
|
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localInterceptor()) .addPathPatterns( "/**" ) .excludePathPatterns( "/user/login" ) .excludePathPatterns( "/swagger-resources/**" , "/webjars/**" , "/v2/**" , "/swagger-ui.html/**" ); } |
或者只添加
1
2
3
4
5
6
7
|
@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è)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://haoxuanli.blog.csdn.net/article/details/104422027