身份验证,即在应用中谁能证明他就是他本人。一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明。
在shiro中,用户需要提供principals (身份)和credentials(证明)给shiro,从而应用能验证用户身份:
principals:身份,即主体的标识属性,可以是任何东西,如用户名、邮箱等,唯一即可。一个主体可以有多个principals,但只有一个Primary principals,一般是用户名/密码/手机号。
credentials:证明/凭证,即只有主体知道的安全值,如密码/数字证书等。
最常见的principals和credentials组合就是用户名/密码了。接下来先进行一个基本的身份认证。
另外两个相关的概念是之前提到的Subject及Realm,分别是主体及验证主体的数据源。
登录/退出
1.shiro.ini
2.
1 public class ShiroTest { 2 3 @Test 4 public void testHelloworld() { 5 // 1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager 6 Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory( 7 "classpath:shiro.ini"); 8 9 // 2、得到SecurityManager实例 并绑定给SecurityUtils 10 org.apache.shiro.mgt.SecurityManager securityManager = factory 11 .getInstance(); 12 SecurityUtils.setSecurityManager(securityManager); 13 14 // 3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证) 15 Subject subject = SecurityUtils.getSubject(); 16 UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123"); 17 18 try { 19 // 4、登录,即身份验证 20 subject.login(token); 21 } catch (AuthenticationException e) { 22 // 5、身份验证失败 23 } 24 25 Assert.assertEquals(true, subject.isAuthenticated()); // 断言用户已经登录 26 27 // 6、退出 28 subject.logout(); 29 }
2.4 身份认证流程
2.5 Realm
Realm:域,Shiro从从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager要验证用户身份,那么它需要 从Realm获取相应的用户进行比较以确定用户身份是否合法;也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;可以把Realm 看成DataSource,即安全数据源。如我们之前的ini配置方式将使用 org.apache.shiro.realm.text.IniRealm。
单Realm配置
1、自定义Realm实现(com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1):
1 package com.test.shiro; 2 3 import org.apache.shiro.authc.AuthenticationException; 4 import org.apache.shiro.authc.AuthenticationInfo; 5 import org.apache.shiro.authc.AuthenticationToken; 6 import org.apache.shiro.authc.IncorrectCredentialsException; 7 import org.apache.shiro.authc.SimpleAuthenticationInfo; 8 import org.apache.shiro.authc.UnknownAccountException; 9 import org.apache.shiro.authc.UsernamePasswordToken; 10 import org.apache.shiro.realm.Realm; 11 12 public class MyRealm1 implements Realm{ 13 14 @Override 15 public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) 16 throws AuthenticationException { 17 18 19 String username = (String)token.getPrincipal(); 20 String password = new String((char[])token.getCredentials()); 21 if(!"zhang".equals(username)){ 22 throw new UnknownAccountException(); 23 } 24 if(!"123".equals(password)){ 25 throw new IncorrectCredentialsException(); //如果 26 } 27 28 return new SimpleAuthenticationInfo(username, password, getName()); 29 } 30 31 @Override 32 public String getName() { 33 return "myRealm1"; 34 } 35 36 @Override 37 public boolean supports(AuthenticationToken token) { 38 return token instanceof UsernamePasswordToken; 39 } 40 41 }
2.shiro-relm.ini
3.
1 package com.test.shiro; 2 3 import org.apache.shiro.SecurityUtils; 4 import org.apache.shiro.authc.AuthenticationException; 5 import org.apache.shiro.authc.UsernamePasswordToken; 6 import org.apache.shiro.config.IniSecurityManagerFactory; 7 import org.apache.shiro.mgt.SecurityManager; 8 import org.apache.shiro.subject.Subject; 9 import org.apache.shiro.util.Factory; 10 import org.junit.Assert; 11 import org.junit.Test; 12 13 public class ShiroTest { 14 15 16 17 @Test 18 public void testCustomRealm() { 19 //1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager 20 Factory<org.apache.shiro.mgt.SecurityManager> factory = 21 new IniSecurityManagerFactory("classpath:shiro-realm.ini"); 22 23 //2、得到SecurityManager实例 并绑定给SecurityUtils 24 org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); 25 SecurityUtils.setSecurityManager(securityManager); 26 27 //3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证) 28 Subject subject = SecurityUtils.getSubject(); 29 UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123"); 30 31 try { 32 //4、登录,即身份验证 33 subject.login(token); 34 } catch (AuthenticationException e) { 35 //5、身份验证失败 36 e.printStackTrace(); 37 } 38 39 Assert.assertEquals(true, subject.isAuthenticated()); //断言用户已经登录 40 41 //6、退出 42 subject.logout(); 43 } 44 }