org.springframework.security.oauth2.client.OAuth2RestTemplate.getOAuth2ClientContext()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(180)

本文整理了Java中org.springframework.security.oauth2.client.OAuth2RestTemplate.getOAuth2ClientContext()方法的一些代码示例,展示了OAuth2RestTemplate.getOAuth2ClientContext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OAuth2RestTemplate.getOAuth2ClientContext()方法的具体详情如下:
包路径:org.springframework.security.oauth2.client.OAuth2RestTemplate
类名称:OAuth2RestTemplate
方法名:getOAuth2ClientContext

OAuth2RestTemplate.getOAuth2ClientContext介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-security-oauth

/**
 * @return the current OAuth2 context
 */
public OAuth2ClientContext getOAuth2ClientContext() {
  return client.getOAuth2ClientContext();
}

代码示例来源:origin: spring-projects/spring-security-oauth

/**
 * @return the current access token request
 */
public AccessTokenRequest getAccessTokenRequest() {
  return client.getOAuth2ClientContext().getAccessTokenRequest();
}

代码示例来源:origin: spring-projects/spring-security-oauth

private void verifyAuthorizationPage(OAuth2RestTemplate restTemplate, String location) {
  final AtomicReference<String> confirmationPage = new AtomicReference<String>();
  AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider() {
    @Override
    protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
      return new ResponseExtractor<ResponseEntity<Void>>() {
        public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
          confirmationPage.set(StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")));
          return new ResponseEntity<Void>(response.getHeaders(), response.getStatusCode());
        }
      };
    }
  };
  try {
    provider.obtainAuthorizationCode(restTemplate.getResource(), restTemplate.getOAuth2ClientContext()
        .getAccessTokenRequest());
  }
  catch (UserApprovalRequiredException e) {
    // ignore
  }
  String page = confirmationPage.get();
  verifyAuthorizationPage(page);
}

代码示例来源:origin: spring-projects/spring-security-oauth

private void verifyAuthorizationPage(OAuth2RestTemplate restTemplate, String location) {
  final AtomicReference<String> confirmationPage = new AtomicReference<String>();
  AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider() {
    @Override
    protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
      return new ResponseExtractor<ResponseEntity<Void>>() {
        public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
          confirmationPage.set(StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")));
          return new ResponseEntity<Void>(response.getHeaders(), response.getStatusCode());
        }
      };
    }
  };
  try {
    provider.obtainAuthorizationCode(restTemplate.getResource(), restTemplate.getOAuth2ClientContext().getAccessTokenRequest());
  } catch (UserApprovalRequiredException e) {
    // ignore
  }
  String page = confirmationPage.get();
  verifyAuthorizationPage(page);
}

代码示例来源:origin: spring-projects/spring-security-oauth

@Override
protected ClientHttpRequest createRequest(URI uri, HttpMethod method) throws IOException {
  OAuth2AccessToken accessToken = getAccessToken();
  AuthenticationScheme authenticationScheme = resource.getAuthenticationScheme();
  if (AuthenticationScheme.query.equals(authenticationScheme)
      || AuthenticationScheme.form.equals(authenticationScheme)) {
    uri = appendQueryParameter(uri, accessToken);
  }
  ClientHttpRequest req = super.createRequest(uri, method);
  if (AuthenticationScheme.header.equals(authenticationScheme)) {
    authenticator.authenticate(resource, getOAuth2ClientContext(), req);
  }
  return req;
}

代码示例来源:origin: org.springframework.security.oauth/spring-security-oauth2

/**
 * @return the current OAuth2 context
 */
public OAuth2ClientContext getOAuth2ClientContext() {
  return client.getOAuth2ClientContext();
}

代码示例来源:origin: org.springframework.security.oauth/spring-security-oauth2

/**
 * @return the current access token request
 */
public AccessTokenRequest getAccessTokenRequest() {
  return client.getOAuth2ClientContext().getAccessTokenRequest();
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2

@Override
  public void mangleURL(
      StringBuilder baseURL, StringBuilder path, Map<String, String> kvp, URLType type) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    OAuth2AccessToken token =
        geoServerOauth2RestTemplate.getOAuth2ClientContext().getAccessToken();
    if (authentication != null
        && authentication.isAuthenticated()
        && token != null
        && token.getTokenType().equalsIgnoreCase(OAuth2AccessToken.BEARER_TYPE)) {
      kvp.put("access_token", token.getValue());
    }
  }
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2-openid-connect

