文章目录
- 自定义Realm
-
- 1.1 在main.java下新建包:com.amoscxy.shiro.realm
- 1.2 新建CustomRealm继承AuthorizingRealm
- 在com.amoscxy.test包中新建测试类:CustomRealmTest:
自定义Realm
1.1 在main.java下新建包:com.amoscxy.shiro.realm
自定义Realm自定义Realm 1.2 新建CustomRealm继承AuthorizingRealm
package com.amoscxy.shiro.realm;
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.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import java.util.*;
/**
* Created by cxy on 2018/9/18.
*/
public class CustomRealm extends AuthorizingRealm {
Map<String,String> userMap = new HashMap<String, String>(16);
{
userMap.put("Mark","123456");
super.setName("customRealm");
}
/**
* 做授权
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//1.从主体传过来的认证信息中,获得用户名
String userName = (String)principalCollection.getPrimaryPrincipal();
//2.从数据库或者缓存中获取角色数据
Set<String> roles = getRolesByUserName(userName);
//从数据库或者缓存中获取权限数据
Set<String> permissions = getPermissionsByUserName(userName);
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setStringPermissions(permissions);
simpleAuthorizationInfo.setRoles(roles);
return simpleAuthorizationInfo;
}
/**
* 做认证
* @param authenticationToken
* @return
* @throws AuthenticationException:主体传过来的认证信息
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//1.从主体传过来的认证信息中,获得用户名
String userName = (String)authenticationToken.getPrincipal();
//2.通过用户名到数据库获取凭证
String password = getPasswordByUserName(userName);
if(password == null){
return null;
}
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName,password,"customRealm");
authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(userName));
return authenticationInfo;
}
/**
* 模拟数据库或缓存中获取权限数据
* @param userName
* @return
*/
private Set<String> getPermissionsByUserName(String userName) {
Set<String> sets = new HashSet<String>();
sets.add("user:delete");
sets.add("user:add");
return sets;
}
/**
* 模拟从数据库或者缓存中获取角色
* @param userName
* @return
*/
private Set<String> getRolesByUserName(String userName) {
Set<String> sets = new HashSet<String>();
sets.add("admin");
sets.add("user");
return sets;
}
/**
* 模拟数据库查询凭证
* @param userName
* @return
*/
private String getPasswordByUserName(String userName) {
return userMap.get(userName);
}
}
在com.amoscxy.test包中新建测试类:CustomRealmTest:
package com.amoscxy.test;
import com.amoscxy.shiro.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
public class CustomRealmTest {
@Test
public void testAuthentication(){
CustomRealm customRealm = new CustomRealm();
//1.构建SecurityManager
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(customRealm);
// //Shiro加密
// HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
// matcher.setHashAlgorithmName("md5");
// //加密的次数
// matcher.setHashIterations(1);
// customRealm.setCredentialsMatcher(matcher);
//2.主体提交认证请求
SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("Mark","123456");
subject.login(token);
System.out.println("isAuthenticated:"+subject.isAuthenticated());
subject.checkRole("admin");
subject.checkPermissions("user:add","user:delete");
}
}