①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳✕✓✔✖
管理员服务(CAS 客户端)可以以何种方式使用 CAS。服务管理工具的核心组件是服务注册表,它存储一个或多个注册服务。 这里是官网关于 service 的说明
小技巧,通过下面配置,来实现多个配置文件:
spring:profiles:include: authn,service
这里使用 Json 来快速配置一个服务。
//支持service的Json注册方法implementation "org.apereo.cas:cas-server-support-json-service-registry"
在resource/services
,目录下建立一个 Json 文件:web-10000001.json
{"@class": "org.apereo.cas.services.RegexRegisteredService","serviceId": "^(https|imaps|http)://.*","name": "web","id": 10000001,"evaluationOrder": 10}
JSON fileName = serviceName + "-" + serviceNumericId + ".json"
让程序可以通过这个配置文件找到配置的内容
cas:service-registry:json:location: classpath:/serviceswatcher-enabled: true
https://localhost:8443/cas/login?service=http://localhost:9080/sample
输入用户名和密码
然后会跳转到
http://localhost:9080/sample?ticket=ST-1-rQsFdoOpENEFmiCZvmV3tNIUVgs-fanhl-2021
{"@class": "org.apereo.cas.services.RegexRegisteredService","serviceId": "fanhl","name": "web","id": 10000001,"evaluationOrder": 10,"redirectUrl": "https://github.com/fanhualei"}
这里要注意了,如果将redirectUrl
换成http://localhost:9080/sample
会出现错误提示,CAS 好像是只认 HTTPS。
通过下面的链接访问:https://localhost:8443/cas/login?service=fanhl
也是可以跳转到 github 的,也就是通过两个都可以访问。
https://github.com/apereo/cas-sample-java-webapp
<filter><filter-name>CAS Authentication Filter</filter-name><filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class><!-- <filter-class>org.jasig.cas.client.authentication.Saml11AuthenticationFilter</filter-class> --><init-param><param-name>casServerLoginUrl</param-name><!-- <param-value>https://mmoayyed.unicon.net:8443/cas/login</param-value>--><param-value>https://localhost:8443/cas/login</param-value></init-param><init-param><param-name>serverName</param-name><!-- <param-value>https://mmoayyed.unicon.net:9446</param-value>--><param-value>https://localhost:9446</param-value></init-param><!-- <init-param><param-name>authn_method</param-name><param-value>mfa-duo</param-value></init-param> --></filter>
sudo gedit /etc/hosts
编辑的内容
127.0.0.1 mmoayyed.unicon.net
mvn clean package jetty:run-forked
The application will be available on:
http://localhost:9080/samplehttp://mmoayyed.unicon.net:9080/sample
and
https://localhost:9446/samplehttps://mmoayyed.unicon.net:9446/sample
dependencies {implementation "org.springframework.boot:spring-boot-starter-web"implementation "org.springframework.boot:spring-boot-starter-freemarker"implementation "org.jasig.cas.client:cas-client-support-springboot:3.6.2"}
server:port: 11600# ssl:# key-store: file:/etc/cas/thekeystore# key-store-password: changeit# key-store-type: PKCS12spring:freemarker:suffix: .ftltemplate-loader-path:- classpath:/templatescas:server-url-prefix: https://localhost:8443/casserver-login-url: https://localhost:8443/cas/loginclient-host-url: http://localhost:11600/
添加@EnableCasClient
进行验证
@SpringBootApplication(scanBasePackages = "com.wukong")@EnableCasClientpublic class MyApplication_11600 {public static void main(String[] args) throws Exception {SpringApplication.run(MyApplication_11600.class, args);}@PostConstructpublic void run() throws Exception{SslUtil.ignoreSsl();}}
cas 强制要求使用证书,那么在调试过程中会有些麻烦。
再 cas 的CommonUtils.getResponseFromServer
会强制验证证书
通过附加类,让 CAS client 不强制执行 SSL 检查。
创建一个类
/*** java信任SSL证书**/public class SslUtil {/*** 忽略HTTPS请求的SSL证书,必须在openConnection之前调用** @throws Exception*/public static void ignoreSsl() throws Exception {HostnameVerifier hv = new HostnameVerifier() {public boolean verify(String urlHostName, SSLSession session) {return true;}};trustAllHttpsCertificates();HttpsURLConnection.setDefaultHostnameVerifier(hv);}private static void trustAllHttpsCertificates() throws Exception {TrustManager[] trustAllCerts = new TrustManager[1];TrustManager tm = new miTM();trustAllCerts[0] = tm;SSLContext sc = SSLContext.getInstance("SSL");sc.init(null, trustAllCerts, null);HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());}static class miTM implements TrustManager, X509TrustManager {public X509Certificate[] getAcceptedIssuers() {return null;}public boolean isServerTrusted(X509Certificate[] certs) {return true;}public boolean isClientTrusted(X509Certificate[] certs) {return true;}public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {return;}public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {return;}}}
然后在 Application 中调用
@PostConstructpublic void run() throws Exception{SslUtil.ignoreSsl();}
将证书导入到 Jdk 中,这些方法没有实验过,参考了相关的网址。
将证书导入到 Jdk 中。
keytool -import -keystore "JAVA_HOME/jre/lib/security/cacerts" -file <path-to-cert> -alias cas -storepass changeit
这里是 jre,如果是 openjdk11 就没有 jre 了,那么在/lib/security
https://localhost:8443/cas/login?service=http%3A%2F%2Flocalhost%3A11600%2F
注销掉原先的注释,然后配置 yml 文件,其中dialect
要配置,不然会报错。
cas:service-registry:# json:# location: classpath:/services# watcher-enabled: truejpa:driver-class: com.mysql.cj.jdbc.Driveruser: rootpassword: rootmysqlurl: jdbc:mysql://localhost:33061/cas?useSSL=false&serverTimezone=Asia/Shanghaidialect: org.hibernate.dialect.MySQL5Dialect
其中以前配置的 Json 文件要移出,不然会出现错误。
package org.apereo.cas.wukong.controller;import org.apereo.cas.services.ServicesManager;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import java.io.IOException;@RestController@RequestMapping("/services")public class ServicesManagerController {@Autowired@Qualifier("servicesManager")private ServicesManager servicesManager;@RequestMapping(value = "/info", method = RequestMethod.GET)public String info() throws IOException {return "1";}}
不添加找到这个类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.apereo.cas.config.CasOverlayOverrideConfiguration,\org.apereo.cas.wukong.authentication.MyAuthenticationEventExecutionPlanConfiguration,\org.apereo.cas.wukong.controller.ServicesManagerController
一定要加cas
https://localhost:8443/cas/services/info
https://localhost:8443/cas/services/addService/http/localhost/123
./gradlew listTemplateViews
参考文档
打开 casInitializr
./gradlew :app:bootRun
生成 cas-management 代码
mkdir cas-management-overlaycd cas-management-overlaycurl http://localhost:8080/starter.tgz -d "type=cas-management-overlay" | tar -xzvf -# 需要编译./gradlew clean build
注释:也可以从 github 上将cas-management-overlay
下载,但是下载的代码与这个代码不一样。
修改etc/cas/config/management.properties
问价
cas.server.name=https://localhost:8443cas.server.prefix=${cas.server.name}/casmgmt.server-name=https://localhost:8444mgmt.admin-roles[0]=ROLE_ADMINmgmt.user-properties-file=file:/etc/cas/config/users.jsonlogging.config=file:/etc/cas/config/log4j2-management.xml
修改src/main/resources/application.yml
文件
server:port: 8444# ssl:# enabled: false
修改etc/cas/config/users.json
,配置可以登录的用户,这些用户必须在cas-server
中。
{"casuser": {"@class": "org.apereo.cas.mgmt.authz.json.UserAuthorizationDefinition","roles": ["ROLE_ADMIN"]},"user": {"@class": "org.apereo.cas.mgmt.authz.json.UserAuthorizationDefinition","roles": ["ROLE_ADMIN"]},"xiaoyu": {"@class": "org.apereo.cas.mgmt.authz.json.UserAuthorizationDefinition","roles": ["ROLE_ADMIN"]}}
/etc
执行下面命令,会被复制到/etc/cas/config
./gradlew copy
每次修改配置文件,都要进行这个操作。
所有的客户端,如果出现这个错误的时候,PKIX path building failed
,需要配置证书
keytool -import \-keystore $JAVA_HOME/lib/security/cacerts \-trustcacerts \-file /etc/cas/cas.crt \-alias CAScert
输入密码:changeit
点击:是
在 gradle 中配置
//支持service的Json注册方法implementation "org.apereo.cas:cas-server-support-json-service-registry"
修改resources/application.yml
,这里配置与cas-server
的配置文件一致
cas:service-registry:json:location: file:/home/fanhl/01-java/cas/cas-overelay/src/main/resources/serviceswatcher-enabled: true
执行下面的命令,
# 会将/etc/cas/config下面的配置文件,复制到/etc/cas/config$./gradlew clean build./gradlew run
/etc/cas/config