@Test
public void testNonBearerToken() throws Exception {
  DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
  token.setTokenType("access_token");
  restTemplate.getOAuth2ClientContext().setAccessToken(token);
  authenticator.authenticate(resource, restTemplate.getOAuth2ClientContext(), request);
  String auth = request.getHeaders().getFirst("Authorization");
  assertTrue(auth.startsWith("access_token "));
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2-github

@Test
public void testNonBearerToken() throws Exception {
  DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
  token.setTokenType("access_token");
  restTemplate.getOAuth2ClientContext().setAccessToken(token);
  authenticator.authenticate(resource, restTemplate.getOAuth2ClientContext(), request);
  String auth = request.getHeaders().getFirst("Authorization");
  assertTrue(auth.startsWith("access_token "));
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2-google

@Test
public void testNonBearerToken() throws Exception {
  DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
  token.setTokenType("access_token");
  restTemplate.getOAuth2ClientContext().setAccessToken(token);
  authenticator.authenticate(resource, restTemplate.getOAuth2ClientContext(), request);
  String auth = request.getHeaders().getFirst("Authorization");
  assertTrue(auth.startsWith("access_token "));
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2-google

@Test(expected = AccessTokenRequiredException.class)
public void testAccessDeneiedException() throws Exception {
  DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
  token.setTokenType("access_token");
  authenticator.authenticate(resource, restTemplate.getOAuth2ClientContext(), request);
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2-openid-connect

@Test(expected = AccessTokenRequiredException.class)
public void testAccessDeneiedException() throws Exception {
  DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
  token.setTokenType("access_token");
  authenticator.authenticate(resource, restTemplate.getOAuth2ClientContext(), request);
}

代码示例来源:origin: org.springframework.security.oauth/spring-security-oauth2

@Override
protected ClientHttpRequest createRequest(URI uri, HttpMethod method) throws IOException {
  OAuth2AccessToken accessToken = getAccessToken();
  AuthenticationScheme authenticationScheme = resource.getAuthenticationScheme();
  if (AuthenticationScheme.query.equals(authenticationScheme)
      || AuthenticationScheme.form.equals(authenticationScheme)) {
    uri = appendQueryParameter(uri, accessToken);
  }
  ClientHttpRequest req = super.createRequest(uri, method);
  if (AuthenticationScheme.header.equals(authenticationScheme)) {
    authenticator.authenticate(resource, getOAuth2ClientContext(), req);
  }
  return req;
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2-github

@Test(expected = AccessTokenRequiredException.class)
public void testAccessDeneiedException() throws Exception {
  DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
  token.setTokenType("access_token");
  authenticator.authenticate(resource, restTemplate.getOAuth2ClientContext(), request);
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2-google

@Test
  public void testCustomAuthenticator() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
    token.setTokenType("access_token");
    restTemplate.getOAuth2ClientContext().setAccessToken(token);
    OAuth2RequestAuthenticator customAuthenticator =
        new OAuth2RequestAuthenticator() {

          @Override
          public void authenticate(
              OAuth2ProtectedResourceDetails resource,
              OAuth2ClientContext clientContext,
              ClientHttpRequest req) {
            req.getHeaders()
                .set(
                    "X-Authorization",
                    clientContext.getAccessToken().getTokenType()
                        + " "
                        + "Nah-nah-na-nah-nah");
          }
        };

    customAuthenticator.authenticate(resource, restTemplate.getOAuth2ClientContext(), request);
    String auth = request.getHeaders().getFirst("X-Authorization");

    assertEquals("access_token Nah-nah-na-nah-nah", auth);
  }
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2-github

@Test
  public void testCustomAuthenticator() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
    token.setTokenType("access_token");
    restTemplate.getOAuth2ClientContext().setAccessToken(token);
    OAuth2RequestAuthenticator customAuthenticator =
        new OAuth2RequestAuthenticator() {

          @Override
          public void authenticate(
              OAuth2ProtectedResourceDetails resource,
              OAuth2ClientContext clientContext,
              ClientHttpRequest req) {
            req.getHeaders()
                .set(
                    "X-Authorization",
                    clientContext.getAccessToken().getTokenType()
                        + " "
                        + "Nah-nah-na-nah-nah");
          }
        };

    customAuthenticator.authenticate(resource, restTemplate.getOAuth2ClientContext(), request);
    String auth = request.getHeaders().getFirst("X-Authorization");

    assertEquals("access_token Nah-nah-na-nah-nah", auth);
  }
}

代码示例来源:origin: org.geoserver.community/gs-sec-oauth2-openid-connect

@Test
  public void testCustomAuthenticator() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
    token.setTokenType("access_token");
    restTemplate.getOAuth2ClientContext().setAccessToken(token);
    OAuth2RequestAuthenticator customAuthenticator =
        new OAuth2RequestAuthenticator() {

          @Override
          public void authenticate(
              OAuth2ProtectedResourceDetails resource,
              OAuth2ClientContext clientContext,
              ClientHttpRequest req) {
            req.getHeaders()
                .set(
                    "X-Authorization",
                    clientContext.getAccessToken().getTokenType()
                        + " "
                        + "Nah-nah-na-nah-nah");
          }
        };

    customAuthenticator.authenticate(resource, restTemplate.getOAuth2ClientContext(), request);
    String auth = request.getHeaders().getFirst("X-Authorization");

    assertEquals("access_token Nah-nah-na-nah-nah", auth);
  }
}

相关文章