Commit 5f21f8be4f6c7c48954c616429873c30274eea38

Authored by 黄 x
1 parent 03f166ec

fix: 登录时,租户过期或者账号过期返回正确的提示信息

1 1 package org.thingsboard.server.config.yunteng;
  2 +
2 3 import lombok.RequiredArgsConstructor;
3 4 import org.springframework.http.HttpStatus;
4 5 import org.springframework.security.access.AccessDeniedException;
  6 +import org.springframework.security.authentication.AccountExpiredException;
5 7 import org.springframework.web.bind.MethodArgumentNotValidException;
6 8 import org.springframework.web.bind.annotation.ControllerAdvice;
7 9 import org.springframework.web.bind.annotation.ExceptionHandler;
... ... @@ -12,6 +14,7 @@ import org.thingsboard.server.exception.yunteng.YunTengErrorResponseHandler;
12 14 import javax.servlet.http.HttpServletRequest;
13 15 import javax.servlet.http.HttpServletResponse;
14 16 import java.util.Objects;
  17 +
15 18 @ControllerAdvice(basePackages = "org.thingsboard.server.controller.yunteng")
16 19 @RequiredArgsConstructor
17 20 public class ControllerExceptionHandler {
... ... @@ -20,65 +23,58 @@ public class ControllerExceptionHandler {
20 23
21 24 @ExceptionHandler(YunTengException.class)
22 25 public void handleYunTengException(YunTengException ex, HttpServletResponse response) {
23   - response.setCharacterEncoding("utf-8");
24 26 errorResponseHandler.handle(ex, response);
25 27 }
26 28
27 29 @ExceptionHandler(MethodArgumentNotValidException.class)
28 30 public void handleMethodArgumentNotValidException(
29   - MethodArgumentNotValidException ex, HttpServletResponse response) {
30   - response.setCharacterEncoding("utf-8");
  31 + MethodArgumentNotValidException ex, HttpServletResponse response) {
31 32 errorResponseHandler.handle(
32   - new YunTengException(
33   - ErrorMessage.INVALID_PARAMETER.setMessage(
34   - Objects.requireNonNull(ex.getBindingResult().getFieldError()).getDefaultMessage()),
35   - HttpStatus.BAD_REQUEST),
36   - response);
  33 + new YunTengException(
  34 + ErrorMessage.INVALID_PARAMETER.setMessage(
  35 + Objects.requireNonNull(ex.getBindingResult().getFieldError()).getDefaultMessage()),
  36 + HttpStatus.BAD_REQUEST),
  37 + response);
37 38 }
38 39
39 40 @ExceptionHandler(YtDataValidationException.class)
40 41 public void handleDataValidationException(
41   - YtDataValidationException ex, HttpServletRequest request, HttpServletResponse response) {
42   - response.setCharacterEncoding("utf-8");
  42 + YtDataValidationException ex, HttpServletRequest request, HttpServletResponse response) {
43 43 YunTengException YunTengException =
44   - new YunTengException(
45   - ErrorMessage.BAD_PARAMETER.setMessage(ex.getMessage()), HttpStatus.BAD_REQUEST);
  44 + new YunTengException(
  45 + ErrorMessage.BAD_PARAMETER.setMessage(ex.getMessage()), HttpStatus.BAD_REQUEST);
46 46 errorResponseHandler.handle(YunTengException, response);
47 47 }
48 48
49 49 @ExceptionHandler(TooManyRequestException.class)
50 50 public void handleTooManyRequestException(HttpServletResponse response) {
51   - response.setCharacterEncoding("utf-8");
52 51 errorResponseHandler.handle(
53   - new YunTengException(ErrorMessage.TOO_MANY_REQUEST, HttpStatus.TOO_MANY_REQUESTS),
54   - response);
  52 + new YunTengException(ErrorMessage.TOO_MANY_REQUEST, HttpStatus.TOO_MANY_REQUESTS),
  53 + response);
55 54 }
56 55
57 56 @ExceptionHandler(AccessDeniedException.class)
58 57 public void handleAccessDeniedException(AccessDeniedException ex, HttpServletResponse response) {
59   - response.setCharacterEncoding("utf-8");
60 58 errorResponseHandler.handle(
61   - new YunTengException(
62   - ErrorMessage.ACCESS_DENIED.setMessage(ex.getMessage()), HttpStatus.FORBIDDEN),
63   - response);
  59 + new YunTengException(
  60 + ErrorMessage.ACCESS_DENIED.setMessage(ex.getMessage()), HttpStatus.FORBIDDEN),
  61 + response);
64 62 }
65 63
66 64 @ExceptionHandler(NoneTenantAssetException.class)
67 65 public void handleNoneTenantAssetException(
68   - NoneTenantAssetException ex, HttpServletResponse response) {
69   - response.setCharacterEncoding("utf-8");
  66 + NoneTenantAssetException ex, HttpServletResponse response) {
70 67 errorResponseHandler.handle(
71   - new YunTengException(
72   - ErrorMessage.NONE_TENANT_ASSET.setMessage(ex.getMessage()), HttpStatus.NOT_FOUND),
73   - response);
  68 + new YunTengException(
  69 + ErrorMessage.NONE_TENANT_ASSET.setMessage(ex.getMessage()), HttpStatus.NOT_FOUND),
  70 + response);
