Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.thinkgem.jeesite.modules.sys.security; import java.io.Serializable; import java.util.HashMap; import java.util.List; import java.util.Map; 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 com.thinkgem.jeesite.common.utils.Encodes; import com.thinkgem.jeesite.modules.sys.entity.Menu; import com.thinkgem.jeesite.modules.sys.entity.User; import com.thinkgem.jeesite.modules.sys.service.SystemService; import com.thinkgem.jeesite.modules.sys.utils.UserUtils; /** * ? * * @author ThinkGem * @version 2013-01-15 */ public class SystemRealm extends AuthorizingRealm { private SystemService systemService; /** * ?, */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; User user = systemService.getUserByLoginName(token.getUsername()); 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) { Principal principal = (Principal) getAvailablePrincipal(principals); User user = systemService.getUserByLoginName(principal.getLoginName()); if (user != null) { UserUtils.putCache("user", user); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); List<Menu> list = systemService.findAllMenu(); for (Menu menu : list) { if (StringUtils.isNotBlank(menu.getPermission())) { // Permission??? info.addStringPermission(menu.getPermission()); } } // IP systemService.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); UserUtils.removeCache("user"); } /** * ?? */ public void clearAllCachedAuthorizationInfo() { Cache<Object, AuthorizationInfo> cache = getAuthorizationCache(); if (cache != null) { for (Object key : cache.keys()) { cache.remove(key); } } UserUtils.removeCache("menuList"); UserUtils.removeCache("categoryList"); } public void setSystemService(SystemService systemService) { this.systemService = systemService; } /** * ?? */ public static class Principal implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String loginName; private String name; private Map<String, Object> cacheMap; public Principal(User user) { this.id = user.getId(); this.loginName = user.getLoginName(); this.name = user.getName(); } public Long getId() { return id; } public String getLoginName() { return loginName; } public String getName() { return name; } public Map<String, Object> getCacheMap() { if (cacheMap == null) { cacheMap = new HashMap<String, Object>(); } return cacheMap; } } }