首页 文章

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

提问于
浏览
1

我使用Spring Security和OAuth2进行身份验证/授权使用以下项目 . http://projects.spring.io/spring-security-oauth/

我需要在OAuth2授权网址中添加参数 . 我不确定应该如何将它添加到AuthorizationCodeResourceDetails bean?

问题是我想通过登录或从客户端站点注册来启动用户旅程 . 客户端将发送OAuth请求,在授权服务器上,我将显示注册表单或登录表单,以便用户继续其旅程 .

默认流只有以下参数/ oauth / authorize?client_id = []&redirect_uri = []&response_type = token&scope = openid profile&state = HZSMKb

我想追加“&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);
}

1 回答

  • 1

    由于“AuthorizationCodeResourceDetails”基于auth2“authorization_code”流不接受额外的参数 . 因此,为了解决这个问题,我通过在授权URL本身中提供参数来解决了这个问题 .

    例如 . 如果授权网址是http://localhost:8080/idp/oauth/authorize

    比我将我的额外参数添加到该网址,如下面的http://localhost:8080/idp/oauth/authorize?startPoint=register

    由于此请求将由Spring在SavedRequest变量下保存到会话中,稍后我可以查看该命令是否已启动请求是用于注册还是登录 .

相关问题