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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

shiro身份验证测试

發布時間:2023/12/19 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shiro身份验证测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

一、登錄驗證

1、首先在shiro.ini里準備一些用戶身份/憑據,后面這里會使用數據庫代替,如:

[users] [main] #realm jdbcRealm=com.learnging.system.shiro.ShiroRealm securityManager.realm=$jdbcRealm authc = org.apache.shiro.web.filter.authc.FormAuthenticationFilter authc.loginUrl = /[urls] /index = authc /a/logout = logout

2登錄測試

準備知識: (1)當運行Web應用程序時,Shiro將創建一些有用的內置過濾器實例,并且自動的在[main]部分使用。如.ini文件配置,要使用authc,先定義其使用的類,這個類必須實現AuthenticatingFilter。這里也可以使用shiro已經實現好的一些AuthenticationFilter,如PassThruAuthenticationFilter,FormAuthenticationFilter。

(2)使用shiro的.ini文件路徑前一定要加CLASSPATH、URL、FILE前綴,如classpath:shiro.ini;只需把shiro.ini放在resources文件夾下,它就能自動找到shiro文件。

測試用例如下:

package learning_system; import junit.framework.Assert; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.junit.Test;public class LoginLogoutTest {@Testpublic void testHelloworld(){//1、獲取SecurityManager工廠,此處使用Ini配置文件初始化SecurityManagerFactory<org.apache.shiro.mgt.SecurityManager> factory =new IniSecurityManagerFactory("classpath:shiro.ini");//2、得到SecurityManager實例 并綁定給SecurityUtilsorg.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();SecurityUtils.setSecurityManager(securityManager);//3、得到Subject及創建用戶名/密碼身份驗證Token(即用戶身份/憑證)Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");try{//4、登錄,即身份驗證subject.login(token);}catch (AuthenticationException e){//5、身份驗證失敗}Assert.assertEquals(true, subject.isAuthenticated()); //斷言用戶已經登錄//6、退出subject.logout();} }

web項目可以省略以上的如下代碼,web容器會幫你做:

Factory<org.apache.shiro.mgt.SecurityManager> factory =new IniSecurityManagerFactory("classpath:shiro.ini");org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();SecurityUtils.setSecurityManager(securityManager);

轉載于:https://my.oschina.net/u/2427561/blog/1517454

總結

以上是生活随笔為你收集整理的shiro身份验证测试的全部內容,希望文章能夠幫你解決所遇到的問題。

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