Java 在Spring引导中配置JDBC身份验证

Java 在Spring引导中配置JDBC身份验证,java,spring,spring-security,spring-boot,Java,Spring,Spring Security,Spring Boot,目标:使用默认安全配置向spring boot添加jdbc身份验证 来源可以找到 通过将AuthenticationManagerBuilder自动连接到某个@Configuration类中的方法来配置全局AuthenticationManager 举个例子: @Autowired private DataSource dataSource; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) t

目标:使用默认安全配置向spring boot添加jdbc身份验证

来源可以找到

通过将AuthenticationManagerBuilder自动连接到某个@Configuration类中的方法来配置全局AuthenticationManager

举个例子:

@Autowired
private DataSource dataSource;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .jdbcAuthentication()
            .dataSource(dataSource)
            .withDefaultSchema()
            .withUser("user").password("password").roles("USER").and()
            .withUser("admin").password("password").roles("USER", "ADMIN");
}
鉴于上述情况,添加了以下内容():

由此产生的错误:

java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configurers.provisioning.JdbcUserDetailsManagerConfigurer@13404f75 to already built object
进一步查看日志,我们可以看到自动配置在不应该的情况下执行它的操作:

2016-02-02 22:52:48.047 DEBUG 30487 --- [ost-startStop-1] eGlobalAuthenticationAutowiredConfigurer : Eagerly initializing {org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration=org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration$$EnhancerBySpringCGLIB$$627430a8@78acaa86}
2016-02-02 22:52:48.074 DEBUG 30487 --- [ost-startStop-1] .s.BootGlobalAuthenticationConfiguration : Eagerly initializing {user=com.msyla.usergreeter.user.User$$EnhancerBySpringCGLIB$$3cd414fd@73128671, coreConfig=com.msyla.usergreeter.user.core.config.CoreConfig$$EnhancerBySpringCGLIB$$30c07250@6ed3c66b}
2016-02-02 22:52:48.095  INFO 30487 --- [ost-startStop-1] b.a.s.AuthenticationManagerConfiguration : 

Using default security password: 5b33158f-156d-43f4-892f-6c452f15e1cc
问题:这里所要求的只是拥有默认的引导配置,除了用户存储的位置,jdbc而不是内存中。我做得不对吗?文件错了吗?我试过其他几条路,但都没有用


提前谢谢

如果要覆盖默认的安全配置,请确保正在扩展
websecurityConfigureAdapter

@Configuration
@EnableAuthorizationServer
@EnableResourceServer
public class AuthenticationManagerConfig extends WebSecurityConfigurerAdapter
{
    // ...
}

并删除角色定义中的前缀ROLE,它将由spring自动添加。

经过大量工作后,我回忆起如何正确使用spring,也就是说,逐步阅读他们的代码,以便更好地理解如何通过外科手术满足我的需要。通过一些调查,确定了需要修改/扩展的类别。导致:

@Configuration
@EnableAuthorizationServer
@EnableResourceServer
public class AuthenticationManagerConfig extends GlobalAuthenticationConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource);
    }
}

上面的命令将指示spring通过jdbc进行查找,同时保持所有其他内容的自动配置。

执行上述操作将使应用程序启动并运行(从authenticationManager中删除
。withDefaultSchema()
),但是由于默认安全配置已更改,我无法通过Oauth2获得访问权限。重申一下,我只想将auth管理器更改为查看jdbc,安全配置与自动配置一样好。非常感谢。你让我明白了,我可能也需要通过他们的代码来通过手术来满足我的需求。
@Configuration
@EnableAuthorizationServer
@EnableResourceServer
public class AuthenticationManagerConfig extends GlobalAuthenticationConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource);
    }
}