第三方登录企业云
1.前端js登录
通过ajax请求调用login,如下:
企业云门户地址为:https://entryent3-vip.c.xcaas.net/entry/pcapp
login的请求地址为:https://entryent3-vip.c.xcaas.net/login
$.ajax({
method : "POST",
url : https://entryent3-vip.c.xcaas.net/login,
dataType:"json",
xhrFields : {
withCredentials : true
},
headers : {
Accept : "application/json"
},
data : {
username:'13800000000',
password:'123456'
}
}).then(function(){
console.log("登录成功!");
}).fail(function(){
console.log("登录失败!");
});
登录成功后在第三方中,企业云中的页面和服务都能直接调用
2.后端java登录
使用http请求传用户名和密码执行login,在response的headers中可以获取Set-Cookie中的user_sesstion。
调用服务时,把获取到user_sesstion的设置到Cookie中,例如:method.addRequestHeader("Cookie", userSession);
调用login的实现如下,本例是在企业云中写的后端代码:
String local = "http://entry." + NamespaceUtil.getTenantNamespace("租户code");
String url = local + "/login";
String username = "13800000000";
String password = "123456";
HttpPost httpRequst = new HttpPost(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
List paramList = new ArrayList();
if (StringUtils.isBlank(username)) {
paramList.add(new BasicNameValuePair("token", password));
} else {
paramList.add(new BasicNameValuePair("username", username));
paramList.add(new BasicNameValuePair("password", password));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");
httpRequst.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpRequst);
if ((httpResponse.getStatusLine().getStatusCode() >= 200) && (httpResponse.getStatusLine().getStatusCode() < 300)) {
Header[] headers = httpResponse.getHeaders("Set-Cookie");
Header[] arrayOfHeader1 = headers;
int i = arrayOfHeader1.length;
int j = 0;
if (j < i) {
Header hd = arrayOfHeader1[j];
String hStr = hd.toString();
hStr = hStr.substring(hStr.indexOf(": ") + 1, hStr.indexOf(";"));
String[] hdArr = hStr.split("=");
String str1 = hdArr[1].trim();
return str1;
}
}
}
catch (Exception e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}