Apache shiro集群实现 (三)shiro身份认证(Shiro Authentication)

 

 

Apache shiro集群实现 (一) shiro入门介绍

Apache shiro集群实现 (二) shiro 的INI配置

Apache shiro集群实现 (三)shiro身份认证(Shiro Authentication)

Apache shiro集群实现 (四)shiro授权(Authentication)--访问控制

Apache shiro集群实现 (五)分布式集群系统下的高可用session解决方案

Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享

Apache shiro集群实现(七)分布式集群系统下---cache共享

Apache shiro集群实现 (八) web集群时session同步的3种方法

 

 

 

一、术语介绍

Authentication:身份认证,即用户提供一些信息来证明自己的身份。如用户名和密码,licence等。

Principals :主体的“标识属性”,可以是任意标识,例如用户名,身份证号码,手机号码等。Principals
可以有多个,但是必须有一个主要的Principal(Primary Principal),这个标识,必须是唯一的。

Credentials:凭据,即只有主体知道或具有的秘密值,例如密码或数字证书,或者某些生物特征,例如指纹,视网膜等。

Principals/ Credentials的配对,他最常见的例子是用户名/密码。

 

二、身份认证(Authenticating Subjects)

 

身份认证可概括为3个步骤。

 

  1. 收集用户提供的身份标识和凭据。<li提交用户的身份标识 凭据信息进行身份验证。<="" li="">
  2. 验证通过允许访问,不通过则要求进行重试或者阻止登录。

代码实现。

 

1.收集用户提供的身份标识和凭据

 

 
  1. <span style="font-size:18px;"><span style="font-size:18px;">//使用最常见的用户名密码的方式

  2. UsernamePasswordToken token = new UsernamePasswordToken(username, password);

  3. //记住我

  4. token.setRememberMe(true);</span></span>

 

在这个例子中,我们使用UsernamePasswordToken,支持最常见的用户名/口令的认证方式。它是org.apache.shiro.authc.AuthenticationToken
接口的一个实现。

值得注意的是,Shiro并不在乎你是如何获得这些信息的(上面例子中的username,和password):这两个参数可能是用户通过http请求提交过来的,也可能是其他的方式获得的。

 

2.提交用户的身份标识/凭据信息进行身份验证。

 

在身份标识和凭据被收集并存入UsernamePasswordToken实例中,我们需要提交Token给Shiro去尝试执行身份认证。

 
  1. <span style="font-size:18px;"><span style="font-size:18px;">//获取Subject对象

  2. Subject currentUser = SecurityUtils.getSubject();

  3. //传入上一步骤创建的token对象,登录,即进行身份验证操作。

  4. currentUser.login(token);</span></span>

 

3.验证成功或失败。

 

我们可以通过subject.isAuthenticated()来判断是否验证成功,验证成功返回true,否则返回false。

如果登录失败,例如用户名或者密码错误,或者请求次数过多。我们可以根据Shiro提供的丰富的运行时错误,来来判断是由于什么引起的错误。

 

 
  1. <span style="font-size:18px;"><span style="font-size:18px;">try {

  2. currentUser.login(token);

  3. } catch ( UnknownAccountException uae ) { //用户名未知...

  4. } catch ( IncorrectCredentialsException ice ) {//凭据不正确,例如密码不正确 ...

  5. } catch ( LockedAccountException lae ) { //用户被锁定,例如管理员把某个用户禁用...

  6. } catch ( ExcessiveAttemptsException eae ) {//尝试认证次数多余系统指定次数 ...

  7. } catch ( AuthenticationException ae ) {

  8. //其他未指定异常

  9. }

  10. //未抛出异常,程序正常向下执行。</span></span>

 

三、已记住和已认证(Remembered vs. Authenticated)

 

如上例所示,shiro支持在登录过程中执行”remember me”,在此值得指出,一个已记住的Subject(remembered
Subject)和一个正常通过认证的Subject(authenticated Subject)在shiro是完全不同的。

 

