Search in sources :

Example 26 with BadRequestException

use of run.halo.app.exception.BadRequestException in project halo by halo-dev.

the class AdminServiceImpl method refreshToken.

@Override
@NonNull
public AuthToken refreshToken(@NonNull String refreshToken) {
    Assert.hasText(refreshToken, "Refresh token must not be blank");
    Integer userId = cacheStore.getAny(SecurityUtils.buildTokenRefreshKey(refreshToken), Integer.class).orElseThrow(() -> new BadRequestException("登录状态已失效,请重新登录").setErrorData(refreshToken));
    // Get user info
    User user = userService.getById(userId);
    // Remove all token
    cacheStore.getAny(SecurityUtils.buildAccessTokenKey(user), String.class).ifPresent(accessToken -> cacheStore.delete(SecurityUtils.buildTokenAccessKey(accessToken)));
    cacheStore.delete(SecurityUtils.buildTokenRefreshKey(refreshToken));
    cacheStore.delete(SecurityUtils.buildAccessTokenKey(user));
    cacheStore.delete(SecurityUtils.buildRefreshTokenKey(user));
    return buildAuthToken(user);
}
Also used : User(run.halo.app.model.entity.User) BadRequestException(run.halo.app.exception.BadRequestException) NonNull(org.springframework.lang.NonNull)

Example 27 with BadRequestException

use of run.halo.app.exception.BadRequestException in project halo by halo-dev.

the class AdminServiceImpl method clearToken.

@Override
public void clearToken() {
    // Check if the current is logging in
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        throw new BadRequestException("您尚未登录,因此无法注销");
    }
    // Get current user
    User user = authentication.getDetail().getUser();
    // Clear access token
    cacheStore.getAny(SecurityUtils.buildAccessTokenKey(user), String.class).ifPresent(accessToken -> {
        // Delete token
        cacheStore.delete(SecurityUtils.buildTokenAccessKey(accessToken));
        cacheStore.delete(SecurityUtils.buildAccessTokenKey(user));
    });
    // Clear refresh token
    cacheStore.getAny(SecurityUtils.buildRefreshTokenKey(user), String.class).ifPresent(refreshToken -> {
        cacheStore.delete(SecurityUtils.buildTokenRefreshKey(refreshToken));
        cacheStore.delete(SecurityUtils.buildRefreshTokenKey(user));
    });
    eventPublisher.publishEvent(new LogEvent(this, user.getUsername(), LogType.LOGGED_OUT, user.getNickname()));
    log.info("You have been logged out, looking forward to your next visit!");
}
Also used : User(run.halo.app.model.entity.User) LogEvent(run.halo.app.event.logger.LogEvent) Authentication(run.halo.app.security.authentication.Authentication) BadRequestException(run.halo.app.exception.BadRequestException)

Example 28 with BadRequestException

use of run.halo.app.exception.BadRequestException in project halo by halo-dev.

the class TwoFactorAuthUtils method validateTFACode.

public static void validateTFACode(String tfaKey, String tfaCode) {
    try {
        int validCode = Integer.parseInt(tfaCode);
        boolean result = TimeBasedOneTimePasswordUtil.validateCurrentNumber(tfaKey, validCode, VALID_TFA_WINDOW_MILLIS);
        if (!result) {
            throw new BadRequestException("两步验证码验证错误,请确认时间是否同步");
        }
    } catch (NumberFormatException e) {
        throw new BadRequestException("两步验证码请输入数字");
    } catch (GeneralSecurityException e) {
        throw new BadRequestException("两步验证码验证异常");
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) BadRequestException(run.halo.app.exception.BadRequestException)

Example 29 with BadRequestException

use of run.halo.app.exception.BadRequestException in project halo-plugin-experimental by guqing.

the class UserServiceImpl method create.

@Override
@CacheLock
public User create(User user) {
    // Check user
    if (count() != 0) {
        throw new BadRequestException("当前博客已有用户");
    }
    User createdUser = super.create(user);
    eventPublisher.publishEvent(new UserUpdatedEvent(this, createdUser.getId()));
    return createdUser;
}
Also used : User(run.halo.app.model.entity.User) BadRequestException(run.halo.app.exception.BadRequestException) UserUpdatedEvent(run.halo.app.event.user.UserUpdatedEvent) CacheLock(run.halo.app.cache.lock.CacheLock)

Example 30 with BadRequestException

use of run.halo.app.exception.BadRequestException in project halo-plugin-experimental by guqing.

the class UserServiceImpl method updatePassword.

@Override
public User updatePassword(String oldPassword, String newPassword, Integer userId) {
    Assert.hasText(oldPassword, "Old password must not be blank");
    Assert.hasText(newPassword, "New password must not be blank");
    Assert.notNull(userId, "User id must not be blank");
    if (oldPassword.equals(newPassword)) {
        throw new BadRequestException("新密码和旧密码不能相同");
    }
    // Get the user
    User user = getById(userId);
    // Check the user old password
    if (!BCrypt.checkpw(oldPassword, user.getPassword())) {
        throw new BadRequestException("旧密码错误").setErrorData(oldPassword);
    }
    // Set new password
    setPassword(user, newPassword);
    // Update this user
    User updatedUser = update(user);
    // Log it
    eventPublisher.publishEvent(new LogEvent(this, updatedUser.getId().toString(), LogType.PASSWORD_UPDATED, HaloUtils.desensitize(oldPassword, 2, 1)));
    return updatedUser;
}
Also used : User(run.halo.app.model.entity.User) LogEvent(run.halo.app.event.logger.LogEvent) BadRequestException(run.halo.app.exception.BadRequestException)

Aggregations

BadRequestException (run.halo.app.exception.BadRequestException)41 User (run.halo.app.model.entity.User)26 LogEvent (run.halo.app.event.logger.LogEvent)15 NonNull (org.springframework.lang.NonNull)12 NotFoundException (run.halo.app.exception.NotFoundException)9 IOException (java.io.IOException)6 CacheLock (run.halo.app.cache.lock.CacheLock)6 ForbiddenException (run.halo.app.exception.ForbiddenException)6 ServiceException (run.halo.app.exception.ServiceException)6 Authentication (run.halo.app.security.authentication.Authentication)6 Transactional (org.springframework.transaction.annotation.Transactional)5 ThemeUpdatedEvent (run.halo.app.event.theme.ThemeUpdatedEvent)5 Category (run.halo.app.model.entity.Category)5 ApiOperation (io.swagger.annotations.ApiOperation)3 Path (java.nio.file.Path)3 GeneralSecurityException (java.security.GeneralSecurityException)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 UserUpdatedEvent (run.halo.app.event.user.UserUpdatedEvent)3 ThemeNotFoundException (run.halo.app.exception.ThemeNotFoundException)3