17

我下面來自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用戶/密碼,即使文檔建議它應該。

我錯過了什麼讓它工作?

+0

沒有它不應該,除非你加上'@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

+0

這裏有一個更好的示例(更新):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

+0

@DaveSyer樣本沒有運行我,「創建名稱爲'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration'的錯誤' – 2015-01-08 03:57:26

回答

7
@Configuration 
protected static class AuthenticationManagerConfiguration extends GlobalAuthenticationConfigurerAdapter { 

     @Override 
     public void init(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication().withUser("min").password("min").roles("USER"); 
     } 

    } 
+0

是的,這比我在2.0.3中使用的更好。 – 2015-01-05 20:02:29

6

正如我仍然在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豆,內置的用戶認證走了,它開始依靠我自己的inMemoryAuthentication。當2.0.4發佈時,我會重新評估Dave在上面發佈的解決方案,因爲它看起來會更優雅。

+0

我認爲當您需要在OAuth服務器中配置靜態頁面並且需要公開時,此解決方案更適合。你可以重寫'configure(HttpSecurity http)'和'configure(WebSecurity web)'併爲此定義特定的'antMatchers'。 – 2015-09-10 16:31:36

+0

您是否知道如何將用戶存儲在數據庫中並在此時檢查他們的憑據? – Marcel 2016-02-27 20:54:48

+0

乾杯!上面的解決方案很有用@s​​hawn – Harleen 2016-07-20 10:47:26

3

的例子指出以上 -
https://github.com/dsyer/sparklr-boot/blob/master/src/main/java/demo/Application.java
爲春季1.3

如果使用Spring 1.5及以上(其通常將是現在的情況下)需要添加額外的屬性。

正如其他人所指出的,我們可以使用

@Configuration 
@EnableWebSecurity 
public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/user/getEmployeesList") 
      .hasAnyRole("ADMIN").anyRequest().authenticated().and().formLogin() 
      .permitAll().and().logout().permitAll(); 

     http.csrf().disable(); 
    } 

    @Override 
    public void configure(AuthenticationManagerBuilder authenticationMgr) throws Exception { 
     authenticationMgr.inMemoryAuthentication().withUser("javainuse").password("javainuse") 
      .authorities("ROLE_ADMIN"); 
    } 
} 

重要的一點是,如果使用Spring啓動1.5及以上還需要添加以下屬性 -

security.oauth2.resource.filter-order = 3 

面臨諸多問題試圖識別這一點。 還發現上述問題陳述了很好的借鑑 - Spring Boot + OAuth2 Example

相關問題