【问题标题】:Parameter 0 of constructor required a bean of type that could not be found构造函数的参数 0 需要一个找不到类型的 bean
【发布时间】:2019-10-20 01:30:25
【问题描述】:

我想要一个 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 类找到吗?

【问题讨论】:

  • 你必须像使用 casAuthentiationFilter 一样创建 CasAuthenticationProvider 的 bean
  • 我正在编辑我的帖子,感谢您的回答
  • CasSecuredAppApplication 是否使用@SpringBootApplication 注解?
  • 我添加了一个答案。如果您能接受它作为正确答案,我将不胜感激。谢谢

标签: spring-boot spring-security-cas


【解决方案1】:

你的 CasSecuredAppApplication 类{不是配置类。

如果它是您的主类,则在其他情况下添加 @SpringBootApplication 在类级别上添加 @Configuration 注释。

【讨论】:

    猜你喜欢
    • 2019-03-21
    • 2023-02-05
    • 2020-02-23
    • 2021-02-02
    • 1970-01-01
    • 2022-11-29
    • 2020-10-17
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多