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?
CodePudding user response:
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....
}
}
CodePudding user response:
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;
}