com.formkiq.server.service.UserDetailsAuthenticationProvider Maven / Gradle / Ivy
package com.formkiq.server.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
/**
* Custom Authentication Provider.
*
*/
@Component
public class UserDetailsAuthenticationProvider extends
AbstractUserDetailsAuthenticationProvider {
/** UserService Instance. */
@Autowired
private UserService userservice;
@Override
protected void additionalAuthenticationChecks(final UserDetails userDetails,
final UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
// no additional authentication checks
}
@Override
protected UserDetails retrieveUser(final String username,
final UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
Authentication auth = SecurityContextHolder.getContext()
.getAuthentication();
String password = authentication.getCredentials() != null
? authentication.getCredentials().toString() : "";
if (auth != null) {
return this.userservice.findActiveUser(auth.getName(),
username, password);
}
try {
UserDetails details = this.userservice.findActiveUser(username,
password);
return details;
} catch (AuthenticationFailureException e) {
if (this.userservice.isUserExists(username)) {
throw e;
}
throw new BadCredentialsException("Invalid Client Id or Secret");
}
}
}