package com.cmb.crm.modules.sys.security;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
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.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;
import com.cmb.crm.common.utils.Encodes;
import com.cmb.crm.modules.sys.model.Menu;
import com.cmb.crm.modules.sys.model.User;
import com.cmb.crm.modules.sys.service.SystemService;
import com.cmb.crm.modules.sys.util.UserUtils;
/**
* 系统安全认证实现类
*
* @author xiongliangsheng
* @version 2014-12-08
*/
@Service("crmAuthorizingRealm")
public class SystemAuthorizingRealm extends AuthorizingRealm {
public static final int SALT_SIZE = 16;
public static final int HASH_INTERATIONS = 1024;
public SystemAuthorizingRealm() {
super();
}
/**
* 认证回调函数, 登录时调用
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
/* 这里编写认证代码 */
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
String userName = token.getUsername();
User user = new User();
// User user = getSystemService().getUserByLoginName(userName);
user.setId("001");
user.setUsername(userName);
user.setPassword(SystemService.entryptPassword("admin"));
if (user != null) {
byte[] salt = Encodes.decodeHex(user.getPassword().substring(0,16));
return new SimpleAuthenticationInfo(new Principal(user),
user.getPassword().substring(16), ByteSource.Util.bytes(salt), getName());
} else {
return null;
}
}
/**
* 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
/* 这里编写授权代码 */
Set<String> roleNames = new HashSet<String>();
Set<String> permissions = new HashSet<String>();
Principal principal = (Principal)principals.getPrimaryPrincipal();
//User user = getSystemService().getUserByLoginName(principal.getId());
User user = new User();
user.setId("001");
if ("superAdmin".equalsIgnoreCase(principal.getUserName())) {
roleNames.add("superAdmin");
permissions.add("superAdmin");
permissions.add("main");
permissions.add("admin");
} else {
roleNames.add("admin");
permissions.add("admin");
permissions.add("main");
}
if (user != null) {
UserUtils.putCache(UserUtils.CACHE_USER, user);
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.setStringPermissions(permissions);
info.addRoles(roleNames);
/*List<Menu> list = UserUtils.getMenuList();
for (Menu menu : list){
if (StringUtils.isNotBlank(menu.getPermission())){
// 添加基于Permission的权限信息
for (String permission : StringUtils.split(menu.getPermission(),",")){
info.addStringPermission(permission);
}
}
}*/
// 更新登录IP和时间
//getSystemService().updateUserLoginInfo(user.getId());
return info;
} else {
return null;
}
}
/**
* 设定密码校验的Hash算法与迭代次数
*/
@PostConstruct
public void initCredentialsMatcher() {
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(SystemService.HASH_ALGORITHM);
matcher.setHashIterations(SystemService.HASH_INTERATIONS);
setCredentialsMatcher(matcher);
}
/**
* 清空用户关联权限认证,待下次使用时重新加载
*/
public void clearCachedAuthorizationInfo(String principal) {
SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
clearCachedAuthorizationInfo(principals);
}
/**
* 清空所有关联认证
*/
public void clearAllCachedAuthorizationInfo() {
Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
if (cache != null) {
for (Object key : cache.keys()) {
cache.remove(key);
}
}
}
/**
* 获取系统业务对象
*/
/*
* public SystemService getSystemService() { if (systemService == null){
* systemService = SpringContextHolder.getBean(SystemService.class); }
* return systemService; }
*/
/**
* 授权用户信息
*/
public static class Principal implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String userName;
private Map<String, Object> cacheMap;
public Principal(User user) {
this.id = user.getId();
this.userName = user.getUsername();
}
public String getId() {
return id;
}
public String getUserName() {
return userName;
}
public Map<String, Object> getCacheMap() {
if (cacheMap==null){
cacheMap = new HashMap<String, Object>();
}
return cacheMap;
}
}
}