1. 程式人生 > 其它 >CAS5.3版本單點登入伺服器-支援https請求

CAS5.3版本單點登入伺服器-支援https請求

cas單點登入支援https請求的設定步驟總結。

生成ssl證書

步驟可參考,連線中引數的說明也很全:Tomcat配置https方式訪問

直接說下我的執行命令

keytool -genkeypair -alias cas.test.org -keyalg RSA -keystore e:\bo.keystore -storetype pkcs12
引數說明:

-genkeypair:生成一對非對稱金鑰並將公鑰包裝到X.509 v3自簽名證書中;

-alias:指定金鑰條目的別名,該別名是公開的;

-keyalg:指定加密演算法,本例中的採用通用的RSA加密演算法;

-keystore:指定金鑰庫的路徑及名稱,若金鑰庫不存在則建立。若不指定則預設在作業系統的使用者目錄下生成一個"
.keystore"的檔案; -storetype:指定金鑰庫的型別,如果不指定,預設是JKS。如果建立預設型別金鑰庫,命令列會提示轉化為pkcs12型別,所以這裡在建立時指定; 注: 1、執行上面命令後需要輸入金鑰庫的口令,該口令需要配置在tomcat中,切記。 2、金鑰庫的密碼至少必須6個字元,可以是純數字或者字母或者數字和字母的組合等 3、"名字與姓氏"應該是輸入域名,而不是我們的個人姓名,其他的可以不填

配置tomcat伺服器

開啟"<tomcat安裝目錄>\conf\server.xml"配置檔案,找到如下注釋的程式碼行:

    <!--
    <Connector port="
8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> -->

修改為

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads
="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="E:\bo.keystore" keystorePass="123456" />

修改Nacos配置

使用https傳送請求,指向cas服務

#cas單點登入
cas:
  prefixUrl: https://192.168.1.59:8443/cas

修改前端

.env.development中

VUE_APP_CAS_BASE_URL=https://192.168.1.59:8443/cas

sso.js 檔案中不需要修改,sso檔案中的http不需要修改,如果cas訪問系統也要使用https協議,就需要改。

修改後端

為了避免需要證書,所以用一個類繼承DefaultHttpClient類,忽略校驗過程。

package org.jeecg.modules.cas.util;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
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.impl.client.DefaultHttpClient;

/**
 * @Author: qiaochengqiang
 * @Date: 2021/12/24       
 * @Description: 用於進行Https請求的HttpClient
 **/
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));
    }
}

CASServiceUtil.java,直接使用上邊編寫的類生成client

/**
     * 驗證ST
     */
    public static String getSTValidate(String url,String st, String service){
        try {
            url = url+"?service="+service+"&ticket="+st;
            //CloseableHttpClient httpclient = createHttpClientWithNoSsl();
            CloseableHttpClient httpclient = new SSLClient();
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpget);
            String res = readResponse(response);
            return res == null ? null : (res == "" ? null : res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }