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

だから、どういうわけか2つのことをする必要があると思います:

  1. タイプ DataSource の適格な Bean を提供します (レポは適格であるべきだと考えていましたが、どういうわけかそうではありません)

  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 を呼び出す必要があると思いますが、フィルター クラスが必要です。それを作成する必要がありますか?また、このイニシャライザはディスパッチャ サーブレットによって自動的に取得されますか、それとも何らかの方法で設定する必要がありますか?

4

1 に答える 1