Browse Source

Fix tests

pull/14074/head
Andrii Landiak 11 months ago
parent
commit
c81121c32e
  1. 25
      application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponseHandler.java
  2. 6
      application/src/main/java/org/thingsboard/server/service/security/auth/jwt/JwtTokenAuthenticationProcessingFilter.java
  3. 7
      application/src/main/java/org/thingsboard/server/service/security/auth/rest/RestAwareAuthenticationFailureHandler.java

25
application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponseHandler.java

@ -137,23 +137,22 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
try { try {
response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setContentType(MediaType.APPLICATION_JSON_VALUE);
if (exception instanceof ThingsboardException) { if (exception instanceof ThingsboardException thingsboardException) {
ThingsboardException thingsboardException = (ThingsboardException) exception;
if (thingsboardException.getErrorCode() == ThingsboardErrorCode.SUBSCRIPTION_VIOLATION) { if (thingsboardException.getErrorCode() == ThingsboardErrorCode.SUBSCRIPTION_VIOLATION) {
handleSubscriptionException((ThingsboardException) exception, response); handleSubscriptionException(thingsboardException, response);
} else if (thingsboardException.getErrorCode() == ThingsboardErrorCode.DATABASE) { } else if (thingsboardException.getErrorCode() == ThingsboardErrorCode.DATABASE) {
handleDatabaseException(thingsboardException.getCause(), response); handleDatabaseException(thingsboardException.getCause(), response);
} else { } else {
handleThingsboardException((ThingsboardException) exception, response); handleThingsboardException(thingsboardException, response);
} }
} else if (exception instanceof TbRateLimitsException) { } else if (exception instanceof TbRateLimitsException rateLimitsException) {
handleRateLimitException(response, (TbRateLimitsException) exception); handleRateLimitException(response, rateLimitsException);
} else if (exception instanceof AccessDeniedException) { } else if (exception instanceof AccessDeniedException) {
handleAccessDeniedException(response); handleAccessDeniedException(response);
} else if (exception instanceof AuthenticationException) { } else if (exception instanceof AuthenticationException authenticationException) {
handleAuthenticationException((AuthenticationException) exception, response); handleAuthenticationException(authenticationException, response);
} else if (exception instanceof MaxPayloadSizeExceededException) { } else if (exception instanceof MaxPayloadSizeExceededException maxPayloadSizeExceededException) {
handleMaxPayloadSizeExceededException(response, (MaxPayloadSizeExceededException) exception); handleMaxPayloadSizeExceededException(response, maxPayloadSizeExceededException);
} else if (exception instanceof DataAccessException e) { } else if (exception instanceof DataAccessException e) {
handleDatabaseException(e, response); handleDatabaseException(e, response);
} else { } else {
@ -238,12 +237,10 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
JacksonUtil.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Token has expired", ThingsboardErrorCode.JWT_TOKEN_EXPIRED, HttpStatus.UNAUTHORIZED)); JacksonUtil.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Token has expired", ThingsboardErrorCode.JWT_TOKEN_EXPIRED, HttpStatus.UNAUTHORIZED));
} else if (authenticationException instanceof AuthMethodNotSupportedException) { } else if (authenticationException instanceof AuthMethodNotSupportedException) {
JacksonUtil.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); JacksonUtil.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
} else if (authenticationException instanceof UserPasswordExpiredException) { } else if (authenticationException instanceof UserPasswordExpiredException expiredException) {
UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException;
String resetToken = expiredException.getResetToken(); String resetToken = expiredException.getResetToken();
JacksonUtil.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken)); JacksonUtil.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken));
} else if (authenticationException instanceof UserPasswordNotValidException) { } else if (authenticationException instanceof UserPasswordNotValidException expiredException) {
UserPasswordNotValidException expiredException = (UserPasswordNotValidException) authenticationException;
JacksonUtil.writeValue(response.getWriter(), ThingsboardCredentialsViolationResponse.of(expiredException.getMessage())); JacksonUtil.writeValue(response.getWriter(), ThingsboardCredentialsViolationResponse.of(expiredException.getMessage()));
} else { } else {
JacksonUtil.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Authentication failed", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); JacksonUtil.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Authentication failed", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));

6
application/src/main/java/org/thingsboard/server/service/security/auth/jwt/JwtTokenAuthenticationProcessingFilter.java

@ -74,7 +74,11 @@ public class JwtTokenAuthenticationProcessingFilter extends AbstractAuthenticati
if (header == null) { if (header == null) {
header = request.getHeader(JWT_TOKEN_HEADER_PARAM_V2); header = request.getHeader(JWT_TOKEN_HEADER_PARAM_V2);
} }
return header != null && header.startsWith(BEARER_HEADER_PREFIX); if (header == null) {
// If there is NO auth header at all, let the JWT filter try to attempt Authentication and failure in the process.
return true;
}
return header.startsWith(BEARER_HEADER_PREFIX);
} }
@Override @Override

7
application/src/main/java/org/thingsboard/server/service/security/auth/rest/RestAwareAuthenticationFailureHandler.java

@ -15,7 +15,6 @@
*/ */
package org.thingsboard.server.service.security.auth.rest; package org.thingsboard.server.service.security.auth.rest;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -24,8 +23,6 @@ import org.springframework.security.web.authentication.AuthenticationFailureHand
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.thingsboard.server.exception.ThingsboardErrorResponseHandler; import org.thingsboard.server.exception.ThingsboardErrorResponseHandler;
import java.io.IOException;
@Component(value = "defaultAuthenticationFailureHandler") @Component(value = "defaultAuthenticationFailureHandler")
public class RestAwareAuthenticationFailureHandler implements AuthenticationFailureHandler { public class RestAwareAuthenticationFailureHandler implements AuthenticationFailureHandler {
@ -37,8 +34,8 @@ public class RestAwareAuthenticationFailureHandler implements AuthenticationFail
} }
@Override @Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
AuthenticationException e) throws IOException, ServletException {
errorResponseHandler.handle(e, response); errorResponseHandler.handle(e, response);
} }
} }

Loading…
Cancel
Save