Search in sources :

Example 1 with BusinessException

use of com.jun.plugin.system.common.exception.BusinessException in project jun_springboot_api_service by wujun728.

the class ScheduleUtils method run.

/**
 * 立即执行任务
 */
public static void run(Scheduler scheduler, SysJobEntity scheduleJob) {
    try {
        // 参数
        JobDataMap dataMap = new JobDataMap();
        dataMap.put(SysJobEntity.JOB_PARAM_KEY, scheduleJob);
        scheduler.triggerJob(getJobKey(scheduleJob.getId()), dataMap);
    } catch (SchedulerException e) {
        throw new BusinessException("立即执行定时任务失败");
    }
}
Also used : BusinessException(com.jun.plugin.system.common.exception.BusinessException)

Example 2 with BusinessException

use of com.jun.plugin.system.common.exception.BusinessException in project jun_springboot_api_service by wujun728.

the class CustomAccessControlFilter method onAccessDenied.

@Override
protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    try {
        Subject subject = getSubject(servletRequest, servletResponse);
        System.out.println(subject.isAuthenticated() + "");
        System.out.println(HttpContextUtils.isAjaxRequest(request));
        log.info(request.getMethod());
        log.info(request.getRequestURL().toString());
        // 从header中获取token
        String token = request.getHeader(Constant.ACCESS_TOKEN);
        // 如果header中不存在token,则从参数中获取token
        if (StringUtils.isEmpty(token)) {
            token = request.getParameter(Constant.ACCESS_TOKEN);
        }
        if (StringUtils.isEmpty(token)) {
            throw new BusinessException(BaseResponseCode.TOKEN_ERROR);
        }
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(token, token);
        getSubject(servletRequest, servletResponse).login(usernamePasswordToken);
    } catch (BusinessException exception) {
        if (HttpContextUtils.isAjaxRequest(request)) {
            customResponse(exception.getMessageCode(), exception.getDetailMessage(), servletResponse);
        } else if (exception.getMessageCode() == BaseResponseCode.TOKEN_ERROR.getCode()) {
            servletRequest.getRequestDispatcher("/index/login").forward(servletRequest, servletResponse);
        } else if (exception.getMessageCode() == BaseResponseCode.UNAUTHORIZED_ERROR.getCode()) {
            servletRequest.getRequestDispatcher("/index/403").forward(servletRequest, servletResponse);
        } else {
            servletRequest.getRequestDispatcher("/index/500").forward(servletRequest, servletResponse);
        }
        return false;
    } catch (AuthenticationException e) {
        if (HttpContextUtils.isAjaxRequest(request)) {
            if (e.getCause() instanceof BusinessException) {
                BusinessException exception = (BusinessException) e.getCause();
                customResponse(exception.getMessageCode(), exception.getDetailMessage(), servletResponse);
            } else {
                customResponse(BaseResponseCode.SYSTEM_BUSY.getCode(), BaseResponseCode.SYSTEM_BUSY.getMsg(), servletResponse);
            }
        } else {
            servletRequest.getRequestDispatcher("/index/403").forward(servletRequest, servletResponse);
        }
        return false;
    } catch (Exception e) {
        if (HttpContextUtils.isAjaxRequest(request)) {
            if (e.getCause() instanceof BusinessException) {
                BusinessException exception = (BusinessException) e.getCause();
                customResponse(exception.getMessageCode(), exception.getDetailMessage(), servletResponse);
            } else {
                customResponse(BaseResponseCode.SYSTEM_BUSY.getCode(), BaseResponseCode.SYSTEM_BUSY.getMsg(), servletResponse);
            }
        } else {
            servletRequest.getRequestDispatcher("/index/500").forward(servletRequest, servletResponse);
        }
        return false;
    }
    return true;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.jun.plugin.system.common.exception.BusinessException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) Subject(org.apache.shiro.subject.Subject) BusinessException(com.jun.plugin.system.common.exception.BusinessException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 3 with BusinessException

use of com.jun.plugin.system.common.exception.BusinessException in project jun_springboot_api_service by wujun728.

the class CustomRealm method doGetAuthorizationInfo.

/**
 * 执行授权逻辑,只有当需要检测用户权限的时候才会调用此方法,例如checkRole,checkPermission之类的
 */
@Override
@SuppressWarnings("unchecked")
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    String account = JwtUtil.getClaim(principalCollection.toString(), Constant.ACCOUNT);
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    String sessionInfoStr = redisDb.get(userTokenPrefix + principalCollection.getPrimaryPrincipal());
    if (StringUtils.isEmpty(sessionInfoStr)) {
        throw new BusinessException(BaseResponseCode.TOKEN_ERROR);
    }
    JSONObject redisSession = JSON.parseObject(sessionInfoStr);
    if (redisSession == null) {
        throw new BusinessException(BaseResponseCode.TOKEN_ERROR);
    }
    if (redisSession.get(Constant.ROLES_KEY) != null) {
        authorizationInfo.addRoles((Collection<String>) redisSession.get(Constant.ROLES_KEY));
    }
    if (redisSession.get(Constant.PERMISSIONS_KEY) != null) {
        authorizationInfo.addStringPermissions((Collection<String>) redisSession.get(Constant.PERMISSIONS_KEY));
    }
    return authorizationInfo;
}
Also used : BusinessException(com.jun.plugin.system.common.exception.BusinessException) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) JSONObject(com.alibaba.fastjson.JSONObject)

