Java 如何让Spring引导和OAuth2示例使用默认之外的密码授予凭据
下面是Dave Syer提供的基本Spring Boot OAuth2示例: 该示例对于这两种类型的授权都非常有效,但密码授权使用Spring Boot默认安全用户(echo在启动期间退出“使用默认安全密码:927ca0a0-634a-4671-bd1c-1323a866618a”) 我的问题是,如何覆盖默认用户帐户并实际依赖WebSecurity配置?我添加了这样一个部分:Java 如何让Spring引导和OAuth2示例使用默认之外的密码授予凭据,java,spring,spring-security,spring-boot,spring-security-oauth2,Java,Spring,Spring Security,Spring Boot,Spring Security Oauth2,下面是Dave Syer提供的基本Spring Boot OAuth2示例: 该示例对于这两种类型的授权都非常有效,但密码授权使用Spring Boot默认安全用户(echo在启动期间退出“使用默认安全密码:927ca0a0-634a-4671-bd1c-1323a866618a”) 我的问题是,如何覆盖默认用户帐户并实际依赖WebSecurity配置?我添加了这样一个部分: @Configuration @EnableWebSecurity @EnableGlobalMethodSecurit
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
protected static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder)
throws Exception {
authManagerBuilder.inMemoryAuthentication().withUser("user")
.password("password").roles("USER");
}
}
但它似乎并没有覆盖默认的Spring用户/密码,即使文档建议它应该这样做
我还缺少什么来让它工作呢?因为我还在使用2.0.3,我尝试了更多的东西,这似乎是有效的:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
protected static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder
.inMemoryAuthentication()
.withUser("user1").password("password1").roles("USER").and()
.withUser("admin1").password("password1").roles("ADMIN");
}
@Bean
@Override
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
}
通过显式定义authenticationManager bean,内置的用户身份验证消失了,它开始依赖于我自己的inMemoryAuthentication。当2.0.4发布后,我将重新评估Dave在上面发布的解决方案,因为它看起来将更加优雅。不,除非您向其添加
@Order(SecurityProperties.ACCESS\u OVERRIDE\u Order)
。通过设置属性security.user.name
和security.user.password
,可以在application.properties文件中设置默认用户名/密码。有关更多属性,请参阅。此处有一个更好的示例(更为最新):。该authenticationManager
方法是2.0.4快照中的一个新覆盖(如果您想在2.0.3中使用它,请查看实现)。@DaveSyer示例没有为我运行,“创建名为'org.springframework.security.config.annotation.web.configuration'的bean时出错”对我有效,并且所有测试都是绿色的(并且您的错误片段太小,无法诊断问题)。您如何构建和运行此应用程序?是的,这是一个比我在2.0.3中使用的更好的解决方案。我认为当您需要在OAuth服务器中配置静态页面并且这些页面需要公开时,此解决方案更合适。您可以覆盖configure(HttpSecurity http)
和配置(WebSecurity web)
并为此定义特定的antMatchers
。您知道如何将用户存储在数据库中并检查其凭据吗?
@Configuration
protected static class AuthenticationManagerConfiguration extends GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("min").password("min").roles("USER");
}
}
@Configuration
protected static class AuthenticationManagerConfiguration extends GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("min").password("min").roles("USER");
}
}