java教程

java 版钉钉免密登录网页版代码例子

位置:首页 > java教程 > java技巧,2019-08-10 14:37
java 版钉钉免密登录网页版代码例子,这只是个思路的代码,并不是完全代码。可以结合自己的开发使用。​从获取code到用户的信息过程。

java 版钉钉免密登录网页版代码例子,这只是个思路的代码,并不是完全代码。可以结合自己的开发使用。从获取code到用户的信息过程。

package com.hqq.api.staff.services.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.hqq.api.staff.entitis.Staff;
import com.hqq.api.staff.repos.StaffRepo;
import com.hqq.api.staff.services.StaffService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
 
import java.io.BufferedReader;
import java.util.List;
 
/**
 * Created by kevinzou on 30/07/2017.
 * StaffServiceImpl
 */
@Service
public class StaffServiceImpl implements StaffService {
 
    @Autowired
    private StaffRepo repo;
 
    @Override
    public Staff save(Staff entity) {
        return repo.save(entity);
    }
 
    @Override
    public void delete(Long id) {
        repo.delete(id);
    }
 
    @Override
    public void delete(Staff entity) {
        repo.delete(entity);
    }
 
    @Override
    public Staff findById(Long id) {
        return repo.findOne(id);
    }
 
    @Override
    public Staff findBySample(Staff sample) {
        return repo.findOne(Example.of(sample));
    }
 
    @Override
    public List<Staff> findAll() {
        return repo.findAll();
    }
 
    @Override
    public List<Staff> findAll(Sort sort) {
        return repo.findAll(sort);
    }
 
    @Override
    public List<Staff> findBySamples(Staff sample) {
        return repo.findAll(Example.of(sample));
    }
 
    @Override
    public List<Staff> findBySamples(Staff sample, Sort sort) {
        return repo.findAll(Example.of(sample), sort);
    }
 
    @Override
    public Page<Staff> findBySamples(Staff sample, PageRequest pageRequest) {
        return repo.findAll(Example.of(sample), pageRequest);
    }
 
    @Override
    public Page<Staff> findBySamples(PageRequest pageRequest) {
        return repo.findAll(pageRequest);
    }
 
    @Override
    public JSONObject goDingLogin(String code) {
        try{
            //获取accesstoken
            String accessToken = getAccesstoken();
            //获取用户授权的持久授权码
            JSONObject json = getPersistentCode(accessToken, code);
            String openId = "";
            String persistentCode = "";
            if(null!=json){
                openId = json.getString("openid");
                persistentCode = json.getString("persistent_code");
            }
            //获取用户授权的SNS_TOKEN
            String snsToken = getSnsToken(accessToken, openId, persistentCode);
            //获取用户unionid
            String unionId = getUnionId(snsToken);
            //根据unionid获取用户userId
            String appAccessToken = getAppAccesstoken();
            String userId = getUserId(appAccessToken, unionId);
            //获取用户详细数据
            JSONObject userData = getUserData(appAccessToken, userId);
            return userData;
        }catch (Exception e){
 
        }
        return null;
    }
 
    public String getAccesstoken() {
        String url = "https://oapi.dingtalk.com/sns/gettoken?appid=dingoap77h68gih6szglb4&appsecret=ZUGaKf3pvd6DDtVEpjdISmjCv_Sq1VNH0Oe8v_nfockMKIQth03KWwSS5fzRIM5o";
        JSONObject json = ossHttpGetUtil(url);
        if(null!=json){
            if (Integer.parseInt(json.get("errcode").toString()) == 0) {
                String accessToken = json.getString("access_token");
                return accessToken;
            }
        }
        return "";
    }
 
    public String getAppAccesstoken() {
        String url = "https://oapi.dingtalk.com/gettoken?corpid=dingbbe74ca7c844b45f35c2f4657eb6378f&corpsecret=MERA4ADae_62lU05Y9fbU9Z43NZBoIBTHnam1SGiKgblcO_NKIzEts-2G3Ji5njw";
        JSONObject json = ossHttpGetUtil(url);
        if(null!=json){
            if (Integer.parseInt(json.get("errcode").toString()) == 0) {
                String appAccessToken = json.getString("access_token");
                return appAccessToken;
            }
        }
        return "";
    }
 
