博客
关于我
Shrio配置多个Realm、SecurityManager认证策略
阅读量:796 次
发布时间:2023-03-22

本文共 3728 字,大约阅读时间需要 12 分钟。

Apache Shiro 认证实践指南

认证方法

authenticate 是 Shiro 认证的核心方法,用于实现认证逻辑。如果认证成功,将返回 AuthenticationInfo,该信息包含身份认证结果及相关凭证;如果认证失败,则会抛出 AuthenticationException

ModularRealmAuthenticator 认证策略

ModularRealmAuthenticator 是一个定义认证策略的接口。开发者在自定义认证策略时,只需继承该类即可。默认使用三种策略之一:

默认策略

  • FirstSuccessfulStrategy:只要有一个 Realm 认证成功即可,返回第一个成功 Realm 的认证信息,其他 Realm 忽略。
  • AtLeastOneSuccessfulStrategy:只要有一个 Realm 认证成功即可,返回所有成功 Realm 的认证信息。
  • AllSuccessfulStrategy:所有 Realm 都必须成功认证,返回所有 Realm 的认证信息。若任意一个 Realm 失败,整体认证失败。

ModularRealmAuthenticator 默认使用 AtLeastOneSuccessfulStrategy 策略。

环境搭建

依赖管理

添加必要的 Shiro 和测试依赖:

org.apache.shiro
shiro-core
1.4.0
junit
junit
4.12

自定义 Realm 实现

MyRealm01

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 - 用户名或密码错误");
}
}
}

MyRealm02

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 - 用户名或密码错误");
}
}
}

测试步骤

实例化 Realm

MyRealm01 myRealm01 = new MyRealm01();
MyRealm02 myRealm02 = new MyRealm02();

实例化 SecurityManager

DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();

设置认证策略

ModularRealmAuthenticator modularRealmAuthenticator = new ModularRealmAuthenticator();
modularRealmAuthenticator.setAuthenticationStrategy(new FirstSuccessfulStrategy());
// 或者
modularRealmAuthenticator.setAuthenticationStrategy(new AllSuccessfulStrategy());
defaultSecurityManager.setAuthenticator(modularRealmAuthenticator);

设置 Realm

Set
realmHashSet = 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/

你可能感兴趣的文章