ZhuDongming
2019-08-15 50e24cdfd75ada4dd6ea5dce74803b7f96e3ba81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.moral.controller;
 
import com.moral.entity.auth.AuthToken;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.*;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
 
import java.util.Arrays;
import java.util.LinkedHashMap;
 
@Controller
public class TokenController {
 
    private static final String AUTH_SERVER_URI = "http://localhost:8001/oauth/token?grant_type=password&";
 
    @ResponseBody
    @RequestMapping(value = "/oauth/token/{username}/{password}", method = RequestMethod.GET)
    public AuthToken get_auth_token(@PathVariable("username") String username, @PathVariable("password") String password) {
 
        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();
        AuthToken token = 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);
        }
        return token;
    }
 
    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";
        String base64ClientCredentials = new String(Base64.encodeBase64(plainClientCredentials.getBytes()));
        HttpHeaders headers = getHeaders();
        headers.add("Authorization", "Basic " + base64ClientCredentials);
        return headers;
    }
}