|
|
|
@ -26,8 +26,10 @@ import org.springframework.security.authentication.DisabledException; |
|
|
|
import org.springframework.security.authentication.LockedException; |
|
|
|
import org.springframework.security.core.AuthenticationException; |
|
|
|
import org.springframework.security.web.access.AccessDeniedHandler; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler; |
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice; |
|
|
|
import org.springframework.web.client.HttpClientErrorException; |
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; |
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
|
|
import org.thingsboard.server.common.msg.tools.TbRateLimitsException; |
|
|
|
@ -40,14 +42,15 @@ import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
@Component |
|
|
|
@Slf4j |
|
|
|
public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { |
|
|
|
@RestControllerAdvice |
|
|
|
public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ObjectMapper mapper; |
|
|
|
|
|
|
|
@Override |
|
|
|
@ExceptionHandler(AccessDeniedException.class) |
|
|
|
public void handle(HttpServletRequest request, HttpServletResponse response, |
|
|
|
AccessDeniedException accessDeniedException) throws IOException, |
|
|
|
ServletException { |
|
|
|
@ -60,6 +63,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(Exception.class) |
|
|
|
public void handle(Exception exception, HttpServletResponse response) { |
|
|
|
log.debug("Processing exception {}", exception.getMessage(), exception); |
|
|
|
if (!response.isCommitted()) { |
|
|
|
@ -159,7 +163,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { |
|
|
|
} else if (authenticationException instanceof AuthMethodNotSupportedException) { |
|
|
|
mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); |
|
|
|
} else if (authenticationException instanceof UserPasswordExpiredException) { |
|
|
|
UserPasswordExpiredException expiredException = (UserPasswordExpiredException)authenticationException; |
|
|
|
UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException; |
|
|
|
String resetToken = expiredException.getResetToken(); |
|
|
|
mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken)); |
|
|
|
} else { |
|
|
|
|