首页 > 其他分享 >Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结

Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结

时间:2022-12-19 15:03:17浏览次数:74  
标签:web java 4.3 Spring Boot Swagger2 jar RELEASE org

How to configure Spring Security to allow Swagger URL to be accessed without authentication

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
}

}

springfox 2.5及以上,要使用:

@Override
public void configure(WebSecurity web) throws Exception {
//allow Swagger URL to be accessed without authentication
web.ignoring().antMatchers("/v2/api-docs",//swagger api json
"/swagger-resources/configuration/ui",//用来获取支持的动作
"/swagger-resources",//用来获取api-docs的URI
"/swagger-resources/configuration/security",//安全选项
"/swagger-ui.html");
}

​https://springfox.github.io/springfox/docs/current/​

Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结_jar

 

​http://stackoverflow.com/questions/37671125/how-to-configure-spring-security-to-allow-swagger-url-to-be-accessed-without-aut​

遇到的问题:

在浏览器中打开http://localhost:8080/swagger-ui.html时,后台报错:

Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结_java_02

org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:207) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:374) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:314) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:61) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:352) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1131) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:936) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.6.jar:8.5.6]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.6.jar:8.5.6]

 

Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结_jar_03

报错的原因:

Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结_jar_04

体现在SpringMvc的路由中:没有语法路径

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login/init],methods=[POST]}" onto public org.springframework.http.ResponseEntity
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[],methods=[PUT]}" onto public org.springframework.http.ResponseEntity<java.lang.S
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[],methods=[POST]}" onto public org.springframework.http.ResponseEntity<java.lang.
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}"

 

Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结_jar_05

 

更改上面的错误,http://localhost/swagger-ui.html就可正常打开

Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结_java_06

 

​http://localhost/swagger-ui.html​

请求时的流程:

Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结_java_07

 



标签:web,java,4.3,Spring,Boot,Swagger2,jar,RELEASE,org
From: https://blog.51cto.com/u_15147537/5952654

相关文章

  • 使用Ratpack与Spring Boot构建高性能JVM微服务
    在微服务天堂中Ratpack和SpringBoot是天造地设的一对。它们都是以开发者为中心的运行于JVM之上的web框架,侧重于生产率、效率以及轻量级部署。他们在服务程序的开发中带来了......
  • Spring MVC Integration,Spring Security
    ​​ http://docs.spring.io/spring-security/site/docs/4.2.0.RELEASE/reference/htmlsingle/#authorize-requests​​37.5 SpringMVCandCSRFIntegration37.5.1 Aut......
  • JAG Spring Contest 2012 G PLAY in BASIC 题解
    提交链接其实就是个大模拟。首先对输入的串进行处理,把所有的命令分开,并把连续的停顿合并。为了方便,定义一个时间单位为全音符的\(\frac1{128}\),这样所有命令的持续时间都......
  • docker-compose入门以及部署SpringBoot+Vue+Redis+Mysql(前后端分离项目)以若依前后端
    场景若依前后端分离版手把手教你本地搭建环境并运行项目:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662上面在搭建起来前后端分离版的项目后。......
  • Springboot整合Apache Dubbo
    ​​Springboot​​​整合​​ApacheDubbo​​一、通过​​Docker​​​安装​​Zookeeper​​​​docker-compose​​​的​​yaml​​文件version:'3'services:zoo......
  • spring事务
    spring事务Spring事务概述事务是一个不可分割的逻辑工作单元,具备ACID特性,实际工作中可借助Spring进行事务管理,Spring提供了两种事务管理方式,编程式事务和声明式事务,本......
  • Spring Cloud构建微服务架构(二)服务消费者
     NetflixRibbonisanInterProcessCommunication(IPC)cloudlibrary.Ribbonprimarilyprovidesclient-sideloadbalancingalgorithms.Apartfromtheclient-si......
  • Spring CredHub 教程学习
    SpringCredHub提供客户端支持,用于从CloudFoundry​平台中运行的CredHub服务器存储,检索和删除凭据。CredHub提供了一个HTTPAPI,用于安全地存储,生成,检索和删除各种类型的凭......
  • Spring LDAP参考
    SpringLDAP使得构建使用LightweightDirectoryAccess协议的基于Spring的应用程序变得更加容易。本文档的副本可以制作供您自己使用和分发给他人,前提是您不对此类副本......
  • Spring提取@Transactional事务注解的源码解析
    声明:本文是自己在学习spring注解事务处理源代码时所留下的笔记;难免有错误,敬请读者谅解!!!1、事务注解标签<tx:annotation-driven/>2、tx命名空间解析器 事务tx命名空间解析......