Example usage for org.springframework.util Base64Utils encode

List of usage examples for org.springframework.util Base64Utils encode

Introduction

In this page you can find the example usage for org.springframework.util Base64Utils encode.

Prototype

public static byte[] encode(byte[] src) 

Source Link

Document

Base64-encode the given byte array.

Usage

From source file:sy.rest.controller.test.GreetingControllerTest.java

private String getAccessToken(String username, String password) throws Exception {
    String authorization = "Basic " + new String(Base64Utils.encode("clientapp:123456".getBytes()));
    String contentType = MediaType.APPLICATION_JSON + ";charset=UTF-8";

    // @formatter:off
    String content = mvc/*from w ww  .  jav  a  2s.  com*/
            .perform(post("/oauth/token").header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED).param("username", username)
                    .param("password", password).param("grant_type", "secret").param("scope", "read write")
                    .param("client_id", "clientapp").param("client_secret", "123456"))
            .andExpect(status().isOk()).andExpect(content().contentType(contentType))
            .andExpect(jsonPath("$.access_token", is(notNullValue())))
            .andExpect(jsonPath("$.token_type", is(equalTo("bearer"))))
            .andExpect(jsonPath("$.refresh_token", is(notNullValue())))
            .andExpect(jsonPath("$.expires_in", is(greaterThan(4000))))
            .andExpect(jsonPath("$.scope", is(equalTo("read write")))).andReturn().getResponse()
            .getContentAsString();

    // @formatter:on

    return content.substring(17, 53);
}

From source file:com.sinespera.GreetingControllerTest.java

private String getAccessToken(String username, String password) throws Exception {
    String authorization = "Basic " + new String(Base64Utils.encode("clientapp:123456".getBytes()));
    String contentType = MediaType.APPLICATION_JSON + ";charset=UTF-8";

    // @formatter:off
    String content = mvc//from   w  w  w  . j  a  va 2  s.c o  m
            .perform(post("/oauth/token").header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED).param("username", username)
                    .param("password", password).param("grant_type", "password").param("scope", "read write")
                    .param("client_id", "clientapp").param("client_secret", "123456"))
            .andExpect(status().isOk()).andExpect(content().contentType(contentType))
            .andExpect(jsonPath("$.access_token", is(notNullValue())))
            .andExpect(jsonPath("$.token_type", is(equalTo("bearer"))))
            .andExpect(jsonPath("$.refresh_token", is(notNullValue())))
            .andExpect(jsonPath("$.expires_in", is(greaterThan(4000))))
            .andExpect(jsonPath("$.scope", is(equalTo("read write")))).andReturn().getResponse()
            .getContentAsString();

    // @formatter:on

    return content.substring(17, 53);
}

From source file:net.ambulando.oauth.test.resources.GreetingControllerTest.java

private Map<String, ?> getAccessTokenFromRefreshToken(final String refreshToken, final String scope,
        final String clientId, final String secret, final String grant) throws Exception {
    final String credentials = clientId + ":" + secret;
    final String authorization = "Basic " + new String(Base64Utils.encode(credentials.getBytes()));
    final String contentType = MediaType.APPLICATION_JSON + ";charset=UTF-8";

    final String content = mvc
            .perform(post("/oauth/token").header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED).param("refresh_token", refreshToken)
                    .param("grant_type", grant).param("scope", scope).param("client_id", clientId)
                    .param("client_secret", secret))
            .andExpect(status().isOk()).andExpect(content().contentType(contentType))
            .andExpect(jsonPath("$.access_token", is(notNullValue())))
            .andExpect(jsonPath("$.token_type", is(equalTo("bearer"))))
            .andExpect(jsonPath("$.refresh_token", is(notNullValue())))
            .andExpect(jsonPath("$.expires_in", is(greaterThan(4000))))
            .andExpect(jsonPath("$.scope", is(equalTo("read write")))).andReturn().getResponse()
            .getContentAsString();/*from w w  w .  j  ava 2 s .  c  o m*/

    return mapper.readValue(content, new TypeReference<Map<String, ?>>() {
    });
}

From source file:proto.GreetingControllerTest.java

private String getAccessToken(String username, String password) throws Exception {
    String authorization = "Basic " + new String(Base64Utils.encode("clientapp:123456".getBytes()));
    String contentType = MediaType.APPLICATION_JSON + ";charset=UTF-8";

    // @formatter:off
    String content = mvc/*from   w  ww. j  ava2s  .c  o  m*/
            .perform(post("/oauth/token").header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED).param("username", username)
                    .param("password", password).param("grant_type", "password").param("scope", "read write")
                    .param("client_id", "clientapp").param("client_secret", "123456"))
            .andExpect(status().isOk()).andExpect(content().contentType(contentType))
            .andExpect(jsonPath("$.access_token", is(notNullValue())))
            .andExpect(jsonPath("$.token_type", is(equalTo("bearer"))))
            .andExpect(jsonPath("$.refresh_token", is(notNullValue())))
            .andExpect(jsonPath("$.expires_in", is(greaterThan(4000))))
            .andExpect(jsonPath("$.scope", is(equalTo("read write")))).andReturn().getResponse()
            .getContentAsString();
    // @formatter:on
    return content.substring(17, content.indexOf(",") - 1);
}

From source file:com.gazbert.bxbot.rest.api.AbstractConfigControllerTest.java