Remembered(已记住)

       一个 remembered 主体,不是匿名的,有一个已知的ID(identity)(subject.getPrincipals()不为空)。而这个记住的过程,发生在之前的身份认证过程中。可以通过subject.isRemembered()来判断是已记住状态,已记住会返true。
   

 Authenticated(已认证)

       一个已认证的Subject是指在当前会话(session)中被成功地验证过了。即在认证过程没有抛出异常,如果是已认证的主体,subject.isAuthenticated()返回true。
    

互斥(Mutually Exclusive)
        以记住和已认证是互斥的,也就对于他们的判断(subject.isRemembered()和subject.isAuthenticated()),一个返回true,则另一个返回false。


为什么有这样的区别


       “身份验证”这个词有很强的证明的意思在里面。也就是说,有一个预期保证Subject 已经证明他们是他们所说的谁。当用户在之前的交互中被程序记住,认证将不复存在。根据已记住的ID(identity)系统可以知道用户很有可能是谁,但在现实中没有办法绝对保证被记住的Subject代表期望的用户。而当这个用户已认证(Authenticated)则这个用户不再被视为已记住的,因为在当前会话(Session)中已得到认证。
        对于高度机密或者重要的系统,例如财务相关的系统,要基于isAuthenticated()来判断,而不要基于isRemembered()来判断,以保证一个预期和核实的身份。这是为了保证安全性。

 

四、退出登录

认证相反的操作是释放所有已知的识别状态。当主体完成与应用程序交互,你可以调用subject.logout()放弃所有识别信息,即退出登录状态。

 

 
  1. <span style="font-size:18px;"><span style="font-size:18px;">currentUser.logout();

  2. //removes all identifying information and invalidates their session too.</span></span>

 

注:调用了注销的方法,用户的session将会被作废,同时,在web系统中,rememberMe的cookie将会被删除。

因为在Web程序中记住身份信息往往使用Cookies,而Cookies只能在Response提交时才能被删除,所以强烈要求在为最终用户调用subject.logout()之后立即将用户引导到一个新页面,确保任何与安全相关的Cookies如期删除,这是Http本身Cookies功能的限制而不是Shiro的限制。

 

五、身份认证过程(Authentication Sequence)

shiro认证流程

 

shiro认证流程

  1. 应用程序根据用户的身份和凭证(principals和credentials)来构造出AuthenticationToken实例,并调用Subject.login的方法进行登录,其会自动委托给Security
    Manager。
  2. Subject实例通常上都是DelegatingSubject
    (或子类),在验证开始的时候,Subject实例会将验证委托给应用程序配置的SecurityManager,并调用securityManager.login(token)方法进行身份认证。
  3. SecurityManager得到token信息后,通过调用authenticator.authenticate(token)方法,把身份验证委托给内置的Authenticator的实例进行验证。authenticator通常是ModularRealmAuthenticator
    实例,支持对一个或多个Realm实例进行适配。ModularRealmAuthenticator提供了一种可插拔的认证风格,你可以在此处插入自定义Realm实现。
  4. 如果配置了多个Realm,ModularRealmAuthenticator会根据配置的AuthenticationStrategy(身份验证策略)进行多Realm认证过程。
    注:如果应用程序中仅配置了一个Realm,Realm将被直接调用而无需再配置认证策略。
  5. 判断每个Realm是否支持提交的token,如果支持Realm就会调用getAuthenticationInfo(token)方法进行认证处理。

Authenticator

默认实现ModularRealmAuthenticator,支持单Realm和多Realm,如果是单Realm会直接调用Realm进行认证,如果配置了多个Realm需要根据认证策略,按照realms指定的顺序进行身份认证。

 

自定义Authenticator实现

 
  1. <span style="font-size:18px;"><span style="font-size:18px;">[main]

  2. authenticator = com.foo.bar.CustomAuthenticator

  3. securityManager.authenticator = $authenticator</span></span>

 

AuthenticationStrategy认证策略

如果是多个Realm,ModularRealmAuthenticator将会根据认证策略来确定认证是否成功。

例如,如果只有一个Realm验证成功,而其他Realm验证失败,那么这次认证是否成功呢?如果大多数的Realm验证成功了,认证是否就认为成功呢?或者,一个Realm验证成功后,是否还需要判断其他Realm的结果?认证策略就是根据应用程序的需要对这些问题作出决断。

