You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.1 KiB
90 lines
3.1 KiB
4 years ago
|
/**
|
||
|
* Copyright (C), 2015-2019
|
||
|
* FileName: HttpAuthorizeBridge
|
||
|
* Author: Louis
|
||
|
* Date: 2019/8/9 9:24
|
||
|
* Description: HttpAuthorizeBridge
|
||
|
* History:
|
||
|
* <author> <time> <version> <desc>
|
||
|
*/
|
||
|
package com.fr.plugin.j7814.request;
|
||
|
|
||
|
import com.fanruan.api.json.JSONKit;
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fanruan.api.net.http.HttpKit;
|
||
|
import com.fanruan.api.util.StringKit;
|
||
|
import com.fr.decision.fun.impl.AbstractHttpAuthorizeProvider;
|
||
|
import com.fr.general.ComparatorUtils;
|
||
|
import com.fr.json.JSONArray;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.j7814.config.SsoConfig;
|
||
|
import com.fr.third.org.apache.http.entity.StringEntity;
|
||
|
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* 〈Function Description〉<br>
|
||
|
* 〈HttpAuthorizeBridge http认证〉
|
||
|
*
|
||
|
* @author Louis
|
||
|
* @since 1.0.0
|
||
|
*/
|
||
|
public class HttpAuthorizeBridge extends AbstractHttpAuthorizeProvider {
|
||
|
|
||
|
@Override
|
||
|
public Scope scope() {
|
||
|
return Scope.REPLACE;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean authorize(String inputUsername, String inputPassword, String savedPassword, String hashPassword) {
|
||
|
SsoConfig config = SsoConfig.getInstance();
|
||
|
String httpUrl = config.getIpgUrl();
|
||
|
if (StringKit.isEmpty(httpUrl)) {
|
||
|
return false;
|
||
|
}
|
||
|
try {
|
||
|
JSONObject bodyParams = JSONKit.create();
|
||
|
bodyParams.put("username", inputUsername);
|
||
|
bodyParams.put("password", inputPassword);
|
||
|
bodyParams.put("clientId", config.getClientId());
|
||
|
bodyParams.put("clientSecret", config.getClientSecret());
|
||
|
Map<String, String> headers = new HashMap<>();
|
||
|
headers.put("Content-Type", "application/json");
|
||
|
StringEntity stringEntity = new StringEntity(bodyParams.encode(), "UTF-8");
|
||
|
String result = HttpKit.executeAndParse(com.fanruan.api.net.http.rs.HttpRequest.custom()
|
||
|
.url(httpUrl).post(stringEntity).headers(headers).build());
|
||
|
LogKit.error("sso-HttpAuthorizeBridge-authorize-result:{}", result);
|
||
|
return validateResult(result, inputUsername);
|
||
|
} catch (Exception e) {
|
||
|
LogKit.error(e.getMessage(), e);
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断验证结果
|
||
|
*
|
||
|
* @param result
|
||
|
* @param inputUsername
|
||
|
* @return
|
||
|
*/
|
||
|
private Boolean validateResult(String result, String inputUsername){
|
||
|
if (StringKit.isEmpty(result)){
|
||
|
return false;
|
||
|
}
|
||
|
final JSONObject jsonObject = JSONKit.create(result);
|
||
|
int errorNumber = jsonObject.getInt("errorNumber");
|
||
|
if (!ComparatorUtils.equals(errorNumber, 0)) {
|
||
|
return false;
|
||
|
}
|
||
|
final JSONObject applicationObject = jsonObject.getJSONObject("applicationAPIDto");
|
||
|
JSONArray applicationUsername = applicationObject.getJSONArray("applicationUsernames");
|
||
|
if (applicationUsername == null || applicationUsername.isEmpty()) {
|
||
|
LogKit.error("sso-HttpAuthorizeBridge-validateResult-applicationUsername is null.");
|
||
|
return false;
|
||
|
}
|
||
|
return applicationUsername.contains(inputUsername);
|
||
|
}
|
||
|
}
|