Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Spring security open Swagger access?

Shulou Source: shulou.com Published: 2022-06-03 17:59:22 10月03日 Update

This article will explain in detail how Spring security opens up access to Swagger. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Spring security opens Swagger access to these four directories

Done.

.antMatch ("/ swagger-ui.html"). PermitAll (). AntMatchers ("/ webjars/**"). PermitAll (). AntMatchers ("/ v2 swagger-resources/** *"). PermitAll (). AntMatchers ("/ swagger-resources/**"). PermitAll () swagger cannot be accessed after adding the interceptor

The problem that swagger cannot be accessed after spring boot is added to the interceptor

When the interceptor is not added, swagger can access the interface information normally, but after adding the interceptor, swagger cannot access it.

Cause analysis

The built-in interface of swagger that cannot be accessed is blocked by the interceptor.

You can see all the requested url information of swagger in the picture, just add them to the interceptor's exclusion list.

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 * interceptor configuration * * / @ Configurationpublic class MyWebAppConfig extends WebMvcConfigurationSupport {@ Bean LoginInterceptor localInterceptor () {return new LoginInterceptor () } @ Override public void addInterceptors (InterceptorRegistry registry) {registry.addInterceptor (localInterceptor ()) .addPathPatterns ("/ * *") .principdePathPatterns ("/ user/login") .acquidePathPa tterns ("/ swagger-resources/**", "/ webjars/**", "/ v2According *", "/ 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/") }} most of the materials found on the Internet only said to add this @ Override public void addInterceptors (InterceptorRegistry registry) {registry.addInterceptor (localInterceptor ()) .addPathPatterns ("/ * *") .origindePathPatterns ("/ user/login") .exc ludePathPatterns ("/ swagger-resources/**", "/ webjars/**", "/ v2 swagger-ui.html/**"). } or just add @ 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/");}

Countless experiments have found that both methods need to be rewritten, and neither of them will work.

This is the end of the article on "how Spring security opens Swagger access". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Interceptor open permission article information reason interface more good practical two content cause analysis picture article method directory knowledge data part Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Shulou Technology Shulou Information MySQL