Shiro入门5:Shiro认证的HelloWorld
2016-03-23 17:42
441 查看
这是一个基于原生实现的一个Shiro认证的HelloWorld的Test,不与其他Spring等框架整合,但是需要用到Junit,或者可以将下面的测试代码用static void main来运行也是可以的。
这篇文章为了让大家更加快速熟悉Shiro的使用流程,在最后我会给大家发一下我自己的总结。
主要需要构建一个ini配置文件来进行Shiro的使用,配置文件放在src/main/resources中
此文老猫原创,转载请加本文连接:/article/11906593.html
更多有关老猫的文章:http://blog.csdn.net/nthack5730
方便大家复制:【整体流程总结精华在最后】
shiro-first.ini
AuthenticationTest.java
此文老猫原创,转载请加本文连接:/article/11906593.html
更多有关老猫的文章:http://blog.csdn.net/nthack5730
流程总结:
此文老猫原创,转载请加本文连接:/article/11906593.html
更多有关老猫的文章:http://blog.csdn.net/nthack5730
这篇文章为了让大家更加快速熟悉Shiro的使用流程,在最后我会给大家发一下我自己的总结。
主要需要构建一个ini配置文件来进行Shiro的使用,配置文件放在src/main/resources中
//============================================================== // Shiro认证部分 //============================================================== |---认证流程 |---构造Security Manager环境 |---Subject.login()提交认证 |---SecurityManager.login()执行认证 |---Authenticator执行认证 |---Realm根据身份获取验证信息 | |---工程结构 |---shiro.ini:通过此配置文件创建SecurityManager工厂 |---对ini进行配置 #对用户信息进行配置 [users] #用户帐号和密码 zhangsan=11111 lisi=22222 | 此文老猫原创,转载请加本文连接:/article/11906593.html 更多有关老猫的文章:http://blog.csdn.net/nthack5730 |---入门程序: | public class AuthenticationTest { //用户登陆和退出 @Test public void testLoginAndLogout(){ //创建securityManager工厂,通过Ini配置文件创建securityManager工厂 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini"); //创建SecurityManager SecurityManager sm = factory.getInstance(); //将securityManager设置到当前的环境中 SecurityUtils.setSecurityManager(sm); //从SecurityUtils里面创建一个subject Subject subject = SecurityUtils.getSubject(); //在认证提交前,需要准备token(令牌) UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "11111"); try { //执行认证提交 subject.login(token); } catch (AuthenticationException e) { // TODO: handle exception e.printStackTrace(); } //是否认证通过 boolean isAuthenticated = subject.isAuthenticated(); System.out.println("是否认证通过:" + isAuthenticated); //退出操作 subject.logout(); //是否认证通过 isAuthenticated = subject.isAuthenticated(); System.out.println("是否认证通过:" + isAuthenticated); }//method }//class
此文老猫原创,转载请加本文连接:/article/11906593.html
更多有关老猫的文章:http://blog.csdn.net/nthack5730
方便大家复制:【整体流程总结精华在最后】
shiro-first.ini
#对用户信息进行配置 [users] #用户帐号和密码 zhangsan=11111 lisi=22222
AuthenticationTest.java
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.mgt.SecurityManager; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.junit.Test; public class AuthenticationTest { //用户登陆和退出 @Test public void testLoginAndLogout(){ //创建securityManager工厂,通过Ini配置文件创建securityManager工厂 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini"); //创建SecurityManager SecurityManager sm = factory.getInstance(); //将securityManager设置到当前的环境中 SecurityUtils.setSecurityManager(sm); //从SecurityUtils里面创建一个subject Subject subject = SecurityUtils.getSubject(); //在认证提交前,需要准备token(令牌) UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "11111"); try { //执行认证提交 subject.login(token); } catch (AuthenticationException e) { // TODO: handle exception e.printStackTrace(); } //是否认证通过 boolean isAuthenticated = subject.isAuthenticated(); System.out.println("是否认证通过:" + isAuthenticated); //退出操作 subject.logout(); //是否认证通过 isAuthenticated = subject.isAuthenticated(); System.out.println("是否认证通过:" + isAuthenticated); }//method }//class
此文老猫原创,转载请加本文连接:/article/11906593.html
更多有关老猫的文章:http://blog.csdn.net/nthack5730
流程总结:
|---执行流程: |---通过ini配置文件创建SecurityManager |---调用subject.login方法提交认证,提交的就是token |---内部通过SecurityManager进行认证 | |---SecurityManager通过调用Authenticator的实现类最终由ModularRealmAuthenticator进行认证 | |---ModularRealmAuthenticator调用IniRealm【给Realm传入token】去ini配置文件中查询用户信息 | |---IniRealm根据输入的token(UsernamePasswordToken)从Shiro-first.ini查询用户信息,根据帐号查询用户信息(帐号和密码) |---如果IniRealm查询到账号:就给ModularRealmAuthenticator返回用户信息【包括帐号和密码】 |---如果IniRealm帐号找不到,就给ModularRealmAuthenticator返回null | |---ModularRealmAuthenticator接受IniRealm返回的Authentication认证信息 |---如果接受信息是null,说明帐号不存在, ModularRealmAuthenticator抛出异常【最终干活的是Realm】 | |---如果接受认证信息不是null【说明IniRealm找到了用户】, ModularRealmAuthenticator对IniRealm返回用户密码(在ini文件中存在)和token中的密码进行比对 | |---如果不一致,抛出异常 |---小结 |---ModularRealmAuthenticator作用进行认证,需要调用realm查询用户信息(在数据库中存在用户信息) |---ModularRealmAuthenticator进行密码对比(认证过程)。 | |---realm:需要根据token中的用户信息去查询数据库,如果查到用户返回认证信息,如果查不到就返回null
此文老猫原创,转载请加本文连接:/article/11906593.html
更多有关老猫的文章:http://blog.csdn.net/nthack5730
相关文章推荐
- 做一个程序员的优势!
- JS操作DOM常用API总结
- 通过angularjs的directive以及service来实现的列表页加载排序分页
- 搜索技术了解
- js 随机数
- Hashtable源码剖析
- InspirARTion - Sketch & Draw!(InspirARTion—素描和画画!)
- springMVC CRUD模板代码
- Android HAL实例解析
- UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128) [duplicate]
- hdu 1232畅通工程
- HBase shell命令整理
- 经典java笔试题(二)
- 【代码整理】JavaScript的寄生组合式继承
- mysql timestamp 值不合法问题
- pomelo的心跳响应与disconnect
- Android网络编程(一)
- Java语言编程的一些想法
- Cg入门26:Fragment shader –纹理混合动画
- 使用代码编辑器Sublime Text 3进行前端开发及相关快捷键