02-自定义Realm-认证(ssm+Shiro)
自定义Realm的原因:
Shiro自带的IniRealm从ini配置文件中读取用户的信息,但在实际开发中需要从系统的数据库中读取用户信息,所以需要自定义realm,
其目的是:改写Shiro的认证方法(默认的Shiro认证方法是从ini文件中获取用户信息的),由程序员编写代码从数据库中获取用户的信息。(这是实际开发中要做的事情)
步骤:
一、编写自定义的Realm,CustomRealm.java
package cn.itcast.shiro.realm; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; // 自定义Realm public class CustomRealm extends AuthorizingRealm { /** * 认证 * token:用户输入的令牌,可以是字符串,也可以是对象 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 从token中获取用户名 String username = (String) token.getPrincipal(); // 根据用户输入的用户名从数据库中查找用户信息(密码),这里只是模拟的静态数据 // 如果查不到就返回null... // 如果查到就返回password String password = "111111"; // 返回认证信息 SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, password, this.getName()); return simpleAuthenticationInfo; } // Realm的名称(自定义),可以不定义 @Override public void setName(String name) { super.setName("customRealm"); } /** * 授权 * principals: */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { return null; } }
二、配置自定义Realm
因为测试类是从配置文件中获取到用户信息的,同理,如果是自定义Realm,也要从配置文件中获取,所以自定义Realm需要在ini文件中配置,注入到SecurityManager
shiro-realm.ini:
[main] # 自定义Realm customRealm=cn.itcast.shiro.realm.CustomRealm # 将自定义的Realm注入到SecurityManager中,这里相当于spring中的注入 securityManager.realms=$customRealm
三、测试类
package cn.itcast.shiro.authentication;
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 {
// 自定义Realm
@Test
public void testCustomRealm() {
// 创建SecurityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini");
// 获取SecurityManager
SecurityManager securityManager = factory.getInstance();
// 将securityManager设置到运行环境中
SecurityUtils.setSecurityManager(securityManager);
// 创建Subject,这个就是从数据库中获取的正确的主体,包含用户名和凭证(如密码、数字签名、指纹等等)
Subject subject = SecurityUtils.getSubject();
// 创建Token,这个token就是用户输入的用户名和密码做成的token令牌,将来要从数据库中获取
UsernamePasswordToken token = new UsernamePasswordToken("Peter", "111111");
// 认证提交,认证不通过时会报异常
try {
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
}
// 认证结果:认证通过放回true,失败返回false
boolean isAuthenticated = subject.isAuthenticated();
// 打印认证结果
System.out.println("认证结果:" + isAuthenticated);
// 退出操作
subject.logout();
// 再次打印认证结果
boolean isAuthenticatedAfterLogout = subject.isAuthenticated();
System.out.println("认证结果:" + isAuthenticatedAfterLogout);
}
}
工程结构:
四、测试结果
如果用户输入的用户名和密码在数据库中找不到匹配值,则报异常
-------------------------------------------------------------
修改以上自定义Realm的代码,模拟从数据库中找到匹配的用户名
运行测试类,报异常: