日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

shiro框架_Shiro安全框架(下)

發布時間:2024/10/6 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shiro框架_Shiro安全框架(下) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們繼續說明這個FormAuthenticationFilter的“城府”。

filter的相關類類圖

它一層層地繼承了很多filter的相關方法。舉例其中的某些方法:

filter類的方法

這些方法都非常的直觀明了,如果需要進行定制,可以將它們進行重寫。

最后 ---進行小測試

這里使用MockMVC進行測試。

首先,先對測試環境進行模擬,模擬出一個類似的Web環境。配置如下的環境:

@Before public void setUp(){mockHttpServletRequest = new MockHttpServletRequest(wac.getServletContext());mockHttpServletResponse = new MockHttpServletResponse();MockHttpSession mockHttpSession = new MockHttpSession(wac.getServletContext());mockHttpServletRequest.setSession(mockHttpSession);SecurityUtils.setSecurityManager(securityManager);mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); }

這部分代碼將環境啟動,自動裝在出一個WebApplicationContext,并配置到MockMVC的Session中去。下面是測試代碼

@Test public void testShiro() throws Exception {System.out.println("---------------Shiro Test---------------");mockMvc.perform(post("/login").param("name","entered-username")//可以修改.param("password","entered-password"))//可以修改.andExpect(status().isOk()).andDo(print()).andReturn().getResponse().getContentAsString(); }

對于密碼錯誤以及賬戶不存在的測試返回如下:

測試結果

這里只進行了簡單的測試,而Forworded URL和Redirected URL可以配置Filter的規則來進行設置。

Shiro自定義過濾器

Shiro的設計是由一條完整的filter責任鏈模式構成的,我們在很多業務場景都需要自定義filter來進行過濾。一般來說,我們都會配置ShiroConfig,把所有的域信息,過濾器組裝起來:

@Configuration public class ShiroConfig{//sessionManager ----已在上文//shiroRealm ----上文的MyRealm //shiroFilter@Bean("shiroFilter")public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();shiroFilterFactoryBean.setSecurityManager(securityManager);shiroFilterFactoryBean.setLoginUrl("/login");shiroFilterFactoryBean.setUnauthorizedUrl("/403.html");Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();Map<String, Filter> filters = new LinkedHashMap<>();filters.put("user",userAuthorizerFilter());shiroFilterFactoryBean.setFilters(filters);filterChainDefinitionMap.put("/**","user");shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);return shiroFilterFactoryBean;} }

這里寫了一個很簡單的配置,如果需要自定義過濾器并且集成到shiro中去,只需要寫個filter,然后設置到市容的filters的Map中。這時候,shiro中就有了自定義的filter,在上述案例里,自定義的filter是userAuthorizerFilter,在shiro中的別名就是user。

但是僅此還不夠,我們還需要將過濾器設置到filterChainDefinitionMap中,這樣自定義過濾器就能夠真正實現。

當然,要注意的是,shiro和其他過濾器加載順序的問題。在配置一系列過濾器時,自定義的過濾器(沒有集成到shiro中去的filter)是最先生效的,其次是集成到shiro中的過濾器,最后是shiro默認的一些過濾器。

總結

Shiro是一個入門比較簡單,易于掌握的安全框架,但是使用起來雖然簡單,但是它的源碼是十分精髓的。本人學習一段時間后,便做了一個小小的總結,如有不足,還請賜教。同時,在今后不斷學習的路上,也會不斷地深入學習該方面的內容,并講心得體會進行分享。

參考

Spring Boot [集成-Shiro]?segmentfault.comSpring boot 中使用Shiro?www.jianshu.com

總結

以上是生活随笔為你收集整理的shiro框架_Shiro安全框架(下)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。