mardi 12 juin 2018

Spring Security Testing: Retreiving a Token

I have successfully implemented code which allows me to retrieve a JWT token from my application via OAuth and UserDetailsService.

Atm I'm trying to implement tests to confirm this But I keep getting hit with 400 Bad requests

@Test
public void is_status_up() throws InterruptedException, JSONException {
    // Given

    final String TOKEN_URL = "http://localhost:8080/oauth/token";
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("username","username");
    params.add("credential","credentials");
    params.add("grant_type","password");

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<String> entity = new HttpEntity<>("parameters" , headers);

    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params, headers);



    RestTemplate restTemplate = new RestTemplate();

    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("client-id","client-secret"));

    //When
    String result = restTemplate.postForObject(TOKEN_URL, request, String.class);

    //Then
    assertThat(result).isNotNull();
}

This should try to attempt to retrieve a token from Spring Security Authorisation Server's /oauth/token endpoint I get met with a 400 Bad Request.

In postman, which successfully retrieves tokens I have the request set up

Headers Authorization: Basic Content-Type: application/x-www-form-urlencoded

body (x-www-form-urlencoded selected) username: password: grant_type:"password"

Aucun commentaire:

Enregistrer un commentaire