shiro整合swagger的注意事項(xiàng)
swagger是一個(gè)很好的rest api管理工具,最近又整合了基于shiro的權(quán)限控制,出問題了,http://localhost:8080/swagger-ui.html訪問不正常,問題肯定是shiro沒放行導(dǎo)致的
shiro 配置
在shiroFilter中的配置如下:
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//Shiro的核心安全接口,這個(gè)屬性是必須的
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String, Filter> filterMap = new LinkedHashMap<>();
filterMap.put("authc", new AjaxPermissionsAuthorizationFilter());
shiroFilterFactoryBean.setFilters(filterMap);
/*定義shiro過濾鏈 Map結(jié)構(gòu)
* Map中key(xml中是指value值)的第一個(gè)'/'代表的路徑是相對于HttpServletRequest.getContextPath()的值來的
* anon:它對應(yīng)的過濾器里面是空的,什么都沒做,這里.do和.jsp后面的*表示參數(shù),比方說login.jsp?main這種
* authc:該過濾器下的頁面必須驗(yàn)證后才能訪問,它是Shiro內(nèi)置的一個(gè)攔截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter
*/
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
/* 過濾鏈定義,從上向下順序執(zhí)行,一般將 / ** 放在最為下邊:這是一個(gè)坑呢,一不小心代碼就不好使了;
authc:所有url都必須認(rèn)證通過才可以訪問; anon:所有url都都可以匿名訪問 */
filterChainDefinitionMap.put("/", "anon");
filterChainDefinitionMap.put("/static/**", "anon");
filterChainDefinitionMap.put("/login/auth", "anon");
filterChainDefinitionMap.put("/login/logout", "anon");
filterChainDefinitionMap.put("/error", "anon");
filterChainDefinitionMap.put("/**", "authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
首先 常規(guī)的過濾放行如下:
filterChainDefinitionMap.put("/swagger-ui.html", "anon");
filterChainDefinitionMap.put("/swagger-resources", "anon");
filterChainDefinitionMap.put("/v2/api-docs", "anon");
filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon");
重新打開shiro,運(yùn)行,swagger2頁面訪問正常,但是程序日志輸出依然有權(quán)限訪問出錯(cuò)
于是繼續(xù)排查,受限請求如下:
http://localhost:8080/configuration/security http://localhost:8080/configuration/ui
所以繼續(xù)添加放行:
filterChainDefinitionMap.put("/configuration/security", "anon");
filterChainDefinitionMap.put("/configuration/ui", "anon");
最終配置為:
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//Shiro的核心安全接口,這個(gè)屬性是必須的
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String, Filter> filterMap = new LinkedHashMap<>();
filterMap.put("authc", new AjaxPermissionsAuthorizationFilter());
shiroFilterFactoryBean.setFilters(filterMap);
/*定義shiro過濾鏈 Map結(jié)構(gòu)
* Map中key(xml中是指value值)的第一個(gè)'/'代表的路徑是相對于HttpServletRequest.getContextPath()的值來的
* anon:它對應(yīng)的過濾器里面是空的,什么都沒做,這里.do和.jsp后面的*表示參數(shù),比方說login.jsp?main這種
* authc:該過濾器下的頁面必須驗(yàn)證后才能訪問,它是Shiro內(nèi)置的一個(gè)攔截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter
*/
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
/* 過濾鏈定義,從上向下順序執(zhí)行,一般將 / ** 放在最為下邊:這是一個(gè)坑呢,一不小心代碼就不好使了;
authc:所有url都必須認(rèn)證通過才可以訪問; anon:所有url都都可以匿名訪問 */
filterChainDefinitionMap.put("/", "anon");
filterChainDefinitionMap.put("/static/**", "anon");
filterChainDefinitionMap.put("/login/auth", "anon");
filterChainDefinitionMap.put("/login/logout", "anon");
filterChainDefinitionMap.put("/error", "anon");
//swagger放行
filterChainDefinitionMap.put("/swagger-ui.html", "anon");
filterChainDefinitionMap.put("/swagger-resources", "anon");
filterChainDefinitionMap.put("/v2/api-docs", "anon");
filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon");
filterChainDefinitionMap.put("/configuration/security", "anon");
filterChainDefinitionMap.put("/configuration/ui", "anon");
filterChainDefinitionMap.put("/**", "authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
注意:filterChainDefinitionMap.put("/**", "authc") 需要放置在最后面。
最終結(jié)果: 訪問http://localhost:8080/api/swagger-ui.html出現(xiàn)

以上就是shiro整合swagger需要注意的地方的詳細(xì)內(nèi)容,更多關(guān)于shiro整合swagger的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java實(shí)現(xiàn)一個(gè)接口調(diào)取另一個(gè)接口(接口一調(diào)取接口二)
這篇文章主要介紹了java實(shí)現(xiàn)一個(gè)接口調(diào)取另一個(gè)接口(接口一調(diào)取接口二),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
MyBatis Plus實(shí)現(xiàn)一對多的查詢場景的三種方法
MyBatis Plus提供了多種簡便的方式來進(jìn)行一對多子查詢,本文主要介紹了MyBatis Plus實(shí)現(xiàn)一對多的查詢場景的三種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級應(yīng)用(附源碼)
這篇文章主要介紹了最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級應(yīng)用(附源碼),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
基于RecyclerChart的KLine的繪制Scale詳解
這篇文章主要為大家詳細(xì)介紹了基于RecyclerChart實(shí)現(xiàn)KLine繪制Scale的相關(guān)資料,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
Java中雙重檢查鎖(double checked locking)的正確實(shí)現(xiàn)
雙重檢查鎖(Double-Check Locking),顧名思義,通過兩次檢查,并基于加鎖機(jī)制,實(shí)現(xiàn)某個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于Java中雙重檢查鎖(double checked locking)的相關(guān)資料,需要的朋友可以參考下2021-09-09
springboot掃碼登錄的簡單實(shí)現(xiàn)
本文主要介紹基于SpringBoot + Vue + Android實(shí)現(xiàn)的掃碼登錄,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Java調(diào)用Deepseek實(shí)現(xiàn)項(xiàng)目代碼審查
這篇文章主要為大家詳細(xì)介紹了Java如何調(diào)用Deepseek實(shí)現(xiàn)項(xiàng)目代碼審查功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02

