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);
}
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!");
}
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("两步验证码验证异常");
}
}
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;
}
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;
}
Aggregations