我下面來自Dave Syer基本春天啓動的OAuth2例如:https://github.com/dsyer/sparklr-boot/blob/master/src/main/java/demo/Application.java如何讓Spring引導和的OAuth2例如使用其他的密碼授予證書不是默認
@Configuration
@ComponentScan
@EnableAutoConfiguration
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RequestMapping("/")
public String home() {
return "Hello World";
}
@Configuration
@EnableResourceServer
protected static class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
// Just for laughs, apply OAuth protection to only 2 resources
.requestMatchers().antMatchers("/","/admin/beans").and()
.authorizeRequests()
.anyRequest().access("#oauth2.hasScope('read')");
// @formatter:on
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("sparklr");
}
}
@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// @formatter:off
clients.inMemory()
.withClient("my-trusted-client")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.resourceIds("sparklr")
.accessTokenValiditySeconds(60)
.and()
.withClient("my-client-with-registered-redirect")
.authorizedGrantTypes("authorization_code")
.authorities("ROLE_CLIENT")
.scopes("read", "trust")
.resourceIds("sparklr")
.redirectUris("http://anywhere?key=value")
.and()
.withClient("my-client-with-secret")
.authorizedGrantTypes("client_credentials", "password")
.authorities("ROLE_CLIENT")
.scopes("read")
.resourceIds("sparklr")
.secret("secret");
// @formatter:on
}
}
}
的例子都非常好,這兩種類型的授權,但密碼授權使用Spring Boot默認安全用戶(在啓動過程中回顯爲「使用默認安全密碼:927ca0a0-634a-4671-bd1c-1323a866618a」)。
我的問題是你如何覆蓋默認的用戶帳戶,實際上依賴WebSecurityConfig?我已經添加了這樣一個部分:
@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用戶/密碼,即使文檔建議它應該。
我錯過了什麼讓它工作?
沒有它不應該,除非你加上'@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)'它。您可以通過設置屬性'security.user.name'和'security.user.password'來設置application.properties文件中的默認用戶名/密碼。有關更多屬性,請參見[參考指南](http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)。 – 2014-10-29 07:05:11
這裏有一個更好的示例(更新):https://github.com/spring-projects/spring-security-oauth/blob/master/tests/annotation/jdbc/src/main/java/demo/Application的.java#L81。這個'authenticationManager'方法是2.0.4快照中的新覆蓋(如果你想在2.0.3中使用它,請查看實現)。 – 2014-10-29 08:03:53
@DaveSyer樣本沒有運行我,「創建名稱爲'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration'的錯誤' – 2015-01-08 03:57:26