博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结
阅读量:6175 次
发布时间:2019-06-21

本文共 4201 字,大约阅读时间需要 14 分钟。

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

@Configurationpublic 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/

 

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时,后台报错:

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]

 

报错的原因:

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

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login/init],methods=[POST]}" onto public org.springframework.http.ResponseEntitys.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{
[],methods=[PUT]}" onto public org.springframework.http.ResponseEntity

 

 

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

 

http://localhost/swagger-ui.html

请求时的流程:

 

转载地址:http://tjhba.baihongyu.com/

你可能感兴趣的文章
输入输出
查看>>
实用技巧:Google 搜索打不开的解决方法【图文教程◆一劳永逸】
查看>>
用国家简写查找对应的国家名称和所在 洲
查看>>
jstl笔记
查看>>
poj 1509 Glass Beads
查看>>
润乾V4导出TXT时自定义分隔符
查看>>
建立一个github博客
查看>>
[PyJs系列介绍]五、回顾及展望
查看>>
BAT文件语法和技巧(bat文件的编写及使用)
查看>>
基于轻量级高性能的CSS3动画库
查看>>
算法思维题
查看>>
DataReader和DataSet区别
查看>>
java和c++的区别
查看>>
原生js实现图片的3d效果
查看>>
前端 组件化开发/模块化开发
查看>>
python发送邮件
查看>>
JAVA的instanceOf什么时候用啊
查看>>
linux下svn服务器的搭建
查看>>
Ubuntu 安装Sqldeveloper
查看>>
python基础一 ------如何对元组各个元素进行命名
查看>>