use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.
the class HuaweiCloudOssService method download.
@Override
public InputStream download(String objectKey) {
ObsClient ossClient = null;
ObsObject ossObject = null;
try {
ossClient = new ObsClient(config.getAccessKeyId(), config.getAccessKeySecret(), config.getEndPoint());
ossObject = ossClient.getObject(config.getBucketName(), objectKey);
return ossObject.getObjectContent();
} catch (ObsException e) {
throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
} finally {
// ObsClient在调用ObsClient.close方法关闭后不能再次使用
if (ossClient != null) {
try {
ossClient.close();
} catch (IOException e) {
log.error("huaweicloud obs client close error", e);
}
}
}
}
use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.
the class ExcelUtils method exportExcelToTarget.
/**
* Excel导出,先sourceList转换成List<targetClass>,再导出
*
* @param response response
* @param fileName 文件名
* @param sourceList 原数据List
* @param targetClass 目标对象Class
*/
public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> sourceList, Class<?> targetClass) {
List<Object> targetList = new ArrayList<>(sourceList.size());
for (Object source : sourceList) {
Object target;
try {
target = targetClass.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
throw new OnexException(ErrorCode.EXCEL_EXPORT_ERROR, e);
}
BeanUtils.copyProperties(source, target);
targetList.add(target);
}
exportExcel(response, fileName, targetList, targetClass);
}
use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.
the class ShiroRealm method doGetAuthenticationInfo.
/**
* 认证(登录时调用)
* doGetAuthenticationInfo->doGetAuthorizationInfo
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// AuthenticationToken包含身份信息和认证信息
String token = authenticationToken.getCredentials().toString();
AuthProps.Config loginConfig;
// 尝试解析为jwt
JWT jwt = JwtUtils.parseToken(token);
if (jwt == null) {
// 非jwt,从数据库里获得类型
Map<String, Object> tokenEntity = shiroDao.getUserTokenByToken(token);
if (tokenEntity == null) {
throw new OnexException(ErrorCode.UNAUTHORIZED);
}
loginConfig = authProps.getConfigs().get(MapUtil.getStr(tokenEntity, "type"));
if (null == loginConfig) {
throw new OnexException(ErrorCode.UNAUTHORIZED);
}
// 查询用户信息
Map<String, Object> userEntity = shiroDao.getUserById(MapUtil.getLong(tokenEntity, "user_id"));
if (userEntity == null) {
// 账号不存在
throw new OnexException(ErrorCode.ACCOUNT_NOT_EXIST);
} else if (MapUtil.getInt(userEntity, "state") != ShiroConst.USER_STATE_ENABLED) {
// 账号锁定
throw new OnexException(ErrorCode.ACCOUNT_LOCK);
}
// 转换成UserDetail对象
ShiroUser userDetail = BeanUtil.mapToBean(userEntity, ShiroUser.class, true, CopyOptions.create().setIgnoreCase(true));
userDetail.setLoginConfig(loginConfig);
if (loginConfig.isTokenRenewal()) {
// 更新token
shiroDao.updateTokenExpireTime(token, loginConfig.getTokenExpire());
}
return new SimpleAuthenticationInfo(userDetail, token, getName());
} else {
// jwt,先验证
loginConfig = authProps.getConfigs().get(jwt.getPayload().getClaimsJson().getStr(authProps.getTokenTypeKey()));
if (null == loginConfig || !JwtUtils.verifyKey(jwt, loginConfig.getTokenKey())) {
throw new OnexException(ErrorCode.UNAUTHORIZED);
}
// 开始验证数据
if ("jwt".equalsIgnoreCase(loginConfig.getVerifyType())) {
// jwt校验
Map<String, Object> userEntity = shiroDao.getUserById(jwt.getPayload().getClaimsJson().getLong("id"));
if (userEntity == null) {
// 账号不存在
throw new OnexException(ErrorCode.ACCOUNT_NOT_EXIST);
} else if (MapUtil.getInt(userEntity, "state") != ShiroConst.USER_STATE_ENABLED) {
// 账号锁定
throw new OnexException(ErrorCode.ACCOUNT_LOCK);
}
// 转换成UserDetail对象
ShiroUser userDetail = BeanUtil.mapToBean(userEntity, ShiroUser.class, true, CopyOptions.create().setIgnoreCase(true));
userDetail.setLoginConfig(loginConfig);
if (loginConfig.isTokenRenewal()) {
// 更新token
shiroDao.updateTokenExpireTime(token, loginConfig.getTokenExpire());
}
return new SimpleAuthenticationInfo(userDetail, token, getName());
} else if ("jwtSimple".equalsIgnoreCase(loginConfig.getVerifyType())) {
// 只简单校验jwt密钥和有效时间,不过数据库
if (!JwtUtils.verifyKeyAndExp(jwt, loginConfig.getTokenKey())) {
throw new OnexException(ErrorCode.UNAUTHORIZED);
}
// 转换成UserDetail对象
ShiroUser userDetail = JSONUtil.toBean(jwt.getPayload().getClaimsJson(), ShiroUser.class);
userDetail.setLoginConfig(loginConfig);
return new SimpleAuthenticationInfo(userDetail, token, getName());
} else {
// 默认full,完整校验,从数据库走
// 根据accessToken,查询用户信息
Map<String, Object> tokenEntity = shiroDao.getUserTokenByToken(token);
if (tokenEntity == null) {
throw new OnexException(ErrorCode.UNAUTHORIZED);
}
// 查询用户信息
Map<String, Object> userEntity = shiroDao.getUserById(MapUtil.getLong(tokenEntity, "user_id"));
if (userEntity == null) {
// 账号不存在
throw new OnexException(ErrorCode.ACCOUNT_NOT_EXIST);
} else if (MapUtil.getInt(userEntity, "state") != ShiroConst.USER_STATE_ENABLED) {
// 账号锁定
throw new OnexException(ErrorCode.ACCOUNT_LOCK);
}
// 转换成UserDetail对象
ShiroUser userDetail = BeanUtil.mapToBean(userEntity, ShiroUser.class, true, CopyOptions.create().setIgnoreCase(true));
userDetail.setLoginConfig(loginConfig);
if (loginConfig.isTokenRenewal()) {
// 更新token
shiroDao.updateTokenExpireTime(token, loginConfig.getTokenExpire());
}
return new SimpleAuthenticationInfo(userDetail, token, getName());
}
}
}
use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.
the class ScheduleUtils method run.
/**
* 立即执行任务
*/
public static void run(Scheduler scheduler, TaskInfo taskInfo) {
try {
// 参数
JobDataMap dataMap = new JobDataMap();
dataMap.put(SchedConst.JOB_PARAM_KEY, taskInfo);
scheduler.triggerJob(getJobKey(taskInfo.getId()), dataMap);
} catch (SchedulerException e) {
throw new OnexException(ErrorCode.JOB_ERROR, e);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
Aggregations