String getAccessToken(String username, String password) throws Exception {

    final String authorization = "Basic "
            + new String(Base64Utils.encode((OAUTH_CLIENT_ID + ":" + OAUTH_CLIENT_SECRET).getBytes()));
    final String contentType = MediaType.APPLICATION_JSON + ";charset=UTF-8";

    final String content = mockMvc
            .perform(post("/oauth/token").header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED).param("username", username)
                    .param("password", password).param("grant_type", "password").param("scope", "read write")
                    .param("client_id", OAUTH_CLIENT_ID).param("client_secret", OAUTH_CLIENT_SECRET))
            .andExpect(status().isOk()).andExpect(content().contentType(contentType))
            .andExpect(jsonPath("$.access_token", is(notNullValue())))
            .andExpect(jsonPath("$.token_type", is(equalTo("bearer"))))
            .andExpect(jsonPath("$.refresh_token", is(notNullValue())))
            .andExpect(jsonPath("$.expires_in", is(greaterThan(4000))))
            .andExpect(jsonPath("$.scope", is(equalTo("read write")))).andReturn().getResponse()
            .getContentAsString();/*from w  w  w. j a v a2s.  c o m*/

    return content.substring(17, 53);
}

From source file:net.ambulando.oauth.test.resources.GreetingControllerTest.java

private Map<String, ?> getAccessTokenFromPassword(final String username, final String password,
        final String scope, final String clientId, final String secret, final String grant) throws Exception {
    final String credentials = clientId + ":" + secret;
    final String authorization = "Basic " + new String(Base64Utils.encode(credentials.getBytes()));
    final String contentType = MediaType.APPLICATION_JSON + ";charset=UTF-8";

    final String content = mvc
            .perform(post("/oauth/token").header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED).param("username", username)
                    .param("password", password).param("grant_type", grant).param("scope", scope)
                    .param("client_id", clientId).param("client_secret", secret))
            .andExpect(status().isOk()).andExpect(content().contentType(contentType))
            .andExpect(jsonPath("$.access_token", is(notNullValue())))
            .andExpect(jsonPath("$.token_type", is(equalTo("bearer"))))
            .andExpect(jsonPath("$.scope", is(equalTo("read write")))).andReturn().getResponse()
            .getContentAsString();//from w  w w .j  a  v a  2s  . c  o  m

    return mapper.readValue(content, new TypeReference<Map<String, ?>>() {
    });
}

From source file:capital.scalable.restdocs.example.testsupport.MockMvcBase.java

private String getAccessToken(String username, String password) throws Exception {
    String authorization = "Basic " + new String(Base64Utils.encode("app:very_secret".getBytes()));
    String contentType = MediaType.APPLICATION_JSON + ";charset=UTF-8";

    String body = mockMvc//from  www  .j  a  va  2  s  .co  m
            .perform(post("/oauth/token").header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED).param("username", username)
                    .param("password", password).param("grant_type", "password").param("scope", "read write")
                    .param("client_id", "app").param("client_secret", "very_secret"))
            .andExpect(status().isOk()).andExpect(content().contentType(contentType))
            .andExpect(jsonPath("$.access_token", is(notNullValue())))
            .andExpect(jsonPath("$.token_type", is(equalTo("bearer"))))
            .andExpect(jsonPath("$.refresh_token", is(notNullValue())))
            .andExpect(jsonPath("$.expires_in", is(greaterThan(4000))))
            .andExpect(jsonPath("$.scope", is(equalTo("read write")))).andReturn().getResponse()
            .getContentAsString();

    return body.substring(17, 53);
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive.ReactiveTokenValidatorTests.java

private String getSignedToken(byte[] header, byte[] claims) throws Exception {
    PrivateKey privateKey = getPrivateKey();
    Signature signature = Signature.getInstance("SHA256WithRSA");
    signature.initSign(privateKey);/*from   w  w w . j a v  a2s . c o m*/
    byte[] content = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encode(claims));
    signature.update(content);
    byte[] crypto = signature.sign();
    byte[] token = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encodeUrlSafe(claims),
            Base64Utils.encodeUrlSafe(crypto));
    return new String(token, StandardCharsets.UTF_8);
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.TokenValidatorTests.java

private String getSignedToken(byte[] header, byte[] claims) throws Exception {
    PrivateKey privateKey = getPrivateKey();
    Signature signature = Signature.getInstance("SHA256WithRSA");
    signature.initSign(privateKey);//from www .  j a  v  a2  s  .c om
    byte[] content = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encode(claims));
    signature.update(content);
    byte[] crypto = signature.sign();
    byte[] token = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encodeUrlSafe(claims),
            Base64Utils.encodeUrlSafe(crypto));
    return new String(token, UTF_8);
}

From source file:org.springframework.boot.actuate.metrics.atsd.AtsdMetricWriter.java

@Override
public void afterPropertiesSet() throws Exception {
    Assert.notNull(this.restTemplate, "RestTemplate is required");
    Assert.isTrue(this.bufferSize > 0, "Buffer size must be greater than 0");
    this.buffer.ensureCapacity(this.bufferSize);
    Assert.notNull(this.namingStrategy, "Naming strategy is required");
    Assert.notNull(this.dataEncoder, "Data encoder is required");
    Assert.hasText(this.url, "Url is required");
    Assert.hasText(this.username, "Username is required");
    Assert.hasText(this.password, "Password is required");
    byte[] token = Base64Utils.encode((this.username + ":" + this.password).getBytes());
    this.basicAuthorizationHeader = "Basic " + new String(token);
}