Java tutorial
/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.wms.studio.realm; import java.io.Serializable; import java.util.Objects; import javax.annotation.PostConstruct; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.log4j.Logger; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AccountException; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.DisabledAccountException; import org.apache.shiro.authc.LockedAccountException; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.cache.CacheManager; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.util.ByteSource; import org.springframework.beans.factory.annotation.Autowired; import com.wms.studio.api.constant.UserConstant; import com.wms.studio.api.constant.UserDisableReason; import com.wms.studio.api.constant.UserRole; import com.wms.studio.api.dto.user.UserLoginDto; import com.wms.studio.api.service.UserService; import com.wms.studio.exception.UnValidationAccountException; import com.wms.studio.exception.ValidateCodeException; import com.wms.studio.token.SystemLoginToken; import com.wms.studio.utils.Encodes; /** * @author WMS * */ public class UserRealm extends AuthorizingRealm { private static final Logger log = Logger.getLogger(UserRealm.class); @Autowired private UserService userservice; public void setUserservice(UserService userservice) { this.userservice = userservice; } /** * ?, ???. */ @Override protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals) { ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal(); UserRole role = shiroUser.role; SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addRole(UserRole..getRole()); if(UserRole.?.equals(role)){ info.addRole(UserRole.?.getRole()); } if(UserRole.?.equals(role)){ info.addRole(UserRole.?.getRole()); info.addRole(UserRole.?.getRole()); } return info; } /** * ?,. */ @Override protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken authcToken) throws AuthenticationException { SystemLoginToken token = (SystemLoginToken) authcToken; if (token.getUsername() == null) { throw new AccountException("??????."); } // ?? String captcha = token.getCaptcha(); String exitCode = (String) SecurityUtils .getSubject() .getSession() .getAttribute( com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); if (null == captcha || !captcha.equalsIgnoreCase(exitCode)) { throw new ValidateCodeException("??"); } UserLoginDto user = userservice.login(token.getUsername()); if (user == null) { return null; } log.info("[]-[??]-?:" + ToStringBuilder.reflectionToString(user)); if (user != null && UserConstant.SUCCESS == user.getResult()) { // ? if (!user.isvStatus()) { log.info("?."); throw new UnValidationAccountException(); } if(user.isDisable()&&UserDisableReason.?.equals(user.getDisableReason())){ throw new LockedAccountException(); } // ? if (user.isDisable()) { log.info("?."); throw new DisabledAccountException(); } byte[] salt = Encodes.decodeHex(user.getSalt()); return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getName(), user.getRole()), user.getPassword(), ByteSource.Util.bytes(salt), getName()); } throw new UnknownAccountException(); } /** * PasswordHash. */ @PostConstruct public void initCredentialsMatcher() { HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(UserConstant.HASH_ALGORITHM); matcher.setHashIterations(UserConstant.HASH_INTERATIONS); setCredentialsMatcher(matcher); } /** * AuthenticationSubject??????. */ @SuppressWarnings("serial") public static class ShiroUser implements Serializable { public String loginName; public String name; public UserRole role; public ShiroUser(String loginName, String name, UserRole role) { this.loginName = loginName; this.name = name; this.role = role; } public String getLoginName() { return loginName; } public String getName() { return name; } public UserRole getRole() { return role; } /** * <shiro:principal/>. */ @Override public String toString() { return loginName; } /** * ?hashCode,?loginName; */ @Override public int hashCode() { return Objects.hashCode(loginName); } /** * ?equals,?loginName; */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } ShiroUser other = (ShiroUser) obj; if (loginName == null) { if (other.loginName != null) { return false; } } else if (!loginName.equals(other.loginName)) { return false; } return true; } } @Override public void setCacheManager(CacheManager cacheManager) { super.setCacheManager(cacheManager); } @Override public void setAuthenticationTokenClass(Class<? extends AuthenticationToken> authenticationTokenClass) { super.setAuthenticationTokenClass(authenticationTokenClass); } @Override public void clearCachedAuthorizationInfo(PrincipalCollection principals) { super.clearCachedAuthorizationInfo(principals); } @Override public void clearCachedAuthenticationInfo(PrincipalCollection principals) { super.clearCachedAuthenticationInfo(principals); } @Override public void clearCache(PrincipalCollection principals) { super.clearCache(principals); } public void clearAllCachedAuthorizationInfo() { getAuthorizationCache().clear(); } public void clearAllCachedAuthenticationInfo() { getAuthenticationCache().clear(); } public void clearAllCache() { clearAllCachedAuthenticationInfo(); clearAllCachedAuthorizationInfo(); } }