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/"); }
无数次的实验发现这两个方法都需要重写,只加任何一个都无法生效。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringMVC中的@RequestMapping注解解析
这篇文章主要介绍了SpringMVC中的@RequestMapping注解解析,SpringMVC使用@RequestMapping注解为控制器指定可以处理哪些 URL 请求,在控制器的类定义及方法定义处都可标注@RequestMapping,需要的朋友可以参考下2023-12-12
最新评论