2014-07-07 42 views
1

我想將OAuth添加到我正在使用Spring框架開發的其他服務中。我使用基於註釋的配置和spring-boot來運行它。oauth令牌端點的密碼

我有下面的類在我的項目:

@Configuration 
@EnableWebSecurity 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class SecuritySettings extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication() 
       .withUser("admin").password("123").authorities("ROLE_USER"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().anyRequest().authenticated() 
      .and().httpBasic().and().csrf().disable(); 
    } 
} 

和我的授權服務器的配置如下:

@Configuration 
@EnableAuthorizationServer 
public static class MyAuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory().withClient("web") 
       .authorizedGrantTypes("password") 
       .authorities("ROLE_CLIENT","ROLE_TRUSTED_CLIENT","ROLE_USER") 
       .scopes("read", "write") 
       .resourceIds(RESOURCE_ID); 
    } 
} 

當我做一個GET請求來/oauth/token/終點,我問輸入HTTP基本憑據。當我嘗試用admin用戶登錄再下面是記錄

o.s.s.o.provider.endpoint.TokenEndpoint : Handling error: NoSuchClientException, No client with requested id: admin 

輸入用戶名作爲web的作品,但我不知道它的密碼。會記錄一個默認密碼,但它也不起作用。

Using default security password: f23087f8-58ce-e3d-bc62-58bf0963e75c 

那麼這個密碼是什麼?我在哪裏可以找到它?我該如何設置它?

回答

2

您使用的API是從this builder class

令牌端點被客戶端應用程序用來請求訪問令牌的資源。它不被瀏覽器最終用戶使用。 OAuth2客戶端通常會分配一個「客戶端密鑰」,可用於在端點進行身份驗證,通常使用in the OAuth 2.0 spec所述的基本身份驗證。

因此,要回答你的具體問題,你可以使用在Builder API的「祕密」的方法,並使用該值作爲客戶端進行身份驗證:

clients.inMemory().withClient("web") 
    .authorizedGrantTypes("password") 
    .secret("webclientsecret") 
    ... 

此外,「密碼」批是指客戶請求令牌using an end users ID and password,只是爲了確保這是你真正想要的。這與密碼問題無關。

+0

嗨!謝謝回覆。你是對的,那正是我想在這裏實現的。我只使用瀏覽器來嘗試用戶名/密碼組合。我實際上讀過你之前鏈接的頁面。文檔中的示例請求會發送'Authorization'標頭,所以在我的情況下它應該是'Basic encode64(web:webclientsecret)'。我對麼?我仍然無法實現它的工作。 – idursun

+0

我終於得到它的工作。我接受你的答案,因爲它實際上回答了我的要求,所以我可以找出問題所在。 – idursun

+0

我在研究與Spring OAuth2中的客戶機定義有關的另一個問題時找到了答案。你願意發表評論嗎?這裏是鏈接:http://stackoverflow.com/questions/36899386/null-client-in-oauth2-multi-factor-authentication – CodeMed

1

這是OAuth訪問令牌。它基於用戶登錄名和密碼並用於訪問受保護的資源。 URL「/ oauth/token」用於獲取訪問令牌,而不是可用的請求令牌。此請求根據請求令牌祕密進行數字簽名。

OAuth協議以這種方式使用此訪問令牌:

  1. 應用,消費者獲得請求令牌。

  2. 用戶在服務提供商的網站上重定向,並在那裏授權請求令牌。 (如果通過Http basic進行授權,則應添加名稱爲「Authorization」並且值爲「Basic EncodeBase64(」name:password「)」的請求頭,其中EncodeBase64是一個函數,「name」和「password」是用戶名和用戶密碼。

  3. 應用,消費者的交流更多關於訪問令牌令牌。

  4. 應用,消費者發送的授權請求到服務的API。

你不能找到其他信息OAuth 2 Developers Guide and Spring Social Reference

我希望你已經得到了你的問題的答案(或接近它)。 =)

+0

謝謝你的回覆。我實際上是通過使用grant_type:password(如[here](http://tools.ietf.org/html/rfc6749#section-4.3))向token端點發出post請求。用戶名是'admin',用戶密碼是'123',我要在授權請求頭中編碼的東西是'Authorization:Basic [X]',其中X是'web:clientsecret'對的編碼值。我對嗎?我在這裏錯過了什麼?這些密碼都不起作用。我總是得到'錯誤:「未經授權」 消息:「錯誤憑據」 – idursun