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("立即执行定时任务失败");
}
}
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;
}
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;
}
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();
}
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异常: ");
}
}
Aggregations