package com.whyc.realm; import com.whyc.anotation.Realm; import com.whyc.pojo.db_user.UserInf; import com.whyc.service.UserBridgeService; import com.whyc.util.RSAUtil; import org.apache.shiro.authc.*; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; /** * 实际处理认证授权,跟数据库交互 */ @Realm public class CustomRealm extends AuthorizingRealm { /**必须@Lazy注解,@Lazy与@Autowired组合,使得依赖于Service相关的Bean都是lazy-resolution proxy*/ @Lazy @Autowired UserBridgeService userBridgeService; /** * 认证 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String userName = (String) authenticationToken.getPrincipal(); UserInf user = userBridgeService.findPasswordByUserName(userName); if(user.getUid()==0){ throw new UnknownAccountException("账号不存在"); } String password = RSAUtil.decrypt(user.getUsnid(),RSAUtil.getPrivateKey()); return new SimpleAuthenticationInfo(user,password,getName()); } /** * 授权 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { UserInf user = (UserInf) principalCollection.getPrimaryPrincipal(); return userBridgeService.getAuthorizationInfo(user); } /**加密*//* @PostConstruct public void initCredentialsMatcher() { HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(SuperConstant.HASH_ALGORITHM_MD5); matcher.setHashIterations(SuperConstant.HASH_INTEGRATIONS_ONE); setCredentialsMatcher(matcher); }*/ }