Error "The method withDefaultPasswordEncoder() is undefined for the type User" Spring Boot password

KeithNolo :

I am trying to include a username and password into my spring application. I was reading the spring docs and it said to put in the below code to implement this. I am getting the following errors (** The method withDefaultPasswordEncoder() is undefined for the type User) on the UserDetails Service**.)

I have looked at the docs and tried some other forums to try and solve this but I can't find anything that works.
Please see the image below for the errors as well as the pom.

Any help would be much appreciated,

Thanks in advance!!!

Code inside my Security package Pom File

Dmitrii Cheremisin :

Remove your method userDetailsService. You need to override method from WebSecurityConfigurerAdapter:

protected void configure(AuthenticationManagerBuilder auth) throws Exception {..}

For example:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Bean
public PasswordEncoder getPasswordEncoder() {
    return NoOpPasswordEncoder.getInstance();
}

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

    auth.inMemoryAuthentication()
        .withUser("user1")
        .password("password1")
        .roles("ADMIN")
        .and()
        .withUser("user2")
        .password("password1")
        .roles("USER");
}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests()
        .antMatchers("/user").hasAnyRole("ADMIN", "USER")
        .antMatchers("/admin").hasRole("ADMIN")
        .antMatchers("/").permitAll()
        .and()
        .formLogin();
}

}

You can check my repo: https://github.com/dmcheremisin/SpringBootSecurity/tree/master/basic.security

Additionally, good article(similar to my code): https://www.baeldung.com/java-config-spring-security

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related