AuthenticationStrategy 是个无状态的组件,在认证过程中会进行4次调用。

 

  1. 在所有Realm被调用之前
  2. 在调用Realm的getAuthenticationInfo
    方法之前
  3. 在调用Realm的getAuthenticationInfo 方法之后
  4. 在所有Realm被调用之后

认证策略的另外一项工作就是聚合所有Realm的结果信息封装至一个AuthenticationInfo实例中,并将此信息返回,以此作为Subject的身份信息。   

 

Shiro有3中认证策略的具体实现:

AuthenticationStrategy类 描述 
AtLeastOneSuccessfulStrategy只要一个或者多个Realm认证通过,则整体身份认证就会视为成功。
FirstSuccessfulStrategy只有第一个验证通过,才会视为整体认证通过。其他的会被忽略。
AllSuccessfulStrategy只有所有的Realm认证成功,才会被视为认证通过。

 

ModularRealmAuthenticator 某人使用AtLeastOneSuccessfulStrategy 策略,你也可以换成其他的认证策略,ini配置: 

 

 
  1. <span style="font-size:18px;"><span style="font-size:18px;">[main]

  2. ...

  3. authcStrategy = org.apache.shiro.authc.pam.FirstSuccessfulStrategy

  4. securityManager.authenticator.authenticationStrategy = $authcStrategy

  5. ...</span></span>

 

自定义策略:继承org.apache.shiro.authc.pam.AbstractAuthenticationStrategy。

Realm的顺序 

Realm顺序对认证是有影响的。

 

默认顺序是按照定义的顺序,例如ini文件 中这样配置:

 

 
  1. <span style="font-size:18px;"><span style="font-size:18px;">blahRealm = com.company.blah.Realm

  2. ...

  3. fooRealm = com.company.foo.Realm

  4. ...

  5. barRealm = com.company.another.Realm</span></span>

 

那么将会按照 blahRealm ,fooRealm ,barRealm 的顺序依次调用。

 

显示指定排序

 
  1. <span style="font-size:18px;"><span style="font-size:18px;">blahRealm = com.company.blah.Realm

  2. ...

  3. fooRealm = com.company.foo.Realm

  4. ...

  5. barRealm = com.company.another.Realm

  6. #显示指定顺序

  7. securityManager.realms = $fooRealm, $barRealm, $blahRealm

  8. ...</span></span>

 

六、完整的例子

1.在ini文件中配置用户信息。

 

新建maven项目,pom文件如下。

 

 
  1. <span style="font-size:18px;"><span style="font-size:18px;"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3. <modelVersion>4.0.0</modelVersion>

  4. <groupId>com.api6.shiro</groupId>

  5. <artifactId>demo1</artifactId>

  6. <version>0.0.1-SNAPSHOT</version>

  7. <packaging>jar</packaging>

  8. <name>demo1</name>

  9. <url>http://maven.apache.org</url>

  10. <properties>

  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  12. </properties>

  13. <dependencies>

  14. <dependency>

  15. <groupId>junit</groupId>

  16. <artifactId>junit</artifactId>

  17. <version>4.12</version>

  18. <scope>test</scope>

  19. </dependency>

  20. <dependency>

  21. <groupId>commons-logging</groupId>

  22. <artifactId>commons-logging</artifactId>

  23. <version>1.1.3</version>

  24. </dependency>

  25. <dependency>

  26. <groupId>org.apache.shiro</groupId>

  27. <artifactId>shiro-core</artifactId>

  28. <version>1.2.3</version>

  29. </dependency>

  30. </dependencies>

  31. </project></span></span>

 

在src/test/resource下新建shiro.ini文件文件配置为:

 

 
  1. <span style="font-size:18px;">[users]

  2. zhao=111

  3. wang=111</span>

 

