<dependency>
<groupId>org.apache.shirogroupId>
<artifactId>shiro-coreartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.14version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
dependency>
[users]
xiaohei=123456
xiaohong=123456
/**
* 作用: 测试Shiro认证,使用系统数据源
* 作者:gjx
* 版本:V1.0
*
*/
public class TestShiro {
public static void main(String[] args) {
//创建sessionFactory
IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
//创建安全管理器
SecurityManager securityManager = factory.getInstance();
//将安全管理器设置到工具类中
SecurityUtils.setSecurityManager(securityManager);
//获取主体
Subject subject = SecurityUtils.getSubject();
//创建用户密码令牌
UsernamePasswordToken token = new UsernamePasswordToken("xiaohei1", "123456");
try {
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
}
System.out.println(subject.isAuthenticated());
}
}
/**
* 作用: 自定义数据源
* 作者:gjx
* 版本:V1.0
*/
public class MyRealm extends AuthenticatingRealm {
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken)token;
String username = upToken.getUsername();
if("xiaobai".equals(username)){
return new SimpleAuthenticationInfo("xiaobai","123456",UUID.randomUUID().toString());
}
return null;
}
}
[main]
#自定义realm
myRealm=cn.gjxblog.demo2.MyRealm
#将realm设置到securityManager中
securityManager.realms=$myRealm
/**
* 作用:测试java版Shiro认证,使用自定义数据源
* 作者:gjx
* 版本:V1.0
*/
public class TestShiro2 {
public static void main(String[] args) {
//创建sessionFactory
IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini");
//创建安全管理器
SecurityManager securityManager = factory.getInstance();
//将安全管理器设置到工具类中
SecurityUtils.setSecurityManager(securityManager);
//获取主体
Subject subject = SecurityUtils.getSubject();
//创建用户密码令牌
UsernamePasswordToken token = new UsernamePasswordToken("xiaobai", "1234567");
//认证
try {
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
}
System.out.println(subject.isAuthenticated());
}
}
public class CustomMD5Realm extends AuthenticatingRealm {
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken)token;
String username = upToken.getUsername();
if("xiaohong".equals(username)){
//参数1 用户名;参数2 md5加密后的密码;参数3 盐;参数4 唯一认证信息id
return new SimpleAuthenticationInfo("xiaohong","3d53b73c485f523ef2fe45f2b8dd3c58",ByteSource.Util.bytes("ABCD"),UUID.randomUUID().toString());
}
return null;
}
}
[main]
#定义凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#定义散列算法
credentialsMatcher.hashAlgorithmName=md5
#散列次数
credentialsMatcher.hashIterations=1024
#将凭证匹配器设置到realm中
myRealm=cn.gjxblog.demo3.CustomMD5Realm
myRealm.credentialsMatcher=$credentialsMatcher
#将realm设置到securityManager中
securityManager.realms=$myRealm
public class TestShiro3 {
public static void main(String[] args) {
//创建sessionFactory
IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro-md5Realm.ini");
//创建安全管理器
SecurityManager securityManager = factory.getInstance();
//将安全管理器设置到工具类中
SecurityUtils.setSecurityManager(securityManager);
//获取主体
Subject subject = SecurityUtils.getSubject();
//创建用户密码令牌
UsernamePasswordToken token = new UsernamePasswordToken("xiaohong", "123456");
try {
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
}
System.out.println(subject.isAuthenticated());
}
}
1、创建token令牌,token中有用户提交的认证信息即账号和密码
2、执行subject.login(token),通过调用securityManager的login方法最终由authenticate(token)进行认证,
3、this.authenticator = new ModularRealmAuthenticator();
authenticator的实现ModularRealmAuthenticator调用realm从ini配置文件取用户真实的账号和密码,这里使用的是IniRealm(shiro自带)
4、IniRealm先根据token中的账号去ini中找该账号,如果找不到则给ModularRealmAuthenticator返回null,如果找到则匹配密码,匹配密码成功则认证通过。
public void test4(){
//参数1 密码; 参数2 盐; 参数3 散列次数
Md5Hash md5Hash = new Md5Hash("123456","ABCD",1024);
System.out.println(md5Hash);
}