当前位置: 首页 > 知识库问答 >
问题:

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

劳和歌
2023-03-14

我想有一个SSO CAS认证,我已经按照Bealdung的教程(https://www.baeldung.com/spring-security-cas-sso第4部分)的说明,但当我作为Spring启动应用程序运行时,我有这个错误

SecurityConfig中构造函数的参数0需要找不到类型为“org.springframework.security.cas.authentication.CasAuthenticationProvider”的bean。

行动:

请考虑在您的配置中定义一个“org . spring framework . security . cas . authentication provider”类型的bean。

我在这里看到了同样的问题,但是在其他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豆子在我的CasSecuredAppApplication类中,就像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 no。

共有1个答案

龚德本
2023-03-14

您的类< code > CasSecuredAppApplication {不是配置类。

如果它是您的主类,则在类级别添加<code>@SpringBootApplication

 类似资料: