
net.smartcosmos.cluster.auth.SmartCosmosAuthenticationProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smartcosmos-auth-server Show documentation
Show all versions of smartcosmos-auth-server Show documentation
SMART COSMOS Authorization Server handles authentication throughout the microservice fleet
The newest version!
package net.smartcosmos.cluster.auth;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.boot.json.JsonParser;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import net.smartcosmos.cluster.auth.domain.UserResponse;
import net.smartcosmos.security.SecurityResourceProperties;
import net.smartcosmos.security.user.SmartCosmosCachedUser;
import static org.apache.commons.lang.StringUtils.defaultIfBlank;
import static org.apache.commons.lang.StringUtils.isNotBlank;
@Slf4j
@Service
@Profile("!test")
@EnableConfigurationProperties({ SecurityResourceProperties.class })
public class SmartCosmosAuthenticationProvider
extends AbstractUserDetailsAuthenticationProvider implements UserDetailsService {
public static final int MILLISECS_PER_SEC = 1000;
private final PasswordEncoder passwordEncoder;
private final Map users = new HashMap<>();
private String userDetailsServerLocationUri;
private RestTemplate restTemplate;
private Integer cachedUserKeepAliveSecs;
@Autowired
public SmartCosmosAuthenticationProvider(
SecurityResourceProperties securityResourceProperties,
PasswordEncoder passwordEncoder,
@Qualifier("userDetailsRestTemplate") RestTemplate restTemplate) {
this.passwordEncoder = passwordEncoder;
this.restTemplate = restTemplate;
this.cachedUserKeepAliveSecs = securityResourceProperties.getCachedUserKeepAliveSecs();
this.userDetailsServerLocationUri = securityResourceProperties.getUserDetails()
.getServer()
.getLocationUri();
}
/**
* This is where the password is actually checked for caching purposes. Assuming the
* same password encoder was used on both the user details service and here, this will
* avoid another round trip for authentication.
*
* @param userDetails the recently retrieved or previously cached details.
* @param authentication the presented token for authentication
* @throws AuthenticationException failure to authenticate.
*/
@Override
protected void additionalAuthenticationChecks(
UserDetails userDetails,
UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
String username = userDetails.getUsername() != null ? userDetails.getUsername() : "(NULL)";
if (authentication.getCredentials() == null) {
log.debug("Authentication failed for user {}: no credentials provided", username);
throw new BadCredentialsException(messages.getMessage(
"AbstractUserDetailsAuthenticationProvider.badCredentials",
"Bad credentials"));
}
String presentedPassword = authentication.getCredentials()
.toString();
if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
log.debug("Authentication failed for user {}: password does not match stored value", username);
throw new BadCredentialsException(messages.getMessage(
"AbstractUserDetailsAuthenticationProvider.badCredentials",
"Bad credentials"));
}
}
protected UserResponse fetchUser(String username, UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException, OAuth2Exception {
try {
if (authentication != null) {
// Authenticating the user.
UserResponse response = restTemplate.exchange(userDetailsServerLocationUri + "/authenticate",
HttpMethod.POST, new HttpEntity
© 2015 - 2025 Weber Informatics LLC | Privacy Policy