package com.wms_admin.shiro.realm; import com.wms_admin.sys.entity.User; import com.wms_admin.sys.service.UserService; import com.wms_admin.utils.SpringUtil; 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.CredentialsMatcher; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.crypto.hash.Md5Hash; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.util.ByteSource; import org.springframework.stereotype.Component; 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 = SpringUtil.getBean(UserService.class); user = userService.accordingUsernameSelectAll(principal); if (!ObjectUtils.isEmpty(user)) { return new SimpleAuthenticationInfo(user.getAccount(), user.getPassword(), null, this.getName()); } return null; } }