在OAuth客户端实现中向AuthServer发出的请求中的范围

huangapple 未分类评论47阅读模式
英文:

Scopes in request to AuthServer in OAuth Client implementation

问题

class AuthTokenManager {
	
    private ResponseEntity<String> requestToAuthServer(AuthServerInfo authServerInfo) {

        // 准备请求
        final HttpEntity<MultiValueMap<String, String>> request =
                new HttpEntity<>(prepareOAuthRequestBody(authServerInfo), prepareOAuthRequestHeader(authServerInfo));

        // 向认证服务器请求令牌
        final ResponseEntity<String> response =
                restTemplate.postForEntity(
                        authServerInfo.getOAuthAccessTokenUri(),
                        request,
                        String.class
                );

        return response;
    }

    private MultiValueMap<String, String> prepareOAuthRequestBody(AuthServerInfo authServerInfo) {
        // 准备请求体
        final MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add(GRANT_TYPE, authServerInfo.getOAuthGrantType());
        map.add(CLIENT_ID, authServerInfo.getOAuthClientId());
        map.add(SCOPE, authServerInfo.getOAuthScopes());  // 在这里添加作用域(scopes)
        return map;
    }

    private String prepareEncodedCredentials(AuthServerInfo authServerInfo) {
        // 准备用于头部的授权属性
        final String credentials = authServerInfo.getOAuthClientId() + ":" +
                authServerInfo.getOAuthClientSecret();
        return StringUtils.str(Base64.encodeBase64(StringUtils.bytes(credentials)));
    }

    private HttpHeaders prepareOAuthRequestHeader(AuthServerInfo authServerInfo) {
        // 准备请求头
        final HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add(AUTHORIZATION, BASIC + prepareEncodedCredentials(authServerInfo));
        headers.add(HttpHeaders.CONTENT_TYPE, URLENCODED);
        headers.setCacheControl(CACHE_CONTROL);
        return headers;
    }
}
英文:

I am working on an assignment, where I have to implement OAuth Client, which will make request to auth server to get access token.

I have implemented it through RestTemplate. Since, I have got some information about the authserver like uri, grant-type, client secret. on the basis of these information I have created a request which returns me proper access token.

But now, requirement is to add scopes in request to auth server. can someone help me where this scopes can be added in request as per below code. ? I have tried to add this in header & map. but i get the bad request error.

class AuthTokenManager {
	
	
    private ResponseEntity&lt;String&gt; requestToAuthServer(AuthServerInfo authServerInfo) {

        //prepare request
        final HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; request =
                new HttpEntity&lt;&gt;(prepareOAuthRequestBody(authServerInfo), prepareOAuthRequestHeader(authServerInfo));

        // requesting auth-server for token
        final ResponseEntity&lt;String&gt; response =
                restTemplate.postForEntity(
                        authServerInfo.getOAuthAccessTokenUri(),
                        request,
                        String.class
                );

        return response;
    }

    private MultiValueMap&lt;String, String&gt; prepareOAuthRequestBody(AuthServerInfo authServerInfo) {
        //prepare request body
        final MultiValueMap&lt;String, String&gt; map = new LinkedMultiValueMap&lt;&gt;();
        map.add(GRANT_TYPE, authServerInfo.getOAuthGrantType());
        map.add(CLIENT_ID, authServerInfo.getOAuthClientId());
//        map.add(SCOPE, authServerInfo.getOAuthScopes());
        return map;
    }

    private String prepareEncodedCredentials(AuthServerInfo authServerInfo) {
        //prepare authorization attribute for header.
        final String credentials = authServerInfo.getOAuthClientId() + &quot;:&quot; +
                authServerInfo.getOAuthClientSecret();
        return StringUtils.str(Base64.encodeBase64(StringUtils.bytes(credentials)));
    }

    private HttpHeaders prepareOAuthRequestHeader(AuthServerInfo authServerInfo) {
        // prepare request header
        final HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add(AUTHORIZATION, BASIC + prepareEncodedCredentials(authServerInfo));
        headers.add(HttpHeaders.CONTENT_TYPE, URLENCODED);
        headers.setCacheControl(CACHE_CONTROL);
        return headers;
    }

}

huangapple
  • 本文由 发表于 2020年7月23日 17:17:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/63050887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定