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

时间:2019-06-04 13:44:01

标签: spring-boot spring-security-cas

我想获得SSO CAS认证,我遵循了Bealdung的教程(https://www.baeldung.com/spring-security-cas-sso第4部分)的说明,但是当我作为Spring Boot应用程序运行时,却遇到了这个错误

  

SecurityConfig中构造函数的参数0需要一个类型为'org.springframework.security.cas.authentication.CasAuthenticationProvider'的bean。

     

操作:

     

考虑在您的配置中定义类型为“ org.springframework.security.cas.authentication.CasAuthenticationProvider”的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 bean在我的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类找到吗?

1 个答案:

答案 0 :(得分:0)

您为CasSecuredAppApplication类别{不是配置类别。

如果是您的主班级,则在班级级别上添加@SpringBootApplication注释,@Configuration