use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.
the class AgileUserDetailsServiceImpl method userLogin.
@Override
public AgileBaseUser userLogin(String loginName, String userPassword) {
try {
AgileSysUser agileSysUser = this.getAgileSysUser(loginName);
if (agileSysUser == null) {
throw new AgileAuthException("用户《" + loginName + "》不存在,请核实!");
}
this.checkAgileSysUser(agileSysUser);
String md5Password = AgileSecurityUtil.encryptPassword(userPassword);
if (!md5Password.equals(agileSysUser.getUserPwd())) {
throw new AgileAuthException(AgileResultCode.FAIL_USER_PWD, "用户密码错误!");
}
return getAgileUserData(agileSysUser);
} catch (AgileBaseException ex) {
throw ex;
} catch (Exception ex) {
throw new AgileAuthException("用户登录异常!");
}
}
use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.
the class AgileUserDetailsServiceImpl method getUserPerm.
@Override
public List<String> getUserPerm(AgileBaseUser agileBaseUser) {
try {
if (agileBaseUser != null) {
if (agileBaseUser.isSuperAdmin()) {
List<String> userPermList = new ArrayList<>();
userPermList.add("*:*:*");
return userPermList;
} else {
return agileUserDetailsMapper.getUserPermByUserId(agileBaseUser.getUserId());
}
} else {
throw new AgileAuthException(AgileResultCode.FAIL_USER_INFO);
}
} catch (AgileBaseException ex) {
throw ex;
} catch (Exception ex) {
throw new AgileAuthException("加载用户权限信息异常!");
}
}
use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.
the class AgileSecurityInterceptor method checkUserSecurity.
/**
* 权限校验
*
* @param handlerMethod
*/
private void checkUserSecurity(HandlerMethod handlerMethod) {
try {
// 获取当前用户安全认证
IAgileSecurity agileSecurity = AgileSecurityUtil.getAgileSecurity();
if (agileSecurity == null) {
throw new AgileAuthException("请设置用户安全接口类《UserSecurity》");
}
// 当前线程存放用户信息
AgileSecurityContext.putCurrentUser(agileSecurity.getUserData());
// 演示模式拦截
AgileDemo agileDemo = handlerMethod.getMethodAnnotation(AgileDemo.class);
if (agileDemo != null && AgileUtil.isDemoEnabled()) {
throw new AgileDemoException();
}
// 如果为超管用户则不在进行权限校验
if (agileSecurity.getUserData().isSuperAdmin()) {
return;
}
AgileRequiresGuest agileRequiresGuest = handlerMethod.getBeanType().getAnnotation(AgileRequiresGuest.class);
if (agileRequiresGuest != null) {
return;
}
agileRequiresGuest = handlerMethod.getMethodAnnotation(AgileRequiresGuest.class);
if (agileRequiresGuest != null) {
return;
}
AgileRequiresAuthentication agileRequiresAuthentication = handlerMethod.getMethodAnnotation(AgileRequiresAuthentication.class);
if (agileRequiresAuthentication != null && !agileSecurity.checkAuthenticated()) {
throw new AgileAuthException("用户未验证通过!");
}
AgileRequiresUser agileRequiresUser = handlerMethod.getMethodAnnotation(AgileRequiresUser.class);
if (agileRequiresUser != null) {
agileSecurity.checkUser();
}
AgileRequiresRoles agileRequiresRoles = handlerMethod.getMethodAnnotation(AgileRequiresRoles.class);
if (agileRequiresRoles != null) {
agileSecurity.checkRole(agileRequiresRoles);
}
AgilePermissionsPrefix agilePermissionsPrefix = handlerMethod.getBeanType().getAnnotation(AgilePermissionsPrefix.class);
AgileRequiresPermissions agileRequiresPermissions = handlerMethod.getMethodAnnotation(AgileRequiresPermissions.class);
if (agileRequiresPermissions != null) {
if (agilePermissionsPrefix != null) {
agileSecurity.checkPermission(agilePermissionsPrefix, agileRequiresPermissions);
} else {
agileSecurity.checkPermission(agileRequiresPermissions);
}
}
} catch (AgileBaseException ex) {
throw ex;
} catch (Exception ex) {
logger.error("用户权限验证异常", ex);
throw new AgileAuthException("用户权限验证异常!");
}
}
use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.
the class AgileAuthorizingRealm method doGetAuthenticationInfo.
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {
if (agileUserDetailsService == null) {
throw new AgileFrameException(AgileResultCode.FAIL_SERVER_EXCEPTION, "请设置用户验证接口实现类!");
}
// 获取用户名
String loginName = (String) authenticationToken.getPrincipal();
// 字符类型密码获取(用户输入的密码)
char[] credentials = (char[]) authenticationToken.getCredentials();
if (credentials == null || credentials.length < 1) {
return null;
}
// 把字符数组转换为String类型(用户输入的密码)
String password = new String(credentials);
try {
AgileBaseUser userData = agileUserDetailsService.getUserDataByLoginName(loginName);
if (userData != null && AgileStringUtil.isNotEmpty(userData.getUserId())) {
if (AgileSecurityUtil.encryptPassword(password).equals(userData.getPassword())) {
userData.setUserToken(SecurityUtils.getSubject().getSession().getId().toString());
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.setLoginAddress(AgileNetUtil.getAddressByIp(userData.getLoginIp()));
userData.setOsName(userAgent.getOperatingSystem().getName());
userData.setDeviceName(userAgent.getOperatingSystem().getDeviceType().getName());
userData.setBrowserName(userAgent.getBrowser().getName());
}
return new SimpleAuthenticationInfo(userData, password, userData.getUserName());
} else {
throw new AgileAuthException(AgileResultCode.FAIL_USER_PWD);
}
} 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 AgileSpringSecurity method userLogin.
@Override
public void userLogin(AgileLoginUser agileLoginUser) {
try {
UsernamePasswordAuthenticationToken passwordAuthenticationToken = new UsernamePasswordAuthenticationToken(agileLoginUser.getUserName(), agileLoginUser.getPassword());
Authentication authentication = authenticationManager.authenticate(passwordAuthenticationToken);
AgileUserDetails agileUserDetails = (AgileUserDetails) authentication.getPrincipal();
if (agileUserDetails != null && AgileStringUtil.isNotEmpty(agileUserDetails.getUsername())) {
String userToken = agileUserDetails.getUserData().getUserToken();
AgileCacheUtil.put(AgileCacheConstants.AGILE_CACHE_SESSION_NAME, userToken, agileUserDetails);
sessionRegistry.registerNewSession(userToken, userToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
} catch (AgileBaseException ex) {
throw ex;
} catch (Exception ex) {
if (ex.getCause() instanceof AgileBaseException) {
throw (AgileBaseException) ex.getCause();
} else if (ex instanceof BadCredentialsException) {
throw new AgileAuthException("用户登录密码错误!");
} else {
log.error("Spring Security用户登录认证出现异常", ex);
throw new AgileAuthException("Spring Security用户登录认证出现异常!");
}
}
}
Aggregations