Showing
5 changed files
with
37 additions
and
32 deletions
1 | package org.thingsboard.server.config.yunteng; | 1 | package org.thingsboard.server.config.yunteng; |
2 | + | ||
2 | import lombok.RequiredArgsConstructor; | 3 | import lombok.RequiredArgsConstructor; |
3 | import org.springframework.http.HttpStatus; | 4 | import org.springframework.http.HttpStatus; |
4 | import org.springframework.security.access.AccessDeniedException; | 5 | import org.springframework.security.access.AccessDeniedException; |
6 | +import org.springframework.security.authentication.AccountExpiredException; | ||
5 | import org.springframework.web.bind.MethodArgumentNotValidException; | 7 | import org.springframework.web.bind.MethodArgumentNotValidException; |
6 | import org.springframework.web.bind.annotation.ControllerAdvice; | 8 | import org.springframework.web.bind.annotation.ControllerAdvice; |
7 | import org.springframework.web.bind.annotation.ExceptionHandler; | 9 | import org.springframework.web.bind.annotation.ExceptionHandler; |
@@ -12,6 +14,7 @@ import org.thingsboard.server.exception.yunteng.YunTengErrorResponseHandler; | @@ -12,6 +14,7 @@ import org.thingsboard.server.exception.yunteng.YunTengErrorResponseHandler; | ||
12 | import javax.servlet.http.HttpServletRequest; | 14 | import javax.servlet.http.HttpServletRequest; |
13 | import javax.servlet.http.HttpServletResponse; | 15 | import javax.servlet.http.HttpServletResponse; |
14 | import java.util.Objects; | 16 | import java.util.Objects; |
17 | + | ||
15 | @ControllerAdvice(basePackages = "org.thingsboard.server.controller.yunteng") | 18 | @ControllerAdvice(basePackages = "org.thingsboard.server.controller.yunteng") |
16 | @RequiredArgsConstructor | 19 | @RequiredArgsConstructor |
17 | public class ControllerExceptionHandler { | 20 | public class ControllerExceptionHandler { |
@@ -20,65 +23,58 @@ public class ControllerExceptionHandler { | @@ -20,65 +23,58 @@ public class ControllerExceptionHandler { | ||
20 | 23 | ||
21 | @ExceptionHandler(YunTengException.class) | 24 | @ExceptionHandler(YunTengException.class) |
22 | public void handleYunTengException(YunTengException ex, HttpServletResponse response) { | 25 | public void handleYunTengException(YunTengException ex, HttpServletResponse response) { |
23 | - response.setCharacterEncoding("utf-8"); | ||
24 | errorResponseHandler.handle(ex, response); | 26 | errorResponseHandler.handle(ex, response); |
25 | } | 27 | } |
26 | 28 | ||
27 | @ExceptionHandler(MethodArgumentNotValidException.class) | 29 | @ExceptionHandler(MethodArgumentNotValidException.class) |
28 | public void handleMethodArgumentNotValidException( | 30 | public void handleMethodArgumentNotValidException( |
29 | - MethodArgumentNotValidException ex, HttpServletResponse response) { | ||
30 | - response.setCharacterEncoding("utf-8"); | 31 | + MethodArgumentNotValidException ex, HttpServletResponse response) { |
31 | errorResponseHandler.handle( | 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 | @ExceptionHandler(YtDataValidationException.class) | 40 | @ExceptionHandler(YtDataValidationException.class) |
40 | public void handleDataValidationException( | 41 | public void handleDataValidationException( |
41 | - YtDataValidationException ex, HttpServletRequest request, HttpServletResponse response) { | ||
42 | - response.setCharacterEncoding("utf-8"); | 42 | + YtDataValidationException ex, HttpServletRequest request, HttpServletResponse response) { |
43 | YunTengException YunTengException = | 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 | errorResponseHandler.handle(YunTengException, response); | 46 | errorResponseHandler.handle(YunTengException, response); |
47 | } | 47 | } |
48 | 48 | ||
49 | @ExceptionHandler(TooManyRequestException.class) | 49 | @ExceptionHandler(TooManyRequestException.class) |
50 | public void handleTooManyRequestException(HttpServletResponse response) { | 50 | public void handleTooManyRequestException(HttpServletResponse response) { |
51 | - response.setCharacterEncoding("utf-8"); | ||
52 | errorResponseHandler.handle( | 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 | @ExceptionHandler(AccessDeniedException.class) | 56 | @ExceptionHandler(AccessDeniedException.class) |
58 | public void handleAccessDeniedException(AccessDeniedException ex, HttpServletResponse response) { | 57 | public void handleAccessDeniedException(AccessDeniedException ex, HttpServletResponse response) { |
59 | - response.setCharacterEncoding("utf-8"); | ||
60 | errorResponseHandler.handle( | 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 | @ExceptionHandler(NoneTenantAssetException.class) | 64 | @ExceptionHandler(NoneTenantAssetException.class) |
67 | public void handleNoneTenantAssetException( | 65 | public void handleNoneTenantAssetException( |
68 | - NoneTenantAssetException ex, HttpServletResponse response) { | ||
69 | - response.setCharacterEncoding("utf-8"); | 66 | + NoneTenantAssetException ex, HttpServletResponse response) { |
70 | errorResponseHandler.handle( | 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 | @ExceptionHandler(EntityCreationException.class) | 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 | errorResponseHandler.handle( | 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,6 +25,7 @@ import org.springframework.http.MediaType; | ||
25 | import org.springframework.http.ResponseEntity; | 25 | import org.springframework.http.ResponseEntity; |
26 | import org.springframework.lang.Nullable; | 26 | import org.springframework.lang.Nullable; |
27 | import org.springframework.security.access.AccessDeniedException; | 27 | import org.springframework.security.access.AccessDeniedException; |
28 | +import org.springframework.security.authentication.AccountExpiredException; | ||
28 | import org.springframework.security.authentication.BadCredentialsException; | 29 | import org.springframework.security.authentication.BadCredentialsException; |
29 | import org.springframework.security.authentication.DisabledException; | 30 | import org.springframework.security.authentication.DisabledException; |
30 | import org.springframework.security.authentication.LockedException; | 31 | import org.springframework.security.authentication.LockedException; |
@@ -110,6 +111,7 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand | @@ -110,6 +111,7 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand | ||
110 | 111 | ||
111 | @ExceptionHandler(Exception.class) | 112 | @ExceptionHandler(Exception.class) |
112 | public void handle(Exception exception, HttpServletResponse response) { | 113 | public void handle(Exception exception, HttpServletResponse response) { |
114 | + response.setCharacterEncoding("utf-8"); | ||
113 | log.debug("Processing exception {}", exception.getMessage(), exception); | 115 | log.debug("Processing exception {}", exception.getMessage(), exception); |
114 | if (!response.isCommitted()) { | 116 | if (!response.isCommitted()) { |
115 | try { | 117 | try { |
@@ -197,7 +199,11 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand | @@ -197,7 +199,11 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand | ||
197 | UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException; | 199 | UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException; |
198 | String resetToken = expiredException.getResetToken(); | 200 | String resetToken = expiredException.getResetToken(); |
199 | mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken)); | 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 | mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Authentication failed", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); | 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,6 +50,7 @@ public class YunTengErrorResponseHandler implements AccessDeniedHandler { | ||
50 | log.debug("Processing exception {}", exception.getMessage(), exception); | 50 | log.debug("Processing exception {}", exception.getMessage(), exception); |
51 | if (!response.isCommitted()) { | 51 | if (!response.isCommitted()) { |
52 | try { | 52 | try { |
53 | + response.setCharacterEncoding("utf-8"); | ||
53 | response.setContentType(MediaType.APPLICATION_JSON_VALUE); | 54 | response.setContentType(MediaType.APPLICATION_JSON_VALUE); |
54 | 55 | ||
55 | if (exception instanceof AccessDeniedException) { | 56 | if (exception instanceof AccessDeniedException) { |
@@ -33,6 +33,7 @@ import org.thingsboard.server.common.data.security.Authority; | @@ -33,6 +33,7 @@ import org.thingsboard.server.common.data.security.Authority; | ||
33 | import org.thingsboard.server.common.data.security.UserCredentials; | 33 | import org.thingsboard.server.common.data.security.UserCredentials; |
34 | import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; | 34 | import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; |
35 | import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils; | 35 | import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils; |
36 | +import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; | ||
36 | import org.thingsboard.server.common.data.yunteng.core.utils.AccountProperties; | 37 | import org.thingsboard.server.common.data.yunteng.core.utils.AccountProperties; |
37 | import org.thingsboard.server.common.data.yunteng.dto.UserDetailRoleDTO; | 38 | import org.thingsboard.server.common.data.yunteng.dto.UserDetailRoleDTO; |
38 | import org.thingsboard.server.common.data.yunteng.dto.UserDetailsDTO; | 39 | import org.thingsboard.server.common.data.yunteng.dto.UserDetailsDTO; |
@@ -376,14 +377,14 @@ public class RestAuthenticationProvider implements AuthenticationProvider { | @@ -376,14 +377,14 @@ public class RestAuthenticationProvider implements AuthenticationProvider { | ||
376 | .ifPresent( | 377 | .ifPresent( |
377 | expireTime -> { | 378 | expireTime -> { |
378 | if (LocalDateTime.now().isAfter(expireTime)) { | 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 | Optional.ofNullable(detailsDTO.getAccountExpireTime()) | 383 | Optional.ofNullable(detailsDTO.getAccountExpireTime()) |
383 | .ifPresent( | 384 | .ifPresent( |
384 | expireTime -> { | 385 | expireTime -> { |
385 | if (LocalDateTime.now().isAfter(expireTime)) { | 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 | boolean enabled = detailsDTO.isEnabled(); | 390 | boolean enabled = detailsDTO.isEnabled(); |
@@ -44,6 +44,7 @@ public enum ErrorMessage { | @@ -44,6 +44,7 @@ public enum ErrorMessage { | ||
44 | PHONE_OR_EMAIL_HAS_REGISTER(400025,"手机或邮箱已被使用"), | 44 | PHONE_OR_EMAIL_HAS_REGISTER(400025,"手机或邮箱已被使用"), |
45 | CONTACT_ALREADY_ASSOCIATED(400026,"当前联系人已被设备配置关联"), | 45 | CONTACT_ALREADY_ASSOCIATED(400026,"当前联系人已被设备配置关联"), |
46 | MSG_CODE_NOT_MATCHED(400027,"验证码不正确"), | 46 | MSG_CODE_NOT_MATCHED(400027,"验证码不正确"), |
47 | + ACCOUNT_HAS_EXPIRED(400028,"账号已过期,请联系管理员"), | ||
47 | HAVE_NO_PERMISSION(500002,"没有修改权限"); | 48 | HAVE_NO_PERMISSION(500002,"没有修改权限"); |
48 | private final int code; | 49 | private final int code; |
49 | private String message; | 50 | private String message; |