I have this code using the latter:
Optional<String> subject = Optional.ofNullable(claims.get().getSubject());
if (subject.isPresent()) {
UserDetails userDetails = userDetailsService.loadUserByUsername(subject.get());
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails,
null, userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
logger.debug("Security - The request authenticated fine from the JWT Access token");
return authentication;
} else {
throw new BadCredentialsException("The authentication token " + optToken + " did not contain a subject.");
}
I'm trying to refactor this using the ifPresent
method.
Should I have the userDetailsService.loadUserByUsername
service call before the functional method call ? If so, how to ? How to return an object of type different than the functional method type ?
I'm on Java 12.