package com.alatus.shirotest;
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.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
public class MyRealm extends AuthenticatingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// 自定义的登录验证方法
// Shiro的login方法底层会调用这个类的认证方法进行认证
// 需要配置让自定义的realm生效,可以在imi文件中配置,也可以在spring的ioc容器配置
// 该方法只是获取进行对比的信息,认证逻辑还是Shiro底层的认证逻辑
// 获取身份信息
String username = authenticationToken.getPrincipal().toString();
System.out.println(username);
// 获取凭证信息
String password = authenticationToken.getCredentials().toString();
System.out.println(password);
// 访问数据库获取存储的信息
// 假设数据库我们找到只有一个叫zhangsan的用户
if(username.equals("zhangsan")){
// 从数据库查询加密后的密文
String pwd = "2c72932f1de524f5a0062c288847f97b";
// 封装到校验逻辑对象
AuthenticationInfo info = new SimpleAuthenticationInfo(
authenticationToken.getPrincipal(),
pwd,
ByteSource.Util.bytes("salt"),
authenticationToken.getPrincipal().toString()
);
// 返回数据
return info;
}
return null;
}
}
package com.alatus.shirotest;
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.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
public class MyRealm extends AuthenticatingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// 自定义的登录验证方法
// Shiro的login方法底层会调用这个类的认证方法进行认证
// 需要配置让自定义的realm生效,可以在imi文件中配置,也可以在spring的ioc容器配置
// 该方法只是获取进行对比的信息,认证逻辑还是Shiro底层的认证逻辑
// 获取身份信息
String username = authenticationToken.getPrincipal().toString();
System.out.println(username);
// 获取凭证信息
String password = authenticationToken.getCredentials().toString();
System.out.println(password);
// 访问数据库获取存储的信息
// 假设数据库我们找到只有一个叫zhangsan的用户
if(username.equals("zhangsan")){
// 从数据库查询加密后的密文
String pwd = "2c72932f1de524f5a0062c288847f97b";
// 封装到校验逻辑对象
AuthenticationInfo info = new SimpleAuthenticationInfo(
authenticationToken.getPrincipal(),
pwd,
ByteSource.Util.bytes("salt"),
authenticationToken.getPrincipal().toString()
);
// 返回数据
return info;
}
return null;
}
}
[users]
zhangsan=2c72932f1de524f5a0062c288847f97b,role1,role2
lisi=123
[roles]
role1=user:insert,user:select
[main]
md5CredentialsMatcher=org.apache.shiro.authc.credential.Md5CredentialsMatcher
md5CredentialsMatcher.hasIterations=3
myrealm=com.alatus.shirotest.MyRealm
myrealm.credentialsMatcher=$md5CredentialsMatcher
securityManager.realms=$myrealm
[users]
zhangsan=2c72932f1de524f5a0062c288847f97b,role1,role2
lisi=123
[roles]
role1=user:insert,user:select
[main]
md5CredentialsMatcher=org.apache.shiro.authc.credential.Md5CredentialsMatcher
md5CredentialsMatcher.hasIterations=3
myrealm=com.alatus.shirotest.MyRealm
myrealm.credentialsMatcher=$md5CredentialsMatcher
securityManager.realms=$myrealm