本文共 3728 字,大约阅读时间需要 12 分钟。
authenticate 是 Shiro 认证的核心方法,用于实现认证逻辑。如果认证成功,将返回 AuthenticationInfo,该信息包含身份认证结果及相关凭证;如果认证失败,则会抛出 AuthenticationException。
ModularRealmAuthenticator 是一个定义认证策略的接口。开发者在自定义认证策略时,只需继承该类即可。默认使用三种策略之一:
ModularRealmAuthenticator 默认使用 AtLeastOneSuccessfulStrategy 策略。
添加必要的 Shiro 和测试依赖:
org.apache.shiro shiro-core 1.4.0 junit junit 4.12
import org.apache.shiro.authc.*;import org.apache.shiro.realm.Realm;public class MyRealm01 implements Realm { private static final String USERNAME = "fury1"; private static final String PASSWORD = "111111"; @Override public String getName() { return "MyRealm01"; } @Override public boolean supports(AuthenticationToken token) { return token instanceof UsernamePasswordToken; } @Override public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = (String) token.getPrincipal(); String password = new String((char[]) token.getCredentials()); if (USERNAME.equals(username) && PASSWORD.equals(password)) { return new SimpleAuthenticationInfo(username, password, getName()); } else { throw new RuntimeException("MyRealm01 - 用户名或密码错误"); } }} import org.apache.shiro.authc.*;import org.apache.shiro.realm.Realm;public class MyRealm02 implements Realm { private static final String USERNAME = "fury2"; private static final String PASSWORD = "222222"; @Override public String getName() { return "MyRealm02"; } @Override public boolean supports(AuthenticationToken token) { return token instanceof UsernamePasswordToken; } @Override public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = (String) token.getPrincipal(); String password = new String((char[]) token.getCredentials()); if (USERNAME.equals(username) && PASSWORD.equals(password)) { return new SimpleAuthenticationInfo(username, password, getName()); } else { throw new RuntimeException("MyRealm02 - 用户名或密码错误"); } }} MyRealm01 myRealm01 = new MyRealm01();MyRealm02 myRealm02 = new MyRealm02();
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
ModularRealmAuthenticator modularRealmAuthenticator = new ModularRealmAuthenticator();modularRealmAuthenticator.setAuthenticationStrategy(new FirstSuccessfulStrategy());// 或者modularRealmAuthenticator.setAuthenticationStrategy(new AllSuccessfulStrategy());defaultSecurityManager.setAuthenticator(modularRealmAuthenticator);
SetrealmHashSet = new HashSet<>();realmHashSet.add(myRealm01);realmHashSet.add(myRealm02);defaultSecurityManager.setRealms(realmHashSet);
SecurityUtils.setSecurityManager(defaultSecurityManager);Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken("fury1", "111111");subject.login(token);System.out.println("认证结果为:" + subject.isAuthenticated());subject.logout();System.out.println("认证结果为:" + subject.isAuthenticated()); 以上步骤将指导您完整地配置并测试一个基于 Shiro 的认证系统。通过合理配置 Realm 和认证策略,您可以根据具体需求灵活扩展认证逻辑。
转载地址:http://vtqfk.baihongyu.com/