CAS單點登錄-https配置(四)

CAS單點登錄-https配置(四)

注意:配置的單點登錄版本爲cas-5.1.3

若需要上個版本代碼,可以點擊下載:GitHub 碼雲

這裏寫圖片描述

上圖是未支持https時默認登錄頁

  • 自簽名服務端需要導入證書

重點:

  • passport.sso.com 是我們測試的域名
  • 測試時需要把本地機的ip映射爲passport.sso.com設到系統HOSTS文件

證書處理

密鑰生成

生成步驟,各參數含義:

  • -genkeypair 生成密鑰
  • -keyalg 指定密鑰算法,這時指定RSA,
  • -keysize 指定密鑰長度,默認是1024位,這裏指定2048,長一點,我讓你破解不了(哈哈…),
  • -siglag 指定數字簽名算法,這裏指定爲SHA1withRSA算法
  • -validity 指定證書有效期,這裏指定36500天,也就是100年,我想我的應用用不到那麼長時間
  • -alias 指定別名,這裏是cas.server.com
  • -keystore 指定密鑰庫存儲位置,這裏存在d盤
  • -dname 指定用戶信息,不用一個一個回答它的問題了;

    注意:CN=域名,我們採用passport.sso.com

keytool -genkeypair -keyalg RSA -keysize 2048 -sigalg SHA1withRSA -validity 36500 -alias passport.sso.com -keystore d:/tomcat.keystore -dname "CN=passport.sso.com,OU=kawhi,O=carl,L=GuangZhou,ST=GuangDong,C=CN"

輸入上述命令,下面密碼我們輸入123456,然後一直回車,就在d盤生成了tomcat.keystore文件;

證書生成

在cmd下輸入如下命令,密碼爲上面輸入的123456:

keytool -exportcert -alias passport.sso.com -keystore d:/tomcat.keystore  -file d:/tomcat.cer -rfc

證書生成在:d:/tomcat.cer

導入cacerts證書庫

輸入密碼爲 changeit並同意導入

keytool -import -alias passport.sso.com -keystore %JAVA_HOME%\jre\lib\security\cacerts -file d:/tomcat.cer -trustcacerts

檢查是否導入成功

keytool -list -keystore "%JAVA_HOME%\jre\lib\security\cacerts" | findstr/i server

有東西輸出代表成功

項目設置

證書有了,我們必須讓項目識別證書,並且把ssl開關打開

密鑰引用

tomcat.keystore拷貝到sso-server\src\main\resources

開啓ssl

文件sso-config\src\main\resources\config\sso-dev.properties調整

  1. 調整server.ssl.enabled=true
  2. 新增如下
server.ssl.key-store=classpath:tomcat.keystore
server.ssl.key-store-password=123456
server.ssl.keyAlias=passport.sso.com

最終配置文件sso-dev.properties:

#
# 版權所有.(c)2008-2017. 卡爾科技工作室
#

##
# CAS Server Context Configuration
#
server.context-path=/cas
server.port=8443


#SSL配置
server.ssl.enabled=true
server.ssl.key-store=classpath:tomcat.keystore
server.ssl.key-store-password=123456
server.ssl.keyAlias=passport.sso.com
#SSL配置


server.max-http-header-size=2097152
server.use-forward-headers=true
server.connection-timeout=20000
server.error.include-stacktrace=NEVER

server.tomcat.max-http-post-size=2097152
server.tomcat.basedir=build/tomcat
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)
server.tomcat.accesslog.suffix=.log
server.tomcat.max-threads=10
server.tomcat.port-header=X-Forwarded-Port
server.tomcat.protocol-header=X-Forwarded-Proto
server.tomcat.protocol-header-https-value=https
server.tomcat.remote-ip-header=X-FORWARDED-FOR
server.tomcat.uri-encoding=UTF-8

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

##
# CAS Cloud Bus Configuration
#
spring.cloud.bus.enabled=false

endpoints.enabled=false
endpoints.sensitive=true

endpoints.restart.enabled=false
endpoints.shutdown.enabled=false

management.security.enabled=true
management.security.roles=ACTUATOR,ADMIN
management.security.sessions=if_required
management.context-path=/status
management.add-application-context-header=false

security.basic.authorize-mode=role
security.basic.enabled=false
security.basic.path=/cas/status/**

##
# CAS Web Application Session Configuration
#
server.session.timeout=300
server.session.cookie.http-only=true
server.session.tracking-modes=COOKIE

##
# CAS Thymeleaf View Configuration
#
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=true
spring.thymeleaf.mode=HTML
##
# CAS Log4j Configuration
#
# logging.config=file:/etc/cas/log4j2.xml
server.context-parameters.isLog4jAutoInitializationDisabled=true

##
# CAS AspectJ Configuration
#
spring.aop.auto=true
spring.aop.proxy-target-class=true

##
# CAS Authentication Credentials
#
cas.authn.accept.users=casuser::Mellon

測試

再次說明: 把本機的ip映射爲passport.sso.com設到系統HOSTS文件
這裏寫圖片描述

127.0.0.1 passport.sso.com
  1. 重啓配置中心
  2. 啓動sso-server
  3. 訪問 https://passport.sso.com:8443/cas/login 如下圖所示

這裏寫圖片描述


下載本章代碼:GitHub

作者聯繫方式

如果技術的交流或者疑問可以聯繫或者提出issue。

郵箱:[email protected]

QQ: 756884434 (請註明:SSO-CSDN)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章