java 无法自动连接,未找到“AuthenticationEntryPoint”类型的Bean

9rnv2umw  于 2023-01-01  发布在  Java
关注(0)|答案(2)|浏览(92)

当我自动连接除我创建的类之外的任何类时,我会收到这样的警告。
集成开发环境:智能J IDEA 2022.3.1
Spring Boot版本:2.7.4

    • 警告:**
Could not autowire. No beans of 'AuthenticationEntryPoint' type found.
    • 安全等级**
@Configuration
@EnableWebSecurity
public class SecurityConfig {
   
    @Autowired
    private AuthenticationEntryPoint authEntryPoint;

    @Bean
    public SecurityFilterChain configure(HttpSecurity http) throws Exception {
        .....
        return http.build();
    }
}
    • 我创建的类ComponentScan的配置类**
@Configuration
@ComponentScan(basePackages = {"com.example.security"})
public class SecurityConfiguration {}
    • Spring.工厂**
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.SecurityConfiguration

在我自己的班级里没有这个问题。但是在其他班级里有这个问题。我该怎么解决呢?

hyrbngr7

hyrbngr71#

SpringWeb是在包org.Springframework.Security.Web.Authentication中实现的authenticationEntryPoint类,但是它们还没有注册到组件中,所以不能使用@Autowired来自动注入。

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

或者尝试将您自己的AuthenticationEntryPoint类添加到项目中

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

}
pvcm50d1

pvcm50d12#

可以按如下方式使用构造函数注入:

@Configuration
@EnableWebSecurity
 public class SecurityConfig{

private AuthenticationEntryPoint authEntryPoint;

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

或者你使用Lombok岛

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
 public class SecurityConfig{

private final AuthenticationEntryPoint authEntryPoint;

}

相关问题