简体   繁体   中英

Could not autowire. No beans of 'AuthenticationEntryPoint' type found

I get an warning like this when I Autowired any class except the classes I created.

IDE: IntelliJ IDEA 2022.3.1

Spring boot version: 2.7.4

Warning:

Could not autowire. No beans of 'AuthenticationEntryPoint' type found. 

Security class

@Configuration
@EnableWebSecurity
public class SecurityConfig {
   
    @Autowired
    private AuthenticationEntryPoint authEntryPoint;


    @Bean
    public SecurityFilterChain configure(HttpSecurity http) throws Exception {
        .....
        return http.build();
    }
}

Configuration class for My Created Class ComponentScan

@Configuration
@ComponentScan(basePackages = {"com.example.security"})
public class SecurityConfiguration {}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.SecurityConfiguration

There is no this problem in my own classes. but there is this problem in other classes. How can i solve?

SpringWeb was implemented AuthenticationEntryPoint class in package org.Springframework.Security.Web.Authentication , but they have not been registered for a component, So you can't use @Autowired to auto-inject. You can try writing its Bean generation function, for example

@Bean
public LoginUrlAuthenticationEntryPoint instance(){
    return new LoginUrlAuthenticationEntryPoint("/login");
}

Or try add your own AuthenticationEntryPoint class to your project

@Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        //your commence here....
    }

}

you can use constructor injection as follows:

@Configuration
@EnableWebSecurity
 public class SecurityConfig{


private AuthenticationEntryPoint authEntryPoint;

public SecurityConfig(AuthenticationEntryPoint authEntryPoint) {
    this.authEntryPoint = authEntryPoint;
}
}

or of you use lombok:

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
 public class SecurityConfig{


private final AuthenticationEntryPoint authEntryPoint;


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM