shiro管理多登录入口配置,手机端登录与网页端登录
1、2个Realm:分别为手机端、网页端的
/**
* 自定义MobileRealm .
*/
public class MobileRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
@Override
// 验证的核心方法
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String userCode = (String) token.getPrincipal();
UsernamePasswordToken usernamePassword = (UsernamePasswordToken) token;
char[] passWord = usernamePassword.getPassword();
if (StringUtil.isEmpty(userCode)) {
throw new UserNameNullException();// 账号为空
}
if (StringUtil.isEmpty(passWord)) {
throw new PassWordNullException();// 密码为空
}
// 根据登录名查询操作员
Admin operator = null;
try {
operator = adminService.getAdminUserByUserCode(userCode);
} catch (Exception e) {
e.printStackTrace();
}
if (operator == null) {
throw new UnknownAccountException();// 没找到帐号
}
if (!Constants.UserLockStatus.UNLOCK.equals(operator.getLock_status())) {
throw new LockedAccountException(); // 账号锁定或者不是生效状态
}
if (!Constants.UserStatus.OPEN.equals(operator.getUser_status())) {
throw new LockedAccountException(); // 账号锁定或者不是生效状态
}
// 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(operator.getUser_name(), // 登录名
operator.getUser_pswd(), // 密码
getName() // realm name
);
return authenticationInfo;
}
@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();
}
}
/**
* 自定义realm .
*
*/
public class WebRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
@Override
// 验证的核心方法
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String userCode = (String) token.getPrincipal();
UsernamePasswordToken usernamePassword = (UsernamePasswordToken) token;
char[] passWord = usernamePassword.getPassword();
if (StringUtil.isEmpty(userCode)) {
throw new UserNameNullException();// 账号为空
}
if (StringUtil.isEmpty(passWord)) {
throw new PassWordNullException();// 密码为空
}
// 根据登录名查询操作员
Admin operator = null;
try {
operator = adminService.getAdminUserByUserCode(userCode);
} catch (Exception e) {
e.printStackTrace();
}
if (operator == null) {
throw new UnknownAccountException();// 没找到帐号
}
if (!Constants.UserLoc