在src/test/java下新建ShiroSimpleTest。

 

 
  1. <span style="font-size:18px;">package com.api6.shiro.demo1;

  2. import org.apache.commons.logging.Log;

  3. import org.apache.commons.logging.LogFactory;

  4. import org.apache.shiro.SecurityUtils;

  5. import org.apache.shiro.authc.AuthenticationException;

  6. import org.apache.shiro.authc.ExcessiveAttemptsException;

  7. import org.apache.shiro.authc.IncorrectCredentialsException;

  8. import org.apache.shiro.authc.LockedAccountException;

  9. import org.apache.shiro.authc.UnknownAccountException;

  10. import org.apache.shiro.authc.UsernamePasswordToken;

  11. import org.apache.shiro.config.IniSecurityManagerFactory;

  12. import org.apache.shiro.mgt.SecurityManager;

  13. import org.apache.shiro.subject.Subject;

  14. import org.apache.shiro.util.Factory;

  15. import org.junit.Assert;

  16. import org.junit.Test;

  17. public class ShiroSimpleTest {

  18. private static Log log = LogFactory.getLog(ShiroSimpleTest.class);

  19. @Test

  20. public void testLogin() {

  21. //1.获取SecurityManager工厂,加载shiro.Ini配置文件初始化SecurityManager

  22. Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");

  23. //2.获取SecurityManager实例

  24. SecurityManager securityManager = factory.getInstance();

  25. //3.将SecurityManager实例,绑定到SecurityUtils。

  26. SecurityUtils.setSecurityManager(securityManager);

  27.  
  28. //4.使用最常见的用户名密码的方式,创建token

  29. UsernamePasswordToken token = new UsernamePasswordToken("zhao", "111");

  30. //5.设置记住我

  31. token.setRememberMe(true);

  32. //6.获取Subject对象

  33. Subject currentUser = SecurityUtils.getSubject();

  34.  
  35. try {

  36. //7.传入上一步骤创建的token对象,登录,即进行身份验证操作。

  37. currentUser.login(token);

  38. } catch ( UnknownAccountException uae ) {

  39. //用户名未知...

  40. log.info("用户不存在");

  41. } catch ( IncorrectCredentialsException ice ) {

  42. //凭据不正确,例如密码不正确 ...

  43. log.info("密码不正确");

  44. } catch ( LockedAccountException lae ) {

  45. //用户被锁定,例如管理员把某个用户禁用...

  46. log.info("用户被禁用");

  47. } catch ( ExcessiveAttemptsException eae ) {

  48. //尝试认证次数多余系统指定次数 ...

  49. log.info("请求次数过多,用户被锁定");

  50. } catch ( AuthenticationException ae ) {

  51. //其他未指定异常

  52. log.info("未知错误,无法完成登录");

  53. }

  54. //未抛出异常,程序正常向下执行。

  55. Assert.assertEquals(true, currentUser.isAuthenticated());

  56. }

  57. }</span>


 

执行junit单元测试,查看执行情况。

2.多Realm认证。自定义两个Realm,并指定认证策略。

Realm1

 

 
  1. <span style="font-size:18px;">package com.api6.shiro.demo1.Realm;

  2. import java.util.ArrayList;

  3. import java.util.HashMap;

  4. import java.util.List;

  5. import java.util.Map;

  6. import org.apache.shiro.authc.AccountException;

  7. import org.apache.shiro.authc.AuthenticationException;

  8. import org.apache.shiro.authc.AuthenticationInfo;

  9. import org.apache.shiro.authc.AuthenticationToken;

  10. import org.apache.shiro.authc.SimpleAuthenticationInfo;

  11. import org.apache.shiro.authc.UnknownAccountException;

  12. import org.apache.shiro.authc.UsernamePasswordToken;

  13. import org.apache.shiro.realm.AuthenticatingRealm;

  14. public class UserRealm1 extends AuthenticatingRealm {

  15. @Override

  16. protected AuthenticationInfo doGetAuthenticationInfo(

  17. AuthenticationToken token) throws AuthenticationException {

  18. UsernamePasswordToken upToken = (UsernamePasswordToken) token;

  19. String username = upToken.getUsername();

  20. String password = "";

  21. //创建一个用户list存放所有用户信息

  22. List<Map<String,String>> userList = new ArrayList<Map<String,String>>();

  23. //用户1

  24. Map<String,String> user1 = new HashMap<String, String>();

  25. user1.put("username", "zhao");

  26. user1.put("password", "111");

  27. //用户2

  28. Map<String,String> user2 = new HashMap<String, String>();

  29. user2.put("username", "zhang");

  30. user2.put("password", "222");

  31. userList.add(user1);

  32. userList.add(user2);

  33.  
  34. if (username == null) {

  35. throw new AccountException("Null usernames are not allowed by this realm.");

  36. }

  37.  
  38. //遍历所有用户查询是否存在请求认证的用户,如果存在获取正确的密码

  39. boolean flag=false;

  40. for(Map<String,String> user:userList){

  41. if(username.equals(user.get("username"))){

  42. password = user.get("password");

  43. flag=true;

  44. break;

  45. }

  46. }

  47.  
  48. if(!flag){

  49. throw new UnknownAccountException("没有找到用户 [" + username + "]");

  50. }

  51. //将正确的用户信息,请求登录用户的用户名和正确的密码,创建AuthenticationInfo对象并返回

  52. SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,password,getName());

  53. return info ;

  54. }

  55. }</span>


