Search in sources :

Example 41 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue by yangzongzhuan.

the class RateLimiterAspect method doBefore.

@Before("@annotation(rateLimiter)")
public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable {
    String key = rateLimiter.key();
    int time = rateLimiter.time();
    int count = rateLimiter.count();
    String combineKey = getCombineKey(rateLimiter, point);
    List<Object> keys = Collections.singletonList(combineKey);
    try {
        Long number = redisTemplate.execute(limitScript, keys, count, time);
        if (StringUtils.isNull(number) || number.intValue() > count) {
            throw new ServiceException("访问过于频繁,请稍候再试");
        }
        log.info("限制请求'{}',当前请求'{}',缓存key'{}'", count, number.intValue(), key);
    } catch (ServiceException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("服务器限流异常,请稍候再试");
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) JoinPoint(org.aspectj.lang.JoinPoint) ServiceException(com.ruoyi.common.exception.ServiceException) Before(org.aspectj.lang.annotation.Before)

Example 42 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue by yangzongzhuan.

the class SysLoginService method login.

/**
 * 登录验证
 *
 * @param username 用户名
 * @param password 密码
 * @param code 验证码
 * @param uuid 唯一标识
 * @return 结果
 */
public String login(String username, String password, String code, String uuid) {
    boolean captchaOnOff = configService.selectCaptchaOnOff();
    // 验证码开关
    if (captchaOnOff) {
        validateCaptcha(username, code, uuid);
    }
    // 用户验证
    Authentication authentication = null;
    try {
        // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
        authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    } catch (Exception e) {
        if (e instanceof BadCredentialsException) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
            throw new UserPasswordNotMatchException();
        } else {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
            throw new ServiceException(e.getMessage());
        }
    }
    AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
    LoginUser loginUser = (LoginUser) authentication.getPrincipal();
    recordLoginInfo(loginUser.getUserId());
    // 生成token
    return tokenService.createToken(loginUser);
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserPasswordNotMatchException(com.ruoyi.common.exception.user.UserPasswordNotMatchException) LoginUser(com.ruoyi.common.core.domain.model.LoginUser) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) ServiceException(com.ruoyi.common.exception.ServiceException) CaptchaExpireException(com.ruoyi.common.exception.user.CaptchaExpireException) CaptchaException(com.ruoyi.common.exception.user.CaptchaException) UserPasswordNotMatchException(com.ruoyi.common.exception.user.UserPasswordNotMatchException)

Example 43 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class SysOssController method download.

@ApiOperation("下载OSS对象存储")
@SaCheckPermission("system:oss:download")
@GetMapping("/download/{ossId}")
public void download(@ApiParam("OSS对象ID") @PathVariable Long ossId, HttpServletResponse response) throws IOException {
    SysOss sysOss = iSysOssService.getById(ossId);
    if (ObjectUtil.isNull(sysOss)) {
        throw new ServiceException("文件数据不存在!");
    }
    response.reset();
    FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
    long data;
    try {
        data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false);
    } catch (HttpException e) {
        if (e.getMessage().contains("403")) {
            throw new ServiceException("无读取权限, 请在对应的OSS开启'公有读'权限!");
        } else {
            throw new ServiceException(e.getMessage());
        }
    }
    response.setContentLength(Convert.toInt(data));
}
Also used : SysOss(com.ruoyi.system.domain.SysOss) ServiceException(com.ruoyi.common.exception.ServiceException) HttpException(cn.hutool.http.HttpException) SaCheckPermission(cn.dev33.satoken.annotation.SaCheckPermission)

Example 44 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class CreateAndUpdateMetaObjectHandler method insertFill.

@Override
public void insertFill(MetaObject metaObject) {
    try {
        if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
            Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime()) ? baseEntity.getCreateTime() : new Date();
            baseEntity.setCreateTime(current);
            baseEntity.setUpdateTime(current);
            String username = StringUtils.isNotBlank(baseEntity.getCreateBy()) ? baseEntity.getCreateBy() : getLoginUsername();
            // 当前已登录 且 创建人为空 则填充
            baseEntity.setCreateBy(username);
            // 当前已登录 且 更新人为空 则填充
            baseEntity.setUpdateBy(username);
        }
    } catch (Exception e) {
        throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) BaseEntity(com.ruoyi.common.core.domain.BaseEntity) Date(java.util.Date) ServiceException(com.ruoyi.common.exception.ServiceException)

Example 45 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class CreateAndUpdateMetaObjectHandler method updateFill.

@Override
public void updateFill(MetaObject metaObject) {
    try {
        if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
            Date current = new Date();
            // 更新时间填充(不管为不为空)
            baseEntity.setUpdateTime(current);
            String username = getLoginUsername();
            // 当前已登录 更新人填充(不管为不为空)
            if (StringUtils.isNotBlank(username)) {
                baseEntity.setUpdateBy(username);
            }
        }
    } catch (Exception e) {
        throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) BaseEntity(com.ruoyi.common.core.domain.BaseEntity) Date(java.util.Date) ServiceException(com.ruoyi.common.exception.ServiceException)

Aggregations

ServiceException (com.ruoyi.common.exception.ServiceException)109 IOException (java.io.IOException)21 Transactional (org.springframework.transaction.annotation.Transactional)21 GenTable (com.ruoyi.generator.domain.GenTable)12 StringWriter (java.io.StringWriter)12 Template (org.apache.velocity.Template)12 VelocityContext (org.apache.velocity.VelocityContext)12 SysRole (com.ruoyi.common.core.domain.entity.SysRole)10 File (java.io.File)10 SysUser (com.ruoyi.common.core.domain.entity.SysUser)9 GenTableColumn (com.ruoyi.generator.domain.GenTableColumn)8 Before (org.aspectj.lang.annotation.Before)8 JSONObject (com.alibaba.fastjson.JSONObject)6 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)6 SysDept (com.ruoyi.common.core.domain.entity.SysDept)6 LoginUser (com.ruoyi.common.core.domain.model.LoginUser)6 Constants (com.ruoyi.common.constant.Constants)5 GenConstants (com.ruoyi.common.constant.GenConstants)5 StringUtils (com.ruoyi.common.utils.StringUtils)5 SysOss (com.ruoyi.system.domain.SysOss)5