SSLContext 相关的 JAAS

时间: 2023-06-22 14:48:23 浏览: 39
JAAS(Java Authentication and Authorization Service)是 Java 中的一种安全框架,它提供了一种标准的方式来实现用户身份验证和访问控制。在使用 SSL/TLS 时,JAAS 可以用于配置和管理 SSLContext。 下面是一个示例,演示如何使用 JAAS 来配置 SSLContext: ``` import javax.net.ssl.SSLContext; import javax.security.auth.login.AppConfigurationEntry; import javax.security.auth.login.Configuration; import javax.security.auth.login.LoginContext; import java.security.KeyStore; import java.security.SecureRandom; import java.util.HashMap; import java.util.Map; public class SSLContextWithJAAS { public static void main(String[] args) throws Exception { // 配置 JAAS System.setProperty("java.security.auth.login.config", "/path/to/jaas.conf"); // 创建 SSLContext SSLContext sslContext = SSLContext.getInstance("TLS"); // 创建 KeyStore KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(SSLContextWithJAAS.class.getResourceAsStream("/client.jks"), "password".toCharArray()); // 创建 KeyManager KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, "password".toCharArray()); KeyManager[] keyManagers = keyManagerFactory.getKeyManagers(); // 创建 TrustManager TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } }; // 配置 JAAS 登录上下文 Map<String, Object> jaasOptions = new HashMap<>(); jaasOptions.put("keyStore", keyStore); AppConfigurationEntry[] appConfigurationEntries = new AppConfigurationEntry[] { new AppConfigurationEntry("com.sun.security.auth.module.KeyStoreLoginModule", AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, jaasOptions) }; Configuration configuration = new Configuration() { @Override public AppConfigurationEntry[] getAppConfigurationEntry(String name) { return appConfigurationEntries; } }; LoginContext loginContext = new LoginContext("SSLClient", null, null, configuration); loginContext.login(); // 使用 JAAS 配置 SSLContext sslContext.init(keyManagers, trustManagers, new SecureRandom(), loginContext.getSubject().getPrivateCredentials(KeyStore.PrivateKeyEntry.class).toArray(new KeyStore.PrivateKeyEntry[0])); // 使用 SSLContext 进行 SSL/TLS 通信 SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket("example.com", 443); sslSocket.startHandshake(); // ... } } ``` 在这个示例中,我们使用 JAAS 配置了一个登录上下文,将 KeyStore 传递给 `KeyStoreLoginModule`,并设置了登录模式为 `REQUIRED`。然后,我们使用登录上下文的私有凭证初始化了 SSLContext。 需要注意的是,JAAS 配置文件中需要配置 `KeyStoreLoginModule`,并指定相应的参数。在本示例中,`jaas.conf` 文件内容如下: ``` SSLClient { com.sun.security.auth.module.KeyStoreLoginModule required keyStore="/path/to/client.jks" storePass="password"; }; ``` 其中,`SSLClient` 是 JAAS 登录上下文的名称,`keyStore` 指定 KeyStore 的路径,`storePass` 指定 KeyStore 的密码。

相关推荐

