Search in sources :

Example 11 with OnexException

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);
            }
        }
    }
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) ObsException(com.obs.services.exception.ObsException) ObsObject(com.obs.services.model.ObsObject) ObsClient(com.obs.services.ObsClient)

Example 12 with OnexException

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);
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with OnexException

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());
        }
    }
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) JWT(cn.hutool.jwt.JWT) AuthProps(com.nb6868.onex.common.auth.AuthProps) Map(java.util.Map)

Example 14 with OnexException

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;
    }
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) OnexException(com.nb6868.onex.common.exception.OnexException)

Aggregations

OnexException (com.nb6868.onex.common.exception.OnexException)13 OSS (com.aliyun.oss.OSS)3 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)3 ClientException (com.aliyuncs.exceptions.ClientException)3 OSSException (com.aliyun.oss.OSSException)2 ObsClient (com.obs.services.ObsClient)2 ObsException (com.obs.services.exception.ObsException)2 IOException (java.io.IOException)2 Dict (cn.hutool.core.lang.Dict)1 Entity (cn.hutool.db.Entity)1 JWT (cn.hutool.jwt.JWT)1 OSSObject (com.aliyun.oss.model.OSSObject)1 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)1 AssumeRoleRequest (com.aliyuncs.auth.sts.AssumeRoleRequest)1 AssumeRoleResponse (com.aliyuncs.auth.sts.AssumeRoleResponse)1 IClientProfile (com.aliyuncs.profile.IClientProfile)1 ColumnEntity (com.nb6868.onex.coder.entity.ColumnEntity)1 TableEntity (com.nb6868.onex.coder.entity.TableEntity)1 LogOperation (com.nb6868.onex.common.annotation.LogOperation)1 AuthProps (com.nb6868.onex.common.auth.AuthProps)1