Example 4 with BusinessException

use of com.jun.plugin.system.common.exception.BusinessException in project jun_springboot_api_service by wujun728.

the class PermissionController method updatePermission.

@PutMapping("/permission")
@ApiOperation(value = "更新菜单权限接口")
@LogAnnotation(title = "菜单权限管理", action = "更新菜单权限")
@RequiresPermissions("sys:permission:update")
public DataResult updatePermission(@RequestBody @Valid SysPermission vo) {
    if (StringUtils.isEmpty(vo.getId())) {
        return DataResult.fail("id不能为空");
    }
    SysPermission sysPermission = permissionService.getById(vo.getId());
    if (null == sysPermission) {
        throw new BusinessException(BaseResponseCode.DATA_ERROR);
    }
    // 只有类型变更或者所属菜单变更
    if (sysPermission.getType().equals(vo.getType()) || !sysPermission.getPid().equals(vo.getPid())) {
        verifyFormPid(vo);
    }
    permissionService.updatePermission(vo);
    return DataResult.success();
}
Also used : BusinessException(com.jun.plugin.system.common.exception.BusinessException) SysPermission(com.jun.plugin.system.entity.SysPermission) LogAnnotation(com.jun.plugin.system.common.aop.annotation.LogAnnotation) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with BusinessException

use of com.jun.plugin.system.common.exception.BusinessException in project jun_springboot_api_service by wujun728.

the class JwtFilter method response401.

/**
 * 无需转发,直接返回Response信息
 */
private void response401(ServletResponse response, String msg) {
    HttpServletResponse httpServletResponse = WebUtils.toHttp(response);
    httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
    httpServletResponse.setCharacterEncoding("UTF-8");
    httpServletResponse.setContentType("application/json; charset=utf-8");
    try (PrintWriter out = httpServletResponse.getWriter()) {
        String data = JsonConvertUtil.objectToJson((HttpStatus.UNAUTHORIZED.value() + "无权访问(Unauthorized):" + msg));
        out.append(data);
    } catch (IOException e) {
        logger.error("直接返回Response信息出现IOException异常:{}", e.getMessage());
        throw new BusinessException(" 直接返回Response信息出现IOException异常: ");
    }
}
Also used : BusinessException(com.jun.plugin.system.common.exception.BusinessException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

BusinessException (com.jun.plugin.system.common.exception.BusinessException)32 SysUser (com.jun.plugin.system.entity.SysUser)7 SysDept (com.jun.plugin.system.entity.SysDept)5 IOException (java.io.IOException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 SysPermission (com.jun.plugin.system.entity.SysPermission)3 SysRolePermission (com.jun.plugin.system.entity.SysRolePermission)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Algorithm (com.auth0.jwt.algorithms.Algorithm)2 SysRole (com.jun.plugin.system.entity.SysRole)2 DeptRespNodeVO (com.jun.plugin.system.vo.resp.DeptRespNodeVO)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SecureRandom (java.security.SecureRandom)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 KeyGenerator (javax.crypto.KeyGenerator)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 SecretKey (javax.crypto.SecretKey)2