构造函数的参数 0 需要找不到类型的 bean

Parameter 0 of constructor required a bean of type that could not be found

我想要一个 SSO CAS 身份验证,我已经按照 Bealdung 教程(https://www.baeldung.com/spring-security-cas-sso 第 4 部分)的说明进行操作,但是当我 运行 作为 spring 启动应用程序时,我出现此错误

Parameter 0 of constructor in SecurityConfig required a bean of type 'org.springframework.security.cas.authentication.CasAuthenticationProvider' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.security.cas.authentication.CasAuthenticationProvider' in your configuration.

我在这里看到了同样的问题,但在其他 spring 的导入中,我尝试添加 @Bean 注释但没有更多结果


@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
 
    private AuthenticationProvider authenticationProvider;
    private AuthenticationEntryPoint authenticationEntryPoint;
    private SingleSignOutFilter singleSignOutFilter;
    private LogoutFilter logoutFilter;
 
    @Autowired
    public SecurityConfig(CasAuthenticationProvider casAuthenticationProvider, AuthenticationEntryPoint eP, LogoutFilter lF, SingleSignOutFilter ssF) {
        this.authenticationProvider = casAuthenticationProvider;
        this.authenticationEntryPoint = eP;
 
        this.logoutFilter = lF;
        this.singleSignOutFilter = ssF;
 
    }
     
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http
        .authorizeRequests()
        .regexMatchers("/secured.*", "/login")
        .authenticated()
        .and()
        .authorizeRequests()
        .regexMatchers("/")
        .permitAll()
        .and()
        .httpBasic()
        .authenticationEntryPoint(authenticationEntryPoint);
    }
 
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.authenticationProvider(authenticationProvider);
    }
 
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
      return new ProviderManager(Arrays.asList(authenticationProvider));
    }
 
    @Bean
    public CasAuthenticationFilter casAuthenticationFilter(ServiceProperties sP) throws Exception {
      CasAuthenticationFilter filter = new CasAuthenticationFilter();
      filter.setServiceProperties(sP);
      filter.setAuthenticationManager(authenticationManager());
      return filter;
    }
}


它应该重定向到安全登录页面(cas 服务器登录页面)但现在我无法启动该应用程序。

感谢您的帮助

编辑:我的 CasAuthenticationProvider bean 在我的 CasSecuredAppApplication class 中,就像 tuto 所说的那样


public class CasSecuredAppApplication {
    
    @Bean
    public ServiceProperties serviceProperties() {
        ServiceProperties serviceProperties = new ServiceProperties();
        serviceProperties.setService("http://xxx/");
        serviceProperties.setSendRenew(false);
        return serviceProperties;
    }
     
    @Bean
    @Primary
    public AuthenticationEntryPoint authenticationEntryPoint(
      ServiceProperties sP) {
      
        CasAuthenticationEntryPoint entryPoint
          = new CasAuthenticationEntryPoint();
        entryPoint.setLoginUrl("https://xxx/cas/login");
        entryPoint.setServiceProperties(sP);
        return entryPoint;
    }
     
    @Bean
    public TicketValidator ticketValidator() {
        return new Cas30ServiceTicketValidator(
          "https:///cas");
    }
     
    @Bean
    public CasAuthenticationProvider casAuthenticationProvider() {
      
        CasAuthenticationProvider provider = new CasAuthenticationProvider();
        provider.setServiceProperties(serviceProperties());
        provider.setTicketValidator(ticketValidator());
        provider.setUserDetailsService(
          s -> new User("casuser", "Mellon", true, true, true, true,
            AuthorityUtils.createAuthorityList("ROLE_ADMIN")));
        provider.setKey("CAS_PROVIDER_LOCALHOST_8080");
        return provider;
    }

}

我的SecurityConfig应该能找到class没有?

你classCasSecuredAppApplication{不是配置class.

如果这是您的主要 class,请在其他情况下添加 @SpringBootApplication @Configuration class 级别的注释。