package com.whyc.realm;
|
|
import com.whyc.anotation.Realm;
|
import com.whyc.pojo.db_user.User;
|
import com.whyc.service.UserBridgeService;
|
import com.whyc.util.MessageUtils;
|
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 name = (String) authenticationToken.getPrincipal();
|
User user = userBridgeService.findPasswordByUserName(name);
|
if(user.getId()==0){
|
throw new UnknownAccountException(MessageUtils.getMessage("AccountOrPasswordError"));
|
}
|
String pwd = RSAUtil.decrypt(user.getPwd(),RSAUtil.getPrivateKey());
|
return new SimpleAuthenticationInfo(user,pwd,getName());
|
}
|
|
/**
|
* 授权
|
*/
|
@Override
|
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
|
User user = (User) 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);
|
}*/
|
}
|