springsecuritybasic-authenticationuserdetailsservice

'org.springframework.security.core.userdetails.UserDetailsService' that could not be found


I am using spring security basic authentication where I'm getting the below error while application startup:

Parameter 2 of method authManager in com.spring.security.firstapp.security.MySecurityConfig required a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' in your configuration.

MySecurityConfig.java

@Configuration public class MySecurityConfig {

@Autowired
private MyAuthenticationProvider authenticationProvider;

@Bean
public AuthenticationManager authManager(HttpSecurity http, BCryptPasswordEncoder passwordEncoder,
        UserDetailsService userDetailService) throws Exception {
    return http.getSharedObject(AuthenticationManagerBuilder.class)
            .authenticationProvider(authenticationProvider)
            .build();
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.httpBasic();
    http.authorizeHttpRequests().anyRequest().authenticated();
    return http.build();

}



@Bean
public static BCryptPasswordEncoder bcryptPasswordEncoder() {
    return new BCryptPasswordEncoder();
}

}

MyAuthenticationProvider.java

@Component public class MyAuthenticationProvider implements AuthenticationProvider {

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String userName = authentication.getName();
    String passWord = authentication.getCredentials().toString();
    
    if("tom".equals(userName) && "abc".equals(passWord))
        return new UsernamePasswordAuthenticationToken(userName, passWord);
    else
        throw new BadCredentialsException("Invalid credentials!!");
    
}

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

}

Controller

@RestController public class HelloController {

@GetMapping("/hello")
public String hello() {
    return "Spring Boot Security";
}

}

pom.xml

4.0.0 org.springframework.boot spring-boot-starter-parent 3.0.4 com.first-app spring-security 0.0.1-SNAPSHOT spring-security spring-security <java.version>17</java.version> org.springframework.boot spring-boot-starter-security org.springframework.boot spring-boot-starter-web

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Solution

  • I added bean on class MySecurityConfig

    @Bean
    public InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) {
        UserDetails user = User.withUsername("user")
            .password(passwordEncoder.encode("password"))
            .roles("USER")
            .build();
    
        UserDetails admin = User.withUsername("admin")
            .password(passwordEncoder.encode("admin"))
            .roles("USER", "ADMIN")
            .build();
    
        return new InMemoryUserDetailsManager(user, admin);
    }
    

    After it run good.