Commit 5e9e1b37 authored by zhangsen's avatar zhangsen

获取isdp的token接口

parent 2f6cccbb
......@@ -120,6 +120,24 @@
<version>2.2.1</version>
</dependency>-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.8</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.yeejoin.amos.fas.business.controller;
import com.yeejoin.amos.fas.business.util.JSONUtil;
import com.yeejoin.amos.fas.business.util.SSLClient;
import com.yeejoin.amos.fas.core.util.CommonResponseUtil2;
import com.yeejoin.amos.fas.core.util.ResponseModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api/sso")
@Api(tags = "获取isdp的tokenAPI")
public class SsoTokenController {
@Value("${sso.client.id}")
private String clientId;
@Value("${sso.client.secret}")
private String clientSecret;
@Value("${sso.login.client}")
private String loginClient;
@Value("${sso.client.url}")
private String clientUrl;
@Value("${sso.login.type}")
private String loginType;
@Autowired
private SSLClient sslClient;
/**
* 获取tokenAPI
*/
@ApiOperation(value = "获取token", notes = "获取token")
@GetMapping(value = "/getSsoToken", produces = "application/json;charset=UTF-8")
public ResponseModel getSsoToken(@RequestParam(value = "username") String username) {
Map<String, Object> params = new HashMap<>();
params.put("client_id", clientId);
params.put("client_secret", clientSecret);
params.put("loginClient", loginClient);
params.put("login_type", loginType);
params.put("username", username);
String message = JSONUtil.toJson(params);
String result = sslClient.doPost(clientUrl, message, "utf-8");
return CommonResponseUtil2.success(result);
}
}
package com.yeejoin.amos.fas.business.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
@Slf4j
@Component
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception {
super();
//传输协议需要根据自己的判断
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
public String doPost(String url, String map, String charset) {
org.apache.http.client.HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity stringEntity = new StringEntity(map);
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
......@@ -86,3 +86,10 @@ maparea.action.is-area=action1-2,action1-6
plan.dynamic.execut.topic=\u6362\u6D41\u7AD9\u6D88\u9632\u4E13\u9879\u9884\u6848/autoExec
#服务端获取isdp的token用
sso.client.id=dce
sso.client.secret=6t5oDDKhEODXa++UNUxxLHSF5kVqECq6j+wahtCbv8c=
sso.login.type=server_auth
sso.login.client=dce
sso.client.url=https://198.87.103.88:30443/oauth2/oauth/rest_token
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment