shiro第三讲 多Realm的实例

本文介绍如何使用Apache Shiro框架进行权限认证,包括搭建Shiro环境、定义Realm类以及进行用户登录验证等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


第一步:进入eclipse新建一个名为shiro的maven工程

第二步:修改shiro工程的编码为utf-8

第三步:加入pom中的内容
  <dependencies>
    <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.9</version>
 </dependency>
 <dependency>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
     <version>1.1.3</version>
 </dependency>
 <dependency>
     <groupId>org.apache.shiro</groupId>
     <artifactId>shiro-core</artifactId>
     <version>1.2.2</version>
 </dependency>
  </dependencies>

第四步:在src/main/resources新建一个abc目录,再在这个目录下新建一个shiro.ini文件,内容如下:
#声明一个 realm 
myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1 
myRealm2=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm2
#指定 securityManager 的 realms 实现
securityManager.realms=$myRealm1,$myRealm2

第五步:在src/main/java/新建一个shiro包,包中新建一个MyRealm1类,这个类要使用Realm接口,代码如下:
package shiro;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;
public class MyRealm1 implements Realm {
public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)
throws AuthenticationException {
String username = (String)token.getPrincipal();  //得到用户名
String password = new String((char[])token.getCredentials()); //得到密码
if(!"zhang".equals(username)) { 
throw new UnknownAccountException(); //如果用户名错误
if(!"123".equals(password)) { 
throw new IncorrectCredentialsException(); //如果密码错误
//如果身份认证验证成功,返回一个 AuthenticationInfo 实现;
return new SimpleAuthenticationInfo(username, password, getName()); 
}
public String getName() {
return "myrealm1";
}
public boolean supports(AuthenticationToken arg0) {
return arg0 instanceof UsernamePasswordToken;
}

}

第六步:在src/main/java/新建一个shiro包,包中新建一个MyRealm2类,这个类要使用Realm接口,代码如下:
package shiro;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;
public class MyRealm2 implements Realm {
public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)
throws AuthenticationException {
String username = (String)token.getPrincipal();  //得到用户名
String password = new String((char[])token.getCredentials()); //得到密码
if(!"wang".equals(username)) { 
throw new UnknownAccountException(); //如果用户名错误
if(!"123".equals(password)) { 
throw new IncorrectCredentialsException(); //如果密码错误
//如果身份认证验证成功,返回一个 AuthenticationInfo 实现;
return new SimpleAuthenticationInfo(username, password, getName()); 
}
public String getName() {
return "myrealm2";
}
public boolean supports(AuthenticationToken arg0) {
return arg0 instanceof UsernamePasswordToken;
}

}

第七步:在src/test/java下新建一个test包,再在这个包下新建一个MyTest类,类中建一个测试函数testHelloworld(),内容如下:
package test;
import junit.framework.Assert;
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.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;

public class MyTest {
@Test 
public void testHelloworld() {
// 1、获取 SecurityManager 工厂,此处使用 Ini 配置文件初始化 SecurityManager
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:abc/shiro-realm.ini");
// 2、得到 SecurityManager 实例 并绑定给 SecurityUtils
org.apache.shiro.mgt.SecurityManager securityManager = factory
.getInstance();
SecurityUtils.setSecurityManager(securityManager);
// 3、得到 Subject 及创建用户名/密码身份验证 Token(即用户身份/凭证)
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
try {
// 4、登录,即身份验证
subject.login(token);
} catch (AuthenticationException e) {
// 5、身份验证失败
}
Assert.assertEquals(true, subject.isAuthenticated()); // 断言用户已经登录
// 6、退出
subject.logout();
}
}
第八步:Run As 选junit运行。
完毕
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庭博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值