iskyforce
iskyforce

Reputation: 37

spring auth error No AuthenticationProvider

I'm trying to build auth system with Spring Boot Security. So I have custom auth provider (without @Component annotation)

public class CustomAuthProvider extends DaoAuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {

        String name = authentication.getName();
        String password = authentication.getCredentials().toString();

        if (authentication.isAuthenticated()) {
            return authentication;
        }

        if ("user".equals(name) && "password".equals(password)) {

            return new UsernamePasswordAuthenticationToken(
                    name, password, new ArrayList<GrantedAuthority>(Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"))));
        } else {
            return null;
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
    }
}

which declared as a bean

@Bean
    public DaoAuthenticationProvider authProvider() {
        final CustomAuthProvider authProvider = new CustomAuthProvider();
        authProvider.setUserDetailsService(userDetailsService);
        return authProvider;
    }

Here is configuration:

@Override
    protected void configure(
            AuthenticationManagerBuilder auth) throws Exception {

        auth.authenticationProvider(authProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/anonymous*").anonymous()
                .antMatchers("/login*").permitAll()
                .antMatchers("/user/registration*").permitAll()
                .anyRequest().authenticated()
                .and().formLogin()
                .loginProcessingUrl("/login");
    }

When I'm trying post query to localhost:8080/login I'm getting sign-in form with message

No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken

Upvotes: 2

Views: 6846

Answers (1)

stacker
stacker

Reputation: 4475

spring's default request parameters for authentication are: username and password you are sending email instead of username so it throws an exception somewhere in your authentication manager.

if you want to override the default values you can simply specify that in your HTTP security configs:

.formLogin()
    .loginProcessingUrl("/login") 
    .usernameParameter("email")
    .passwordParameter("password")

Upvotes: 1

Related Questions