· 9 years ago · Nov 10, 2016, 08:48 AM
1@Test
2public void getAccessToken() throws Exception {
3 String authorization = "Basic " + new String(Base64Utils.encode("clientapp:123456".getBytes()));
4 String contentType = MediaType.APPLICATION_JSON + ";charset=UTF-8";
5
6 // @formatter:off
7 String content = mvc
8 .perform(
9 post("/oauth/token")
10 .header("Authorization", authorization)
11 .contentType(
12 MediaType.APPLICATION_FORM_URLENCODED)
13 .param("username", "roy")
14 .param("password", "spring")
15 .param("grant_type", "password")
16 .param("scope", "read write")
17 .param("client_id", "clientapp")
18 .param("client_secret", "123456"))
19 .andExpect(status().isOk())
20 .andExpect(content().contentType(contentType))
21 .andExpect(jsonPath("$.access_token", is(notNullValue())))
22 .andExpect(jsonPath("$.token_type", is(equalTo("bearer"))))
23 .andExpect(jsonPath("$.refresh_token", is(notNullValue())))
24 .andExpect(jsonPath("$.expires_in", is(greaterThan(4000))))
25 .andExpect(jsonPath("$.scope", is(equalTo("read write"))))
26 .andReturn().getResponse().getContentAsString();
27
28 // @formatter:on
29
30 String token= content.substring(17, 53);
31}
32
33@RequestMapping(value = "/authentication", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
34@ResponseBody
35public ResponseEntity authenticate(@RequestBody CredentialsDto credentials) {
36 try {
37
38 String email = credentials.getEmail();
39 String password = credentials.getPassword();
40 String tokenUrl = "http://" + env.getProperty("server.host") + ":8080" + "/dms-application-0.0.1-SNAPSHOT" + "/oauth/token";
41
42 // create request body
43 JSONObject request = new JSONObject();
44 request.put("username", "roy");
45 request.put("password", "spring");
46 request.put("grant_type","password");
47 request.put("scope","read write");
48 request.put("client_secret","123456");
49 request.put("client_id","clientapp");
50
51
52 // set headers
53 HttpHeaders headers = new HttpHeaders();
54
55 String authorization = "Basic " + new String(Base64Utils.encode("clientapp:123456".getBytes()));
56 String contentType = MediaType.APPLICATION_FORM_URLENCODED.toString();
57 headers.set("Authorization",authorization);
58 headers.set("Accept","application/json");
59 headers.set("Content-Type",contentType);
60
61 HttpEntity<String> entity = new HttpEntity<String>(request.toString(), headers);
62
63 // send request and parse result
64 ResponseEntity<String> loginResponse = restClient.exchange(tokenUrl, HttpMethod.POST, entity, String.class);
65 // restClient.postForEntity(tokenUrl,entity,String.class,)
66 if (loginResponse.getStatusCode() == HttpStatus.OK) {
67 //JSONObject userJson = new JSONObject(loginResponse.getBody());
68 String response = loginResponse.getBody();
69 return ResponseEntity.ok(response);
70 } else if (loginResponse.getStatusCode() == HttpStatus.UNAUTHORIZED) {
71 // nono... bad credentials
72 return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
73
74 }
75
76 } catch (Exception e) {
77 e.printStackTrace();
78 return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
79 }
80 return null;
81}