spring-bootspring-security-cas

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


I want to have an SSO CAS Authentification, I've followed the instruction of the Bealdung's tutorial (https://www.baeldung.com/spring-security-cas-sso part 4) but when I run as spring boot app I've got this error

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.

I saw same issue here but with other spring's import and i've tried to add @Bean annotation but no more results


@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;
    }
}


It suppose to redirect to the secured login page (the cas server login page) but now I can't start the app.

Thanks for your help

EDIT : My CasAuthenticationProvider bean is in my CasSecuredAppApplication class like the tuto said


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;
    }

}

It should be found by my SecurityConfig class no ?


Solution

  • You class CasSecuredAppApplication {isn't a configuration class.

    If it's your main class add @SpringBootApplication in other cases @Configuration annotation on the class level.