Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/free lance/infosys">infosys</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.exam.mserver.common.persistence.util; 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.SecurityUtils; 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.session.Session; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.SimplePrincipalCollection; import org.apache.shiro.util.ByteSource; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Service; import com.exam.mserver.controller.LoginController; import com.exam.mserver.entity.User; import com.exam.mserver.service.UserService; /** * ? * @author free lance * @version 2013-5-29 */ @Service @DependsOn({ "userDao", "roleDao", "menuDao" }) public class MyRealm extends AuthorizingRealm { private UserService systemService; /** * ?, */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; User user = getSystemService().findUserByName(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 = getSystemService().findUserByName(principal.getLoginName()); if (user != null) { UserUtil.putCache("user", user); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); return info; } else { return null; } } /** * ?Hash */ @PostConstruct public void initCredentialsMatcher() { HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(UserService.HASH_ALGORITHM); matcher.setHashIterations(UserService.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 UserService getSystemService() { if (systemService == null) { systemService = ApplicationContextHelper.getBean(UserService.class); } return systemService; } /** * ?? */ public static class Principal implements Serializable { private static final long serialVersionUID = 1L; private String id; private String loginName; private Map<String, Object> cacheMap; public Principal(User user) { this.id = user.getId(); this.loginName = user.getUserName(); } public String getId() { return id; } public String getLoginName() { return loginName; } public Map<String, Object> getCacheMap() { if (cacheMap == null) { cacheMap = new HashMap<String, Object>(); } return cacheMap; } } }