2014-02-13 63 views
2

我想獲得oauth2春季啓動工作,並保護我的休息方法調用,取得了很大的成功。 我已經嘗試使用spring-security-oauth2-javaconfig:1.0.0.CI-SNAPSHOT與rg.springframework.boot:spring-boot-starter-security:1.0.0.RC1。獲取oauth2與春季啓動和休息

* gradle這個: 編譯( 「org.springframework.boot:春天開機起動安全:1.0.0.RC1」)

compile ('org.springframework.security.oauth:spring-security-oauth2-javaconfig:1.0.0.CI-SNAPSHOT'){ 
    exclude module: 'spring-security-config' 
    exclude module: 'spring-security-core' 
    exclude module: 'spring-security-web' 
} 

現在我只是想獲得認證資源服務器工作。我已經複製並嘗試修改spring-security-oauth2-javaconfig示例中的現有sparklr2示例。

最後的錯誤我得到的是: 「錯誤」: 「invalid_client」, 「ERROR_DESCRIPTION」: 「壞客戶端憑證 當我運行卷曲-v --data」 grant_type =密碼&用戶名=瑪麗莎&密碼=考拉& CLIENT_ID = TONR &祕密=祕密」 -X POST本地主機:。8100 /的OAuth /令牌

我瞭解從初學者的角度和資源方面的oauth2彈簧引導和休息使它很難貧乏的oauth2任何建議?

如果有人可以提供一種類似於Cookbook的方法來配置oauth2 aut驗證和授權以保護休息API調用以及相關的curl命令,那將是非常棒的。

回答

3

對oauth2的Java配置支持正在進行中,但您可能在my fork上獲得更多成功。如果我是你,我現在會堅持使用XML獲取oauth2位。這是一個帶有最小XML的bootified sparklr2。我沒有檢查它最近的工作,但它不應該是壞的形狀,如果你更新引導依賴關係到1.0.0.RC2。

更新:@Configuration的東西已經轉移到主OAuth2 repo,所以叉子和它的父母現在基本上是多餘的(並且可能很快會被刪除)。

更新:引導樣本現在也使用@Configuration

+0

您好戴夫,當我點擊叉,我收到了404 – user3304825

+0

對不起。拼寫錯誤。再試一次。 –

+0

謝謝 - 我會檢查出來並使用它。 – user3304825

0

是的。這就是我所做的努力,讓它以這種方式工作。我相信這是正確的解決方案(除了爲grant_type使用client_credentials,但我不是專家:-)如果有更好的解決方案,那將是非常棒的。非常感謝您花時間幫助我。


import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.core.authority.AuthorityUtils; 
import org.springframework.security.core.userdetails.User; 
import org.springframework.security.core.userdetails.UserDetails; 
import org.springframework.security.oauth2.config.annotation.authentication.configurers.InMemoryClientDetailsServiceConfigurer; 
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ServerConfigurerAdapter; 
import org.springframework.security.oauth2.config.annotation.web.configurers.OAuth2ServerConfigurer; 
import org.springframework.security.oauth2.provider.token.InMemoryTokenStore; 
import org.springframework.security.provisioning.InMemoryUserDetailsManager; 

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.List; 

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends OAuth2ServerConfigurerAdapter { 

    private final String applicationName = "restservice"; 

    @Value("${client_id}") 
    private String client_id; 

    @Value("${client_secret}") 
    private String client_secret; 

    @Value("${grant_type}") 
    private String grant_type; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .requestMatchers() 
      .and() 
      .authorizeRequests() 
       .antMatchers("/").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .apply(new OAuth2ServerConfigurer()) 
      .tokenStore(new InMemoryTokenStore()) 
      .resourceId(applicationName); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .userDetailsService(new InMemoryUserDetailsManager(getUserDetails())) 
      .and() 
      .apply(new InMemoryClientDetailsServiceConfigurer()) 
       .withClient(client_id) 
       .resourceIds(applicationName) 
       .scopes("read", "write") 
       .authorities("USER") 
       .authorizedGrantTypes(grant_type) 
       .secret(client_secret); 
    } 

    private static final Collection<UserDetails> getUserDetails() { 
     List<UserDetails> userDetails = new ArrayList<UserDetails>(); 
     userDetails.add(new User("user", "password", AuthorityUtils.createAuthorityList(
         "USER", "read", "write"))); 
     return userDetails; 
    } 

} 
+0

如果這對您有用,那麼很棒。儘管如此,我仍然在重構這些東西,所以它在Spring OAuth 2.0發佈之前可能會破壞,即使不是,它最終可能也不是最好的解決方案。 –