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 "";
    }
原文地址:https://www.cnblogs.com/qcq0703/p/15728591.html