Realm2

 

 
  1. <span style="font-size:18px;">package com.api6.shiro.demo1.Realm;

  2. import java.util.ArrayList;

  3. import java.util.HashMap;

  4. import java.util.List;

  5. import java.util.Map;

  6. import org.apache.shiro.authc.AccountException;

  7. import org.apache.shiro.authc.AuthenticationException;

  8. import org.apache.shiro.authc.AuthenticationInfo;

  9. import org.apache.shiro.authc.AuthenticationToken;

  10. import org.apache.shiro.authc.SimpleAuthenticationInfo;

  11. import org.apache.shiro.authc.UnknownAccountException;

  12. import org.apache.shiro.authc.UsernamePasswordToken;

  13. import org.apache.shiro.realm.AuthenticatingRealm;

  14. public class UserRealm2 extends AuthenticatingRealm {

  15. @Override

  16. protected AuthenticationInfo doGetAuthenticationInfo(

  17. AuthenticationToken token) throws AuthenticationException {

  18. UsernamePasswordToken upToken = (UsernamePasswordToken) token;

  19. String username = upToken.getUsername();

  20. String password = "";

  21. //创建一个用户list存放所有用户信息

  22. List<Map<String,String>> userList = new ArrayList<Map<String,String>>();

  23. //用户1

  24. Map<String,String> user1 = new HashMap<String, String>();

  25. user1.put("username", "zhao");

  26. user1.put("password", "123");

  27. //用户2

  28. Map<String,String> user2 = new HashMap<String, String>();

  29. user2.put("username", "zhang");

  30. user2.put("password", "123");

  31. userList.add(user1);

  32. userList.add(user2);

  33.  
  34. boolean flag=false;

  35. for(Map<String,String> user:userList){

  36. if(username.equals(user.get("username"))){

  37. password = user.get("password");

  38. flag=true;

  39. break;

  40. }

  41. }

  42.  
  43. if(!flag){

  44. throw new UnknownAccountException("没有找到用户 [" + username + "]");

  45. }

  46.  
  47. return new SimpleAuthenticationInfo(username,password,getName());

  48. }

  49. }</span>


 

测试代码不变,修改shiro.ini文件如下:

 


 

 
  1. <span style="font-size:18px;"><span style="font-size:18px;">[main]

  2. #自定义realm

  3. myRealm1 = com.api6.shiro.demo1.Realm.UserRealm1

  4. myRealm2 = com.api6.shiro.demo1.Realm.UserRealm2

  5. #指定realm的顺序

  6. securityManager.realms = $myRealm1,$myRealm2

  7. #策略

  8. #authcStrategy = org.apache.shiro.authc.pam.FirstSuccessfulStrategy

  9. #authcStrategy = org.apache.shiro.authc.pam.AllSuccessfulStrategy

  10. authcStrategy = org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy

  11. securityManager.authenticator.authenticationStrategy = $authcStrategy</span></span>


执行junit单元测试,查看执行情况。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值