public SSLContext loadTwoWayTrust(RestSystemConfigDto restSystemConfigDto) { SSLContext sslContext = null; String keyStoreFile = restSystemConfigDto.getKeyStoreFile(); String keyPass = restSystemConfigDto.getKeyStorePass(); String trustKeyStoreFile = restSystemConfigDto.getTrustStoreFile(); String trustKeyPass = restSystemConfigDto.getTrustStorePass(); String keystoreType = restSystemConfigDto.getKeyStoreType(); // 判断是否正确配置keystore以及trust keystore信息 if (StringUtil.isNotEmpty(keyStoreFile) && StringUtil.isNotEmpty(keyPass) && StringUtil.isNotEmpty( trustKeyStoreFile) && StringUtil.isNotEmpty(trustKeyPass)) { FileInputStream trustIns = null; FileInputStream keyStoreIns = null; try { KeyStore trustStore = KeyStore.getInstance(keystoreType); KeyStore keyStore = KeyStore.getInstance(keystoreType); trustIns = new FileInputStream(trustKeyStoreFile); keyStoreIns = new FileInputStream(keyStoreFile); trustStore.load(trustIns, trustKeyPass.toCharArray()); keyStore.load(keyStoreIns, keyPass.toCharArray()); sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, keyPass.toCharArray()) .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build(); } catch (Exception ex) { logger.debug("load keystore or trust keystore file failure.", ex); sslContext = loadAllTrust(); } finally { try { if (trustIns != null) { trustIns.close(); } if (keyStoreIns != null) { keyStoreIns.close(); } } catch (IOException e) { logger.warn("Close KeyStore Stream Failed", e); } } } else { logger.debug("not found keystore or trust keystore."); sslContext = loadAllTrust(); } return sslContext; }

java.security path: D:\biayu\jdk\jre\lib\security Security providers: [SUN version 1.8, SunRsaSign version 1.8, SunEC version 1.8, SunJSSE version 1.8, SunJCE version 1.8, SunJGSS version 1.8, SunSASL version 1.8, XMLDSig version 1.8, SunPCSC version 1.8, SunMSCAPI version 1.8] SSLContext provider info: Sun JSSE provider(PKCS12, SunX509/PKIX key/trust factories, SSLv3/TLSv1/TLSv1.1/TLSv1.2/TLSv1.3) SSLContext provider services: [SunJSSE: KeyPairGenerator.RSA -> sun.security.rsa.RSAKeyPairGenerator$Legacy aliases: [OID.1.2.840.113549.1.1, 1.2.840.113549.1.1, 1.2.840.113549.1.1.1] , SunJSSE: KeyFactory.RSA -> sun.security.rsa.RSAKeyFactory$Legacy aliases: [OID.1.2.840.113549.1.1, 1.2.840.113549.1.1, 1.2.840.113549.1.1.1] , SunJSSE: Signature.SHA1withRSA -> sun.security.rsa.RSASignature$SHA1withRSA aliases: [OID.1.2.840.113549.1.1.5, 1.2.840.113549.1.1.5, 1.3.14.3.2.29] attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey} , SunJSSE: SSLContext.TLS -> sun.security.ssl.SSLContextImpl$TLSContext aliases: [SSL] , SunJSSE: SSLContext.TLSv1 -> sun.security.ssl.SSLContextImpl$TLS10Context aliases: [SSLv3] , SunJSSE: Signature.MD2withRSA -> sun.security.rsa.RSASignature$MD2withRSA aliases: [OID.1.2.840.113549.1.1.2, 1.2.840.113549.1.1.2] attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey} , SunJSSE: Signature.MD5withRSA -> sun.security.rsa.RSASignature$MD5withRSA aliases: [OID.1.2.840.113549.1.1.4, 1.2.840.113549.1.1.4] attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey} , SunJSSE: KeyStore.PKCS12 -> sun.security.pkcs12.PKCS12KeyStore , SunJSSE: TrustManagerFactory.SunX509 -> sun.security.ssl.TrustManagerFactoryImpl$SimpleFactory , SunJSSE: KeyManagerFactory.SunX509 -> sun.security.ssl.KeyManagerFactoryImpl$SunX509 , SunJSSE: TrustManagerFactory.PKIX -> sun.security.ssl.TrustManagerFactoryImpl$PKIXFactory aliases: [SunPKIX, X509, X.509] , SunJSSE: KeyManagerFactory.NewSunX509 -> sun.security.ssl.KeyManagerFactoryImpl$X509 aliases: [PKIX] , SunJSSE: SSLContext.Default -> sun.security.ssl.SSLContextImpl$DefaultSSLContext , SunJSSE: SSLContext.TLSv1.1 -> sun.security.ssl.SSLContextImpl$TLS11Context , SunJSSE: Signature.MD5andSHA1withRSA -> sun.security.ssl.RSASignature , SunJSSE: SSLContext.TLSv1.3 -> sun.security.ssl.SSLContextImpl$TLS13Context , SunJSSE: SSLContext.TLSv1.2 -> sun.security.ssl.SSLContextImpl$TLS12Context ] java.ext.dirs: D:\biayu\jdk\jre\lib\ext;C:\Windows\Sun\Java\lib\ext 2023-07-14 09:02:08.895 ERROR 6772 --- [ main] com.zaxxer.hikari.pool.HikariPool : master - Exception during pool initialization. com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client preferences [TLS13, TLS12]”。 ClientConnectionId:275886a9-fcb1-451b-af33-3b3ea3195ee6

最新推荐

recommend-type

Java实现SSL双向认证的方法

主要介绍了Java实现SSL双向认证的方法,实例分析了ssl认证的原理与相关实现技巧,需要的朋友可以参考下
recommend-type

WebSphere配置SSL指南 .doc

WebSphere配置SSL指南,指导你配置SSL证书,启动https访问服务,实现ssl验证。配置证书登录,访问。
recommend-type

Domino SSL配置详解

Domino SSL配置详解,图示实例,简单方便,照着配置即可。 Domino关于SSL的配置资料网上不多,大家可以参考一下。
recommend-type

MySQL 使用 SSL 连接配置详解

本文给大家分享的是如何配置MySQL支持SSL连接方式的方法以及在docker中配置的具体案例,有需要的小伙伴可以参考下
recommend-type

java访问IBM MQ SSL加密通道

描述了java程序代码去访问MQ的SSL加密的通道。如何配置JKS,如何配置MQ服务器的SSL秘钥库,如何配置证书制作证书和秘钥库。主要是如何编写java代码去访问SSL通道并取到数据。
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

Redis验证与连接:快速连接Redis服务器指南

![Redis验证与连接:快速连接Redis服务器指南](https://img-blog.csdnimg.cn/20200905155530592.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzNTg5NTEw,size_16,color_FFFFFF,t_70) # 1. Redis验证与连接概述 Redis是一个开源的、内存中的数据结构存储系统,它使用键值对来存储数据。为了确保数据的安全和完整性,Redis提供了多
recommend-type

gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker app:app 报错 ModuleNotFoundError: No module named 'geventwebsocket' ]

这个报错是因为在你的环境中没有安装 `geventwebsocket` 模块,可以使用下面的命令来安装: ``` pip install gevent-websocket ``` 安装完成后再次运行 `gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker app:app` 就不会出现这个报错了。
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。