package com.yuanchu.limslaboratory.shiro.realm; import com.yuanchu.limslaboratory.pojo.User; import com.yuanchu.limslaboratory.service.UserService; import com.yuanchu.limslaboratory.utils.MyUtils; import com.yuanchu.limslaboratory.utils.SpringUtils; import org.apache.shiro.authc.*; import org.apache.shiro.authc.credential.CredentialsMatcher; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.springframework.util.ObjectUtils; public class ShiroRealm extends AuthorizingRealm { public User user; /** * 限定这个 Realm 只处理 UsernamePasswordToken */ @Override public boolean supports(AuthenticationToken token) { return token instanceof UsernamePasswordToken; } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { return null; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) { String principal = (String) authenticationToken.getPrincipal(); //获取UserService对象 UserService userService = SpringUtils.getBean(UserService.class); user = userService.AccordingUsernameSelectAll(principal); MyUtils.PrintLog(user.toString()); if (!ObjectUtils.isEmpty(user)) { return new SimpleAuthenticationInfo(user.getAccount(), user.getPassword(), this.getName()); } else { throw new UnknownAccountException(); } } @Override public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) { // HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); // //设置使用MD5加密算法 // hashedCredentialsMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME); // //散列次数 // hashedCredentialsMatcher.setHashIterations(1024); // super.setCredentialsMatcher(hashedCredentialsMatcher); super.setCredentialsMatcher(credentialsMatcher); } }