2015-06-19 51 views
1

我使用Spring Security與OAuth2進行身份驗證/授權,使用以下項目。 http://projects.spring.io/spring-security-oauth/Spring Security OAuth2 - 將參數添加到授權URL

我有要求將參數添加到OAuth2授權網址。我不知道我應該如何將它添加到AuthorizationCodeResourceDetails bean?

問題是我想通過登錄或從客戶端網站註冊來啓動用戶旅程。客戶端將發送OAuth請求,並在授權服務器上顯示註冊表單或登錄表單,以供用戶繼續其旅程。

默認流只以下參數 /OAuth的/授權?CLIENT_ID = [] & REDIRECT_URI = [] & RESPONSE_TYPE =記號&範圍=的OpenID +輪廓&狀態= HZSMKb

欲追加「&的startPoint =寄存器」

public OAuth2ProtectedResourceDetails googleOAuth2Details() { 
    AuthorizationCodeResourceDetails googleOAuth2Details = new AuthorizationCodeResourceDetails(); 
    googleOAuth2Details.setAuthenticationScheme(header); 
    googleOAuth2Details.setClientAuthenticationScheme(header); 
    googleOAuth2Details.setClientId(clientId); 
    googleOAuth2Details.setClientSecret(clientSecret); 
    googleOAuth2Details.setUserAuthorizationUri(authorizationUrl); 
    googleOAuth2Details.setAccessTokenUri(accessTokenUrl); 

    googleOAuth2Details.setScope(asList("openid","profile")); 
    return googleOAuth2Details; 
} 

@SuppressWarnings("SpringJavaAutowiringInspection") // Provided by Spring Boot 
@Resource 
private OAuth2ClientContext oAuth2ClientContext; 

@Bean 
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) 
public OAuth2RestOperations authCodeRestTemplate() { 
    return new OAuth2RestTemplate(googleOAuth2Details(), oAuth2ClientContext); 
} 
+0

您嘗試對哪個OAuth提供程序進行身份驗證?並且請張貼一些相關的代碼或者人們會簡單地冷靜下來。 –

+0

我使用spring-security-oauth2作爲提供者。 – itontips

+0

不,你想我的觀點,你想讓你的用戶登錄Google,對嗎? –

回答

0

爲 「」,它是基於auth2 「authorization_code」 流不接受額外的參數AuthorizationCodeResourceDetails。因此,爲了解決這個問題,我通過在授權url本身提供了參數來解決問題。

例如,如果授權網址是 http://localhost:8080/idp/oauth/authorize

比我附加我額外的參數到該URL像下面 http://localhost:8080/idp/oauth/authorize?startPoint=register

由於這個請求將被保存到由Spring下SavedRequest變量的會話,我以後可以坐上去找出發起的請求是註冊還是登錄。