74 71 }
75 72
76 73 @ExceptionHandler(EntityCreationException.class)
77   - public void handleEntityCreationException(
78   - EntityCreationException ex, HttpServletResponse response) {
79   - response.setCharacterEncoding("utf-8");
  74 + public void handleEntityCreationException(HttpServletResponse response) {
80 75 errorResponseHandler.handle(
81   - new YunTengException(ErrorMessage.SEND_DESTINATION_NOT_FOUND, HttpStatus.PRECONDITION_FAILED),
82   - response);
  76 + new YunTengException(
  77 + ErrorMessage.SEND_DESTINATION_NOT_FOUND, HttpStatus.PRECONDITION_FAILED),
  78 + response);
83 79 }
84 80 }
... ...
... ... @@ -25,6 +25,7 @@ import org.springframework.http.MediaType;
25 25 import org.springframework.http.ResponseEntity;
26 26 import org.springframework.lang.Nullable;
27 27 import org.springframework.security.access.AccessDeniedException;
  28 +import org.springframework.security.authentication.AccountExpiredException;
28 29 import org.springframework.security.authentication.BadCredentialsException;
29 30 import org.springframework.security.authentication.DisabledException;
30 31 import org.springframework.security.authentication.LockedException;
... ... @@ -110,6 +111,7 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
110 111
111 112 @ExceptionHandler(Exception.class)
112 113 public void handle(Exception exception, HttpServletResponse response) {
  114 + response.setCharacterEncoding("utf-8");
113 115 log.debug("Processing exception {}", exception.getMessage(), exception);
114 116 if (!response.isCommitted()) {
115 117 try {
... ... @@ -197,7 +199,11 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
197 199 UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException;
198 200 String resetToken = expiredException.getResetToken();
199 201 mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken));
200   - } else {
  202 + }else if(authenticationException instanceof AccountExpiredException) {
  203 + AccountExpiredException expiredException = (AccountExpiredException) authenticationException;
  204 + mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(),ThingsboardErrorCode.AUTHENTICATION, HttpStatus.FORBIDDEN));
  205 + }
  206 + else {
201 207 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Authentication failed", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
202 208 }
203 209 }
... ...
... ... @@ -50,6 +50,7 @@ public class YunTengErrorResponseHandler implements AccessDeniedHandler {
50 50 log.debug("Processing exception {}", exception.getMessage(), exception);
51 51 if (!response.isCommitted()) {
52 52 try {
  53 + response.setCharacterEncoding("utf-8");
53 54 response.setContentType(MediaType.APPLICATION_JSON_VALUE);
54 55
55 56 if (exception instanceof AccessDeniedException) {
... ...
... ... @@ -33,6 +33,7 @@ import org.thingsboard.server.common.data.security.Authority;
33 33 import org.thingsboard.server.common.data.security.UserCredentials;
34 34 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
35 35 import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils;
  36 +import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
36 37 import org.thingsboard.server.common.data.yunteng.core.utils.AccountProperties;
37 38 import org.thingsboard.server.common.data.yunteng.dto.UserDetailRoleDTO;
38 39 import org.thingsboard.server.common.data.yunteng.dto.UserDetailsDTO;
... ... @@ -376,14 +377,14 @@ public class RestAuthenticationProvider implements AuthenticationProvider {
376 377 .ifPresent(
377 378 expireTime -> {
378 379 if (LocalDateTime.now().isAfter(expireTime)) {
379   - throw new AccountExpiredException("tenant has expired");
  380 + throw new AccountExpiredException(ErrorMessage.ACCOUNT_HAS_EXPIRED.getMessage());
380 381 }
381 382 });
382 383 Optional.ofNullable(detailsDTO.getAccountExpireTime())
383 384 .ifPresent(
384 385 expireTime -> {
385 386 if (LocalDateTime.now().isAfter(expireTime)) {
386   - throw new AccountExpiredException("user account has expired");
  387 + throw new AccountExpiredException(ErrorMessage.ACCOUNT_HAS_EXPIRED.getMessage());
387 388 }
388 389 });
389 390 boolean enabled = detailsDTO.isEnabled();
... ...
... ... @@ -44,6 +44,7 @@ public enum ErrorMessage {
44 44 PHONE_OR_EMAIL_HAS_REGISTER(400025,"手机或邮箱已被使用"),
45 45 CONTACT_ALREADY_ASSOCIATED(400026,"当前联系人已被设备配置关联"),
46 46 MSG_CODE_NOT_MATCHED(400027,"验证码不正确"),
  47 + ACCOUNT_HAS_EXPIRED(400028,"账号已过期,请联系管理员"),
47 48 HAVE_NO_PERMISSION(500002,"没有修改权限");
48 49 private final int code;
49 50 private String message;
... ...