Skip to content

Commit

Permalink
[#550] Provide messageArguments and messageIf for UsernameNotAvailabl…
Browse files Browse the repository at this point in the history
…eException and BadCredentialsException
  • Loading branch information
palagdan committed Sep 17, 2024
1 parent 1fb2883 commit 6753bfb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,16 @@ public ErrorInfo handleEvaluationException(HttpServletRequest request, Calculati
.requestUri(request.getRequestURI())
.build();
}

@ExceptionHandler(UsernameNotAvailableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorInfo handleUsernameNotAvailableException(HttpServletRequest request, UsernameNotAvailableException e) {
log.warn("> handleUsernameNotAvailableException - {}", request.getRequestURI());
return ErrorInfo.builder()
.message(e.getMessage())
.requestUri(request.getRequestURI())
.messageId(e.getMessageId())
.messageArguments(e.getMessageArguments())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cz.cvut.kbss.analysis.exception;

import lombok.Getter;
import lombok.Setter;

import java.util.Map;

@Getter
@Setter
public class BadCredentialsException extends org.springframework.security.authentication.BadCredentialsException {

private final String messageId;
private Map<String, String> messageArguments;

public BadCredentialsException(String message) {
super(message);
this.messageId = null;
}

public BadCredentialsException(String message, Throwable cause) {
super(message, cause);
this.messageId = null;
}

public BadCredentialsException(String messageId, String message, Map<String, String> args){
super(message);
this.messageId = messageId;
messageArguments = args;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
package cz.cvut.kbss.analysis.exception;

import lombok.Getter;
import lombok.Setter;

import java.util.Map;

@Getter
@Setter
public class UsernameNotAvailableException extends RuntimeException {

private final String messageId;
private Map<String, String> messageArguments;

public UsernameNotAvailableException(String message) {
super(message);
messageId = null;
}

public UsernameNotAvailableException(String messageId, String message, Map<String, String> args){
super(message);
this.messageId = messageId;
messageArguments = args;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cz.cvut.kbss.analysis.security;

import cz.cvut.kbss.analysis.exception.BadCredentialsException;
import cz.cvut.kbss.analysis.service.security.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
Expand All @@ -12,6 +12,8 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* The class is used for local authentication instead of OAuth2.
*/
Expand All @@ -38,7 +40,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
final String password = (String) authentication.getCredentials();
if (!passwordEncoder.matches(password, userDetails.getPassword())) {
log.trace("Provided password for username '{}' doesn't match.", username);
throw new BadCredentialsException("Provided password for username '" + username + "' doesn't match.");
throw new BadCredentialsException("error.user.passwordMismatch" ,"Provided password for username '" + username + "' doesn't match.", Map.of("username", username));
}
return SecurityUtils.setCurrentUser(userDetails);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.net.URI;
import java.util.Map;
import java.util.Optional;

@Slf4j
Expand Down Expand Up @@ -47,7 +48,7 @@ protected GenericDao<User> getPrimaryDao() {
public URI register(User user) {
if (userDao.existsWithUsername(user.getUsername())) {
log.warn("User with username {} already exists", user.getUsername());
throw new UsernameNotAvailableException("Username '" + user.getUsername() + "' has already been used.");
throw new UsernameNotAvailableException( "error.user.usernameNotAvailable","Username '" + user.getUsername() + "' has already been used.", Map.of("username", user.getUsername()));
}

user.setPassword(passwordEncoder.encode(user.getPassword()));
Expand Down

0 comments on commit 6753bfb

Please sign in to comment.