目标:使用默认安全配置将jdbc身份验证添加到spring boot。
可以找到来源here
通过将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");
}
鉴于上述情况,添加了以下内容(found here):
@Configuration
@EnableAuthorizationServer
@EnableResourceServer
public class AuthenticationManagerConfig {
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema()
.withUser("user").password("password").roles("ROLE_USER").and()
.withUser("admin").password("password").roles("ROLE_USER", "ROLE_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而不是内存。我做错了吗?文档错了吗?我尝试了其他几条路线无济于事。
提前致谢!
答案 0 :(得分:2)
经过大量工作,我回忆起如何正确使用弹簧,即通过他们的代码来更好地理解手术解决我的需求。通过一些调查,确定了需要修改/扩展的类。导致:
@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查找,同时保持everthing else自动配置。
答案 1 :(得分:0)
if you want to override the default security configuration, make sure you are extending the WebSecurityConfigurerAdapter
class
@Configuration
@EnableAuthorizationServer
@EnableResourceServer
public class AuthenticationManagerConfig extends WebSecurityConfigurerAdapter
{
// ...
}
and remove the prefix ROLE_ on your role definition, it will added automatically by spring.