| | |
| | | package com.moral.controller; |
| | | |
| | | import com.moral.entity.auth.AuthToken; |
| | | import java.util.Arrays; |
| | | import java.util.LinkedHashMap; |
| | | |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.springframework.http.*; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.web.bind.annotation.PathVariable; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.LinkedHashMap; |
| | | import com.moral.entity.auth.AuthToken; |
| | | |
| | | @Controller |
| | | public class TokenController { |
| | |
| | | |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | HttpEntity<String> request = new HttpEntity<String>(getHeadersWithClientCredentials()); |
| | | ResponseEntity<Object> response = restTemplate.exchange(AUTH_SERVER_URI+"username="+username+"&password="+password, HttpMethod.POST, request, Object.class); |
| | | LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>)response.getBody(); |
| | | ResponseEntity<Object> response = restTemplate.exchange(AUTH_SERVER_URI + "username=" + username + "&password=" + password, HttpMethod.POST, request, Object.class); |
| | | LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) response.getBody(); |
| | | AuthToken token = null; |
| | | if(map!=null){ |
| | | if (map != null) { |
| | | token = new AuthToken(); |
| | | token.setAccess_token((String)map.get("access_token")); |
| | | token.setToken_type((String)map.get("token_type")); |
| | | token.setRefresh_token((String)map.get("refresh_token")); |
| | | token.setExpires_in((Integer)map.get("expires_in")); |
| | | token.setScope((String)map.get("scope")); |
| | | System.out.println(token); |
| | | token.setAccess_token((String) map.get("access_token")); |
| | | token.setToken_type((String) map.get("token_type")); |
| | | token.setRefresh_token((String) map.get("refresh_token")); |
| | | token.setExpires_in((Integer) map.get("expires_in")); |
| | | token.setScope((String) map.get("scope")); |
| | | } |
| | | return token; |
| | | } |
| | | |
| | | private static HttpHeaders getHeaders(){ |
| | | private static HttpHeaders getHeaders() { |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| | | return headers; |
| | | } |
| | | |
| | | private static HttpHeaders getHeadersWithClientCredentials(){ |
| | | String plainClientCredentials="my-trusted-client:secret"; |
| | | private static HttpHeaders getHeadersWithClientCredentials() { |
| | | String plainClientCredentials = "my-trusted-client:secret"; |
| | | String base64ClientCredentials = new String(Base64.encodeBase64(plainClientCredentials.getBytes())); |
| | | HttpHeaders headers = getHeaders(); |
| | | headers.add("Authorization", "Basic " + base64ClientCredentials); |