通过查看shiro提供的Realm源码,自定义一个Realm需继承AuthorizingRealm类,重写其两个方法。
public class CustomRealm extends AuthorizingRealm {
{
//设置自定义名
super.setName("Real");
}
//用做授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//从认证信息获取用户名
Object primaryPrincipal = principals.getPrimaryPrincipal();
//通过用户名从数据库中获取角色
String[] roles = getRolesByUser();
//通过用户名从数据库中获取权限信息
String[] permissions = getPermissionsByUser();
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//将找出来的角色信息设置到返回对象中
simpleAuthorizationInfo.setRoles(new HashSet<String>(Arrays.asList(roles)));
//将找出来的权限信息设置到返回对象中
simpleAuthorizationInfo.setStringPermissions(new HashSet<String>(Arrays.asList(permissions)));
return simpleAuthorizationInfo;
}
private String[] getPermissionsByUser() {
String[] permissions = {"招人","delete开除"};//这里模拟数据库中的角色表信息
return permissions;
}
private String[] getRolesByUser() {
String[] roles = {"管理员","admin"};//这里模拟数据库中的角色表信息
return roles;
}
//用作认证
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//通过主题传过来的认证信息中获取用户名
String name = (String)token.getPrincipal();
//通过用户名去数据库获取密码
String password = getPasswordByUser(name);
if (password == null){
return null;
}
SimpleAuthenticationInfo simpleAuthenticationInfo
= new SimpleAuthenticationInfo(name, password, "Real");
//把已经知道的密码盐值设置到返回的认证对象中
simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("dinkon"));
return simpleAuthenticationInfo;
}
private String getPasswordByUser(String name) {
/**
* 这里去数据库获取密码通过传入的用户名
* 这里通过省略,通过MAP模拟
*/
Map<String,String> user = new HashMap<String, String>();
user.put("0000","c013a606260dfaaeff1562248b26e93a");//这就是数据库的数据
return user.get(name);
}
public static void main(String[] args) {
Md5Hash dinkon = new Md5Hash("000000", "dinkon", 10);
System.out.println(dinkon);//模拟注册生成密码
}
}
@Test
public void testAuthtication(){
//创建核心部分的对象(SecurityManager)
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
//创建Realm
CustomRealm customRealm = new CustomRealm();
//创建散列对象
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
matcher.setHashAlgorithmName("md5");//加密方式
matcher.setHashIterations(10);//加密次数
customRealm.setCredentialsMatcher(matcher);//将散列设置到Realm中
defaultSecurityManager.setRealm(customRealm);//将Realm设置到核心中去
//主体提交认证
SecurityUtils.setSecurityManager(defaultSecurityManager);//工具类把核心对象设置到环境中
Subject subject = SecurityUtils.getSubject();//工具类可以获取主体
//认证数据
UsernamePasswordToken token = new UsernamePasswordToken("0000", "000000");
String message = null;
try {
subject.login(token);//提交认证,到这一步还没有指定Realms
}catch (UnknownAccountException e){
message = "账号不存在";
}catch (IncorrectCredentialsException e){
message = "密码错了";
}
if (subject.isAuthenticated() == true){//验证认证是否通过
message = "登陆成功";
}
//subject.checkRole("管理员");//检查角色
//subject.checkPermission("招人");
//subject.logout();//退出登录
// try {
// subject.checkRole("admin0");//检查角色
// subject.checkPermission("杀人");//检查权限
// }catch (UnauthorizedException e){
// System.out.println(e.getLocalizedMessage());
// }
System.out.println(message);
}