use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.
the class AgileUserDetailsServiceImpl method loadUserByUsername.
@Override
public UserDetails loadUserByUsername(String loginName) throws UsernameNotFoundException {
try {
if (agileUserDetailsService == null) {
throw new AgileFrameException(AgileResultCode.FAIL_SERVER_EXCEPTION, "请设置用户验证接口实现类!");
}
AgileBaseUser userData = agileUserDetailsService.getUserDataByLoginName(loginName);
if (userData != null && AgileStringUtil.isNotEmpty(userData.getUserId())) {
userData.setUserToken(AgileStringUtil.getUuid());
userData.setUserPerm(agileUserDetailsService.getUserPerm(userData));
userData.setUserRole(agileUserDetailsService.getUserRole(userData));
HttpServletRequest httpServletRequest = AgileServletUtil.getHttpServletRequest();
if (httpServletRequest != null) {
UserAgent userAgent = AgileAgentUtil.getUserAgent(httpServletRequest);
userData.setLoginIp(AgileAgentUtil.getUserClientIp(httpServletRequest));
userData.setOsName(userAgent.getOperatingSystem().getName());
userData.setDeviceName(userAgent.getOperatingSystem().getDeviceType().getName());
userData.setBrowserName(userAgent.getBrowser().getName());
}
List<SimpleGrantedAuthority> authorities = userData.getUserRole().stream().map(role -> new SimpleGrantedAuthority(role)).collect(Collectors.toList());
AgileUserDetails agileUserDetails = new AgileUserDetails();
agileUserDetails.setUserData(userData);
agileUserDetails.setAuthorities(authorities);
return agileUserDetails;
} else {
throw new AgileAuthException(AgileResultCode.FAIL_USER_NAME);
}
} catch (AgileBaseException ex) {
throw ex;
} catch (Exception ex) {
throw new AgileAuthException(AgileResultCode.FAIL_AUTH_EXCEPTION, ex);
}
}
use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.
the class AgileAuthorizingRealm method doGetAuthorizationInfo.
/**
* 用户授权
*
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
try {
AgileBaseUser userData = (AgileBaseUser) principalCollection.getPrimaryPrincipal();
if (userData == null || AgileStringUtil.isEmpty(userData.getUserId())) {
throw new AgileAuthException(AgileResultCode.FAIL_USER_INFO);
}
SimpleAuthorizationInfo authenticationInfo = new SimpleAuthorizationInfo();
List<String> userPermList = userData.getUserPerm();
if (userPermList == null || userPermList.isEmpty()) {
userPermList = agileUserDetailsService.getUserPerm(userData);
userData.setUserPerm(userPermList);
}
authenticationInfo.addStringPermissions(userPermList);
List<String> userRoleList = userData.getUserRole();
if (userRoleList == null || userRoleList.isEmpty()) {
userRoleList = agileUserDetailsService.getUserRole(userData);
userData.setUserRole(userRoleList);
}
authenticationInfo.addRoles(userRoleList);
return authenticationInfo;
} catch (AgileBaseException ex) {
throw ex;
} catch (Exception ex) {
throw new AgileAuthException(AgileResultCode.FAIL_AUTH_EXCEPTION, ex.getMessage());
}
}
use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.
the class AgileShiroSecurity method checkRole.
@Override
public void checkRole(AgileRequiresRoles agileRequiresRoles) {
try {
String[] roles = agileRequiresRoles.value();
Subject subject = SecurityUtils.getSubject();
if (roles.length == 1) {
subject.checkRole(roles[0]);
return;
}
if (AgileLogical.AND.equals(agileRequiresRoles.logical())) {
subject.checkRoles(Arrays.asList(roles));
return;
}
if (AgileLogical.OR.equals(agileRequiresRoles.logical())) {
boolean hasAtLeastOneRole = false;
for (String role : roles) if (subject.hasRole(role))
hasAtLeastOneRole = true;
if (!hasAtLeastOneRole)
subject.checkRole(roles[0]);
}
} catch (Exception ex) {
logger.error("SHIRO用户角色校验异常", ex);
throw new AgileAuthException(AgileResultCode.FAIL_USER_PERMS);
}
}
use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.
the class AgileShiroSecurity method userLogin.
@Override
public void userLogin(AgileLoginUser agileLoginUser) {
try {
UsernamePasswordToken token = new UsernamePasswordToken(agileLoginUser.getUserName(), agileLoginUser.getPassword());
token.setRememberMe(agileLoginUser.isRememberMe());
SecurityUtils.getSubject().login(token);
} catch (AgileBaseException ex) {
throw ex;
} catch (Exception ex) {
if (ex.getCause() instanceof AgileBaseException) {
throw (AgileBaseException) ex.getCause();
}
throw new AgileAuthException("SHIRO用户登录认证出现异常!");
}
}
use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.
the class AgileUserDetailsServiceImpl method getUserDataByLoginName.
@Override
public AgileUserData getUserDataByLoginName(String loginName) {
try {
AgileSysUser agileSysUser = this.getAgileSysUser(loginName);
if (agileSysUser == null) {
throw new AgileAuthException("用户《" + loginName + "》不存在,请核实!");
}
this.checkAgileSysUser(agileSysUser);
return getAgileUserData(agileSysUser);
} catch (AgileBaseException ex) {
throw ex;
} catch (Exception ex) {
throw new AgileAuthException("加载用户信息异常!");
}
}
Aggregations