简体   繁体   English

Spring Security OAuth2 - 向授权 URL 添加参数

[英]Spring Security OAuth2 - Add parameter to Authorization URL

I am using Spring Security with OAuth2 for authentication/authorization using following project.我正在使用带有 OAuth2 的 Spring Security 进行身份验证/授权,使用以下项目。 http://projects.spring.io/spring-security-oauth/ http://projects.spring.io/spring-security-oauth/

I have a requirement to add parameter to OAuth2 authorization url.我需要向 OAuth2 授权 url 添加参数。 I am not sure how should I add it to AuthorizationCodeResourceDetails bean?我不确定我应该如何将它添加到 AuthorizationCodeResourceDetails bean 中?

The problem is I want to start the user journey by login or registration from client site.问题是我想通过从客户端站点登录或注册来开始用户旅程。 Client will send an OAuth request and on Authorization server I will show either registration form or login form for user to continue its journey.客户端将发送 OAuth 请求,在授权服务器上,我将显示注册表或登录表单供用户继续其旅程。

The default flow has only following parameters /oauth/authorize?client_id=[]&redirect_uri=[]&response_type=token&scope=openid+profile&state=HZSMKb默认流程只有以下参数 /oauth/authorize?client_id=[]&redirect_uri=[]&response_type=token&scope=openid+profile&state=HZSMKb

I want to append "&startPoint=register"我想附加“&startPoint=register”

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);
}

As "AuthorizationCodeResourceDetails" which is based on auth2 "authorization_code" flow doesn't accept extra parameters.由于基于 auth2“authorization_code”流程的“AuthorizationCodeResourceDetails”不接受额外的参数。 Therefore, to fix this I did workaround by providing the parameter in the authorization url itself.因此,为了解决这个问题,我通过在授权 url 本身中提供参数来解决这个问题。

For eg.例如。 if the authorization url is http://localhost:8080/idp/oauth/authorize如果授权网址是http://localhost:8080/idp/oauth/authorize

than I have appended my extra parameter to that url like following http://localhost:8080/idp/oauth/authorize?startPoint=register比我已将我的额外参数附加到该 url 后,如http://localhost:8080/idp/oauth/authorize?startPoint=register

As this request will be saved into the session by Spring under SavedRequest variable which I can get later on to find out whether initiated request was for registration or login.由于此请求将被 Spring 保存到会话中的 SavedRequest 变量下,我稍后可以了解发起的请求是用于注册还是登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM