Hi.
NOTE: UPDATED CODE IS on the link:
https://github.com/omudzingwa/jwt-security/
First, its necessary to encrypt the password.
Second: I updated my code using your recommendation and it didnt work
Tthirdly. Just a while back I was now able to access protected URL's when I changed my code for OurUserDetailsService as:
From:
public UserDetails loadUserByUsername(
String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username)
.orElseThrow(()-> new UsernameNotFoundException("OurUserDetailsService-: Username not found"));
Set<GrantedAuthority> authorities = new HashSet<>();
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
authorities);
}
To:
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return userRepository.findByUsername(username)
.map(this::createUserDetails)
.orElseThrow(()->new UsernameNotFoundException("Username not found"));
}
private UserDetails createUserDetails(com.netrork.pine.security.users.User user) {
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
user.getAuthorities());
}
But then I later added some flesh to my CustomLogoutHandler as below and then everything went hay wire and I was failing to access protected URL's again. The CustomLogoutHandler which implements LogoutHandler is as below:
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
String token = request.getHeader("refresh_token");
log.info("Length for Token is : " + token.length());
log.info("Fetching token from Database");
String tokenFromDatabase = refreshTokenService.findTokenByValue(token)
.orElseThrow(()-> new RuntimeException("Token not found"));
log.info("Token from Database is : " + tokenFromDatabase);
log.info("Token from Request is : " + token);
//1 - Check if token exists in database
if(token.equals(tokenFromDatabase)){
long userId=refreshTokenService.findUserIdFromTokenByTokenValue(token);
String username= refreshTokenService.getUsernameForTokenByUserId(userId);
refreshTokenService.deleteTokenByTokenValue(token);
log.info("Deleted Refresh token for user : " + username);
SecurityContextHolder.clearContext();
try {
request.logout();
log.info("Logged out User : " + username);
} catch (ServletException e) {
throw new RuntimeException(e);
}
NOTE: UPDATED CODE IS on the link:
https://github.com/omudzingwa/jwt-security/