0

我有一个实体“AppUser”的存储库,如下所示:

@Repository
public interface AppUserRepository extends CrudRepository<AppUser, Long>{

    public AppUser findByUserName(String username);
    public void delete(AppUser user);
}

它被自动连接到我的 UserService 类中:

@Service("userService")
public class UserService {

    @Autowired
    AppUserRepository repository;

public AppUser registerNewAppUser(AppUser user) {
    AppUser u = repository.findByUserName(user.getUsername());
    if (u!=null)
        return null;
    return repository.save(user);
}

UserService 自动连接到我的 CustomUserDetailsS​​ervice 中,如下所示:

@Service("userDetailsService")
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    private UserService userService;

最后自动连接到 WebSecurityConfiguration 为:

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource datasource;

@Autowired
private CustomUserDetailsService customUserDetailsService;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.jdbcAuthentication().dataSource(datasource)
                .and()
                .userDetailsService(customUserDetailsService);

        // @formatter:on
    }

更新:我遵循了这里的一些建议,并更新了错误和 WebSecurityConfiguration 文件,如上所示。

我现在得到的错误是:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; 
nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private javax.sql.DataSource showcase.WebSecurityConfiguration.datasource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

所以,我认为我需要以某种方式做两件事:

  1. 为它提供一个 DataSource 类型的合格 bean(我认为 repo 应该符合条件,但不知何故不是)

  2. setFilterChainProxySecurityConfigurer. 不知道该怎么做。我按照教程创建了:

    public class WebMVCApplicationInitializer implements WebApplicationInitializer {
    
    public void onStartup(ServletContext container) {
    ServletRegistration.Dynamic registration =
            container.addServlet("dispatcher", new DispatcherServlet());
    registration.setLoadOnStartup(1);
    registration.addMapping("*.request");
    }
    }
    

    要添加 SpringSecurityFilterChain,我假设我需要在这里调用 addFilter 但它需要一个过滤器类。我需要创建它吗?另外,这个初始化程序是由调度程序 servlet 自动拾取的,还是我需要以某种方式配置它?

4

1 回答 1

0

你只需要WebSecurityConfiguration像这样连接你的数据源:

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private Datasource datasource;

    @Autowired
    private CustomUserDetailsService customUserDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.dataSource(dataSource).userDetailsService(customUserDetailsService);
    }
}

并且不确定您是否正在这样做,但请记住将 SpringSecurityFilterChaint 添加到您的上下文中。

于 2014-12-24T10:36:21.663 回答