Reputation: 2203
i'm trying to build a Restful API with Spring-boot and oauth2 using hibernate.
i've made a CustomAuthenticationProvider to authenticate users from a database but im having the following response of the server
NOTE: json response.
error": "unauthorized",
"error_description": "No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken"
}
here is my CustomAuthenticationProvider:
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
public CustomAuthenticationProvider() {
super();
}
@Autowired
private UsuarioDAO user;
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
String username = String.valueOf(auth.getName());
String password = String.valueOf(auth.getCredentials().toString());
Usuarios us = null;
try {
us = user.userAuthentication(username, password);
} catch (Exception ex) {
}
if (us != null) {
final List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
final UserDetails principal = new User(username, password, grantedAuths);
final Authentication authentication = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths);
us = null;
return authentication;
} else {
throw new BadCredentialsException("Bad Credentials");
}
}
@Override
public boolean supports(Class<? extends Object> authentication) {
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
}
Here is my WebSecurityConfiguration:
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
Upvotes: 3
Views: 10581
Reputation: 44515
You have to configure Spring Security to use your custom authentication provider. Add the following code to your WebSecurityConfiguration class:
@Autowired
CustomAuthenticationProvider customAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthenticationProvider);
}
Upvotes: 5