bb905f24168f1be4fd24e17e5e7b48e44fce68f7.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package org.jeecg.modules.cas.util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.security.cert.X509Certificate;
  6. import javax.net.ssl.SSLContext;
  7. import javax.net.ssl.TrustManager;
  8. import javax.net.ssl.X509TrustManager;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
  12. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. public class CASServiceUtil {
  16. public static void main(String[] args) {
  17. String serviceUrl = "https://cas.8f8.com.cn:8443/cas/p3/serviceValidate";
  18. String service = "http://localhost:3003/user/login";
  19. String ticket = "ST-5-1g-9cNES6KXNRwq-GuRET103sm0-DESKTOP-VKLS8B3";
  20. String res = getSTValidate(serviceUrl,ticket, service);
  21. System.out.println("---------res-----"+res);
  22. }
  23. /**
  24. * 验证ST
  25. */
  26. public static String getSTValidate(String url,String st, String service){
  27. try {
  28. url = url+"?service="+service+"&ticket="+st;
  29. CloseableHttpClient httpclient = createHttpClientWithNoSsl();
  30. HttpGet httpget = new HttpGet(url);
  31. HttpResponse response = httpclient.execute(httpget);
  32. String res = readResponse(response);
  33. return res == null ? null : (res == "" ? null : res);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. return "";
  38. }
  39. /**
  40. * 读取 response body 内容为字符串
  41. *
  42. * @param response
  43. * @return
  44. * @throws IOException
  45. */
  46. private static String readResponse(HttpResponse response) throws IOException {
  47. BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  48. String result = new String();
  49. String line;
  50. while ((line = in.readLine()) != null) {
  51. result += line;
  52. }
  53. return result;
  54. }
  55. /**
  56. * 创建模拟客户端(针对 https 客户端禁用 SSL 验证)
  57. *
  58. * @param cookieStore 缓存的 Cookies 信息
  59. * @return
  60. * @throws Exception
  61. */
  62. private static CloseableHttpClient createHttpClientWithNoSsl() throws Exception {
  63. // Create a trust manager that does not validate certificate chains
  64. TrustManager[] trustAllCerts = new TrustManager[]{
  65. new X509TrustManager() {
  66. @Override
  67. public X509Certificate[] getAcceptedIssuers() {
  68. return null;
  69. }
  70. @Override
  71. public void checkClientTrusted(X509Certificate[] certs, String authType) {
  72. // don't check
  73. }
  74. @Override
  75. public void checkServerTrusted(X509Certificate[] certs, String authType) {
  76. // don't check
  77. }
  78. }
  79. };
  80. SSLContext ctx = SSLContext.getInstance("TLS");
  81. ctx.init(null, trustAllCerts, null);
  82. LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(ctx);
  83. return HttpClients.custom()
  84. .setSSLSocketFactory(sslSocketFactory)
  85. .build();
  86. }
  87. }