ApiController.java 3.6 KB
Newer Older
1 2
package com.sso.demo.controller;

zlt2000's avatar
zlt2000 已提交
3
import cn.hutool.core.collection.CollectionUtil;
4 5 6 7 8 9 10 11 12 13 14 15 16 17
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
zlt2000's avatar
zlt2000 已提交
18
import java.util.ArrayList;
19
import java.util.HashMap;
zlt2000's avatar
zlt2000 已提交
20
import java.util.List;
21 22 23 24 25 26
import java.util.Map;

/**
 * @author zlt
 * @date 2020/3/10
 * <p>
27
 * Blog: https://zlt2000.gitee.io
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
 * Github: https://github.com/zlt2000
 */
@RestController
public class ApiController {
    @Value("${zlt.sso.client-id:}")
    private String clientId;

    @Value("${zlt.sso.client-secret:}")
    private String clientSecret;

    @Value("${zlt.sso.redirect-uri:}")
    private String redirectUri;

    @Value("${zlt.sso.access-token-uri:}")
    private String accessTokenUri;

    @Value("${zlt.sso.user-info-uri:}")
    private String userInfoUri;

    @GetMapping("/token/{code}")
    public Map tokenInfo(@PathVariable String code) throws UnsupportedEncodingException {
        //获取token
        Map tokenMap = getAccessToken(code);
        String accessToken = (String)tokenMap.get("access_token");
        //获取用户信息
        Map userMap = getUserInfo(accessToken);
zlt2000's avatar
zlt2000 已提交
54
        List<String> roles = getRoles(userMap);
55 56 57 58

        Map result = new HashMap(2);
        result.put("tokenInfo", tokenMap);
        result.put("userInfo", userMap);
zlt2000's avatar
zlt2000 已提交
59
        result.put("roles", roles);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        return result;
    }

    /**
     * 获取token
     */
    public Map getAccessToken(String code) throws UnsupportedEncodingException {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        byte[] authorization = (clientId + ":" + clientSecret).getBytes("UTF-8");
        BASE64Encoder encoder = new BASE64Encoder();
        String base64Auth = encoder.encode(authorization);
        headers.add("Authorization", "Basic " + base64Auth);

        MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
        param.add("code", code);
        param.add("grant_type", "authorization_code");
        param.add("redirect_uri", redirectUri);
        param.add("scope", "app");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(param, headers);
        ResponseEntity<Map> response = restTemplate.postForEntity(accessTokenUri, request , Map.class);
        Map result = response.getBody();
        return result;
    }

    /**
     * 获取用户信息
     */
    public Map getUserInfo(String accessToken) {
        RestTemplate restTemplate = new RestTemplate();
        Map result = restTemplate.getForObject(userInfoUri+"?access_token="+accessToken, Map.class);
        return (Map)result.get("datas");
    }
zlt2000's avatar
zlt2000 已提交
94 95 96 97 98 99 100 101 102 103 104

    private List<String> getRoles(Map userMap) {
        List<Map<String, String>> roles = (List<Map<String, String>>)userMap.get("roles");
        List<String> result = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(roles)) {
            roles.forEach(e -> {
                result.add(e.get("code"));
            });
        }
        return result;
    }
105
}