    public JSONObject getPersistentCode(String accessToken,String code) {
        String url = "https://oapi.dingtalk.com/sns/get_persistent_code?access_token=" + accessToken;
        JSONObject jsonData = new JSONObject();
        jsonData.put("tmp_auth_code", code);
        JSONObject json = ossHttpPostUtil(url, jsonData);
        if(null!=json){
            if (Integer.parseInt(json.get("errcode").toString()) == 0) {
                return json;
            }
        }
        return null;
    }
 
    public String getSnsToken(String accesstoken, String openid, String persistent_code) {
        String url = "https://oapi.dingtalk.com/sns/get_sns_token?access_token="+accesstoken;
        HttpPost httpPost = new HttpPost(url);
        JSONObject jsonData = new JSONObject();
        jsonData.put("openid", openid);
        jsonData.put("persistent_code", persistent_code);
        JSONObject json = ossHttpPostUtil(url, jsonData);
        if(null!=json){
            if (Integer.parseInt(json.get("errcode").toString()) == 0) {
                String snsToken = json.getString("sns_token");
                return snsToken;
            }
        }
        return null;
    }
 
    public String getUnionId(String snsToken) {
        String url = "https://oapi.dingtalk.com/sns/getuserinfo?sns_token="+snsToken;
        JSONObject json = ossHttpGetUtil(url);
        if(null!=json){
            if (Integer.parseInt(json.get("errcode").toString()) == 0) {
                JSONObject jsonUser = json.getJSONObject("user_info");
                String unionid = jsonUser.getString("unionid");
                return unionid;
            }
        }
        return "";
    }
 
    public String getUserId(String accessToken, String unionId) {
        String url = "https://oapi.dingtalk.com/user/getUseridByUnionid?unionid="+unionId+"&access_token="+accessToken;
        JSONObject json = ossHttpGetUtil(url);
        if(null!=json){
            if (Integer.parseInt(json.get("errcode").toString()) == 0) {
                String userId = json.getString("userid");
                return userId;
            }
        }
        return "";
    }
 
    public JSONObject getUserData(String accessToken, String userId) {
        String url = "https://oapi.dingtalk.com/user/get?access_token="+accessToken+"&userid="+userId;
        JSONObject json = ossHttpGetUtil(url);
        if(null!=json){
            if (Integer.parseInt(json.get("errcode").toString()) == 0) {
                return json;
            }
        }
        return null;
    }
 
    private JSONObject ossHttpGetUtil(String url){
        HttpGet httpGet = new HttpGet(url);
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        HttpClient httpClient = httpClientBuilder.build();
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
        } catch (Exception e) {
        }
        BufferedReader bufferedReader = null;
        StringBuilder entityStringBuilder = new StringBuilder();
        //得到httpResponse的状态响应码
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        JSONObject jsonObject = null;
        String access_token = "";
        if (statusCode == HttpStatus.SC_OK) {
            //得到httpResponse的实体数据
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                try {
                    return jsonObject = JSONObject.parseObject(EntityUtils.toString(httpEntity));
                } catch (Exception e) {
 
                }
            }
        }
        return null;
    }
 
    private JSONObject ossHttpPostUtil(String url, JSONObject json){
        HttpPost httpPost = new HttpPost(url);
        HttpEntity httpEntity = null;
        httpEntity = new StringEntity(json.toString(), "UTF-8");
        httpPost.setEntity(httpEntity);
        HttpResponse httpResponse = null;
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        HttpClient httpClient = httpClientBuilder.build();
        try {
            httpResponse = httpClient.execute(httpPost);
        } catch (Exception e) {
 
        }
        StringBuilder entityStringBuilder = new StringBuilder();
        //得到httpResponse的状态响应码
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            //得到httpResponse的实体数据
            HttpEntity httpEntity2 = httpResponse.getEntity();
            JSONObject jsonObject = null;
            if (httpEntity2 != null) {
                try {
                    return jsonObject = jsonObject.parseObject(EntityUtils.toString(httpEntity2));
                } catch (Exception e) {
 
                }
            }
        }
        return null;
    }
 
}

TAGS:钉钉免密登录

猜你喜欢

NewHot