CAS5.3服务器搭建与客户端整合SpringBoot以及踩坑笔记

cas服务器的搭建
  1. 导出证书(1和2步骤是找了课程,随便写了一下存记录,不过对于自己测试不投入使用应该不影响)

    C:\Users\Ddlm2>keytool -genkey -alias testcas -keystore D:/testcas -storepass 123456 -keyalg RSA -validity 3650
    您的名字与姓氏是什么?
    [Unknown]: test
    您的组织单位名称是什么?
    [Unknown]: test
    您的组织名称是什么?
    [Unknown]: test
    您所在的城市或区域名称是什么?
    [Unknown]: test
    您所在的省/市/自治区名称是什么?
    [Unknown]: test
    该单位的双字母国家/地区代码是什么?
    [Unknown]: test
    CN=test, OU=test, O=test, L=test, ST=test, C=test是否正确?
    [否]: Y 输入 <testcas> 的密钥口令
    (如果和密钥库口令相同, 按回车):
    再次输入新口令: Warning:
    JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore D:/testcas -destkeystore D:/testcas -deststoretype pkcs12" 迁 移到行业标准格式 PKCS12。 C:\Users\Ddlm2>keytool -genkey -alias testcas -keystore D:/testcas -storepass 123456 -keyalg RSA -validity 3650 -export -trustcacerts -file D:/testcas.cer
    存储在文件 <D:/testcas.cer> 中的证书 Warning:
    JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore D:/testcas -destkeystore D:/testcas -deststoretype pkcs12" 迁 移到行业标准格式 PKCS12。
  2. 导入jdk的证书库

    C:\Users\Ddlm2>keytool -import -trustcacerts -alias testcas -file D:/testcas.cer
    输入密钥库口令:
    再次输入新口令:
    所有者: CN=test, OU=test, O=test, L=test, ST=test, C=test
    发布者: CN=test, OU=test, O=test, L=test, ST=test, C=test
    序列号: 429f9b95
    生效时间: Fri Aug 13 11:15:22 CST 2021, 失效时间: Mon Aug 11 11:15:22 CST 2031
    证书指纹:
    SHA1: 3A:20:B4:25:12:96:6E:EB:F7:15:15:BE:11:A7:C5:66:60:25:D6:A8
    SHA256: 06:80:FC:34:63:27:20:ED:6E:74:CB:51:2B:5B:86:E0:70:C2:CD:65:36:46:52:13:25:0A:B4:9B:7F:67:31:B7
    签名算法名称: SHA256withRSA
    主体公共密钥算法: 2048 位 RSA 密钥
    版本: 3 扩展: #1: ObjectId: 2.5.29.14 Criticality=false
    SubjectKeyIdentifier [
    KeyIdentifier [
    0000: 24 05 3F B0 42 A4 02 84 70 D1 F2 09 4A AB D8 93 $.?.B...p...J...
    0010: 8B 29 85 BB .)..
    ]
    ] 是否信任此证书? [否]: y
    证书已添加到密钥库中

    注:应该是在webapps\cas\WEB-INF\classes中的application.properties中配置(开头7行左右)

    不过自己测试,只会提示密码泄露不安全什么的,无大碍。

  3. 下载 cas server 安装包,这里使用的是5.3。下面的链接都可以使用

    https://github.com/apereo/cas-overlay-template/tree/5.3
    ----------------------------------------
    https://wwa.lanzoui.com/b010pvygd
    密码:casserver
  4. 打war包。 安装maven的环境变量(或者导入idea打包),在cas-overlay-template-5.3目录打开cmd命令行使用mvn package进行打包,会在target目录下出现cas.war文件,

  5. 部署tomcat。 将本地tomcat服务器拷贝出来一份,作为cas服务器,将war包复制到webapps下面。运行tomcat,war包会自动解压到car文件夹中。

    注:如果cas使用5以上,tomcat必须8以上。

  6. 端口映射。 在tomcat目录下的conf目录下的server.xml中添加

    <Connector port="8332" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" />
    <!-- 意为将8332接口转到8443接口 -->
    <!-- 8332是对外的,那么8443呢,cas默认端口就是8443,applicaion.properties中的第5行左右 -->

    注:主要为了防止端口号冲突,都可以任意修改。

  7. 添加http协议。 webapps\cas\WEB-INF\classes\services下的HTTPSandIMAPS-10000001.json的第三行修改成"serviceId" : "^(https|http|imaps)://.*",使其接受http协议。

  8. 也可以自定义用户名。 webapps\cas\WEB-INF\classes下的application.properties 最后那里

    cas.authn.accept.users=casuser::Mellon

    是默认的用户名和密码,可以修改为自己喜欢的。

  9. 重启tomcat, 访问localhost:8332/cas/login输入7步骤中的账号密码进行测试。


接下来整理,springboot整合cas client。cas服务器端返回数据在下面。


SpringBoot整合cas客户端
  1. 新建maven或者springboot项目都可以,打开pom文件添加依赖。

    <dependencies>
    
        <!-- cas的客户端 -->
    <dependency>
    <groupId>net.unicon.cas</groupId>
    <artifactId>cas-client-autoconfig-support</artifactId>
    <version>2.2.0-GA</version>
    <exclusions>
    <exclusion>
    <groupId>org.jasig.cas.client</groupId>
    <artifactId>cas-client-core</artifactId>
    </exclusion>
    </exclusions>
    </dependency> <!-- 使用更高版本的cas-client-core -->
    <dependency>
    <groupId>org.jasig.cas.client</groupId>
    <artifactId>cas-client-core</artifactId>
    <version>3.5.0</version>
    </dependency> <!-- 故意降的版本 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.1.3.RELEASE</version>
    </dependency> </dependencies>

    注:若启动时候报错,且去掉cas相关东西又可以正常启动。可以尝试降一下你自己的springboot版本。

  2. 添加启动类,切记@EnableCasClient

    import net.unicon.cas.client.configuration.EnableCasClient;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication; /**
    * @author ddlm
    * @date 2021/08/17 10:53
    */
    @EnableCasClient
    @SpringBootApplication
    public class CASClientOne {
    public static void main(String[] args) {
    SpringApplication.run(CASClientOne.class, args);
    }
    }
  3. 添加配置,这里使用的yml,properties就是:变.和=而已。

    server:
    port: 9101 cas:
    # cas服务器端的地址和端口,注意端口号。
    server-url-prefix: http://localhost:8332/cas
    server-login-url: http://localhost:8332/cas/login
    # 本地的地址,用于回调回来
    client-host-url: http://localhost:9101
    # 需要拦截的地址(所有为"/*"),可配置数组
    authentication-url-patterns:
    - "/auth/login"
    - "/auth/createOrder"
    validation-type: CAS3

    注:拦截的接口是个例子。因为使用场景场景不同:全由cas控制或者只需要登录在自己项目控制。

    如果服务端不需要关,只需要使用cas实现单点登录,且获取数据,如果从session中获取,需要配置中开启一下:

    cas: use-session: true,对应获取数据就request.getSession().getAttribute("key");

  4. 随便写个接口测试。

    @RestController
    public class TestController { @RequestMapping("/testFirst")
    public String TestMethod() {
    return "This is First Method.";
    }
    }

    注:确保url在配置中的拦截范围内。

  5. 为了方便对比,再新建一个项目,步骤一致,起码接口名、端口名和返回内容修改一下。

  6. 启动两个项目,访问第一个项目中的接口,需要登录,登录后。

  7. 访问第二个项目的接口,此时不需要登录即为成功。

  8. 扩展: 一些博客中,结合了拦截器进行匹配,这个看需要进行添加,主要是可以忽略接口,加几层一般都可以避免,如果使用过滤器注意与配置的交集。

    @Configuration
    public class CASConfig { @Value("${cas.server-url-prefix}")
    private String serverUrlPrefix;
    @Value("${cas.server-login-url}")
    private String serverLoginUrl;
    @Value("${cas.client-host-url}")
    private String clientHostUrl;
    @Value("${ignore-host-url}")
    private String ignoreHostUrl; /**
    * 授权过滤器
    */
    @Bean
    public FilterRegistrationBean filterAuthenticationRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new AuthenticationFilter());
    // 设定匹配的路径
    registration.addUrlPatterns("/*");
    Map<String,String> initParameters = new HashMap<>();
    initParameters.put("casServerLoginUrl", serverUrlPrefix);
    initParameters.put("serverName", clientHostUrl);
    //忽略的url,"|"分隔多个url
    initParameters.put("ignorePattern", ignoreHostUrl);
    registration.setInitParameters(initParameters);
    // 设定加载的顺序
    registration.setOrder(1);
    return registration;
    }
    }

    其中ignore-host-url需要另外在yml中配置。


接下来配置cas服务器从数据库中获取用户名和密码,并返回该人员所有信息(只是测试使用)

cas服务器连接数据库
  1. 获取登录名。为什么多返回更多内容呢,因为用户名默认会返回,这里介绍获取用户名的三种方法。

    request.getRemoteUser();
    
    ((Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION)).getPrincipal().getName();
    
    第二步不getName(),getAttributes();在得到的集合中去get("username"),大概,可以打印出来找一下。

    注:从request中能get到的都get过来了,然后转成json输出找到的...

  2. CAS服务器配置连接数据库。

    我测试时候是重新打包的,也可以参考这位大佬的博客tomcat8搭建cas服务器,使用MYSQL数据库

    或者这位大佬的:cas5.3.1 从搭建到连接mysql(简而优美),是直接复制jar包进来,不需要重新打包的。

    添加需要访问数据库的依赖,在cas-overlay-template-5.3/pom.xml

    <dependency>
    <groupId>org.apereo.cas</groupId>
    <artifactId>cas-server-support-jdbc</artifactId>
    <version>${cas.version}</version>
    </dependency>
    <dependency>
    <groupId>org.apereo.cas</groupId>
    <artifactId>cas-server-support-jdbc-drivers</artifactId>
    <version>${cas.version}</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <!-- 与数据库版本一致 -->
    <version>8.0.23</version>
    </dependency>

    注:切记最后面那个版本号一定一定一定要跟自己mysql的版本一致。可百度查看如何查看MySQL版本。

    ​ 小版本号也要保证一致!!!

  3. 打包,部署,重新按照服务器的部署修改一下。

  4. 修改 webapps\cas\WEB-INF\classes下的application.properties最后部分,注释掉默认或者自定义的账号密码:

    # cas.authn.accept.users=casuser::Mellon

    添加:

    cas.authn.jdbc.query[0].url=jdbc:mysql://localhost:3306/cas_server_demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
    cas.authn.jdbc.query[0].principalAttributeList=id,username,password,stunumber,stuclass
    cas.authn.jdbc.query[0].user=root
    cas.authn.jdbc.query[0].password=123456
    cas.authn.jdbc.query[0].sql=select id,username,password,stu_number as stuNumber,stu_class as stuClass from student where username=?
    cas.authn.jdbc.query[0].fieldPassword=password
    # cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
    cas.authn.jdbc.query[0].driverClass=com.mysql.cj.jdbc.Driver # 忽略https安全协议,使用 HTTP 协议
    cas.tgc.secure=false
    # 是否开启json识别功能,默认为false
    cas.serviceRegistry.initFromJson=true
    # 指明路径
    cas.serviceRegistry.json.location=classpath:/services

    注:

    1. principalAttributeList中是返回的字段名,如果想要定义别名,需要在sql中使用as重新命名。
    2. 注意mysql版本号与driverClass的对应。
    3. 未涉及密码加密。
    4. 当不需要定义别名时候,sql可以直接select *
    5. 见网上说数据库字段名带下划线返回不回来,经过测试可以返回,如果失败定义别名可解决。
  5. 修改webapps\cas\WEB-INF\classes\services\HTTPSandIMAPS-10000001.json,最后添加:

    {
    "@class" : "org.apereo.cas.services.RegexRegisteredService",
    "serviceId" : "^(https|http|imaps)://.*",
    "name" : "HTTPS and IMAPS",
    "id" : 10000001,
    "description" : "This service definition authorizes all application urls that support HTTPS and IMAPS protocols.",
    "evaluationOrder" : 10000,
    "attributeReleasePolicy" : {
    "@class" : "org.apereo.cas.services.ReturnAllAttributeReleasePolicy"
    }
    }

    添加attributeReleasePolicy,注意json格式,上面要添加,

  6. 准备MySQL数据库数据

    /*
    Navicat Premium Data Transfer Source Server : localMysql
    Source Server Type : MySQL
    Source Server Version : 80023
    Source Host : localhost:3306
    Source Schema : cas_server_demo Target Server Type : MySQL
    Target Server Version : 80023
    File Encoding : 65001 Date: 17/08/2021 13:56:44
    */ SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
    -- Table structure for student
    -- ----------------------------
    DROP TABLE IF EXISTS `student`;
    CREATE TABLE `student` (
    `id` int(0) NOT NULL AUTO_INCREMENT,
    `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
    `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
    `stu_number` int(0) NULL DEFAULT NULL,
    `stu_class` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
    PRIMARY KEY (`id`) USING BTREE
    ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; -- ----------------------------
    -- Records of student
    -- ----------------------------
    INSERT INTO `student` VALUES (1, 'admin', 'admin', 222222424, '234');
    INSERT INTO `student` VALUES (2, 'test1', 'test1', 123454321, '235'); SET FOREIGN_KEY_CHECKS = 1;
  7. 重启tomcat,注意启动日志。

  8. springboot cas client取数据,我整理了一个简易工具类。

    import org.jasig.cas.client.authentication.AttributePrincipal;
    import org.jasig.cas.client.util.AbstractCasFilter;
    import org.jasig.cas.client.validation.Assertion; import javax.servlet.http.HttpServletRequest;
    import java.util.Map; public class CASUtil {
    private static HttpServletRequest request; /**
    * 从cas中获取用户名
    *
    * @param request Http请求
    * @return 登录cas服务器的用户名
    */
    public static String getAccountNameFromCas(HttpServletRequest request) {
    CASUtil.request = request;
    Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
    if (assertion != null) {
    AttributePrincipal principal = assertion.getPrincipal();
    return principal == null ? null : principal.getName();
    } else {
    return null;
    }
    } /**
    * 从cas返回值中获取内容
    *
    * @param request Http 请求
    * @param attr 需要获取值的键名
    * @return 键名attr对应的值
    */
    public static String getAttributeFromCas(HttpServletRequest request, String attr) {
    CASUtil.request = request;
    Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION); if (assertion != null) {
    AttributePrincipal principal = assertion.getPrincipal();
    // 一般不会为空,因为有attributes
    if(principal != null) {
    Object obj = principal.getAttributes().get(attr);
    return obj == null ? null : obj.toString();
    }
    }
    return null;
    }
    }
  9. 至此,完毕。

  10. 整理要点

    1. cas服务器

      1. 端口(server.xml)
      2. 连接数据库(application.properties)
      3. 修改json配置文件(HTTPSandIMAPS-10000001.json)
      4. 连接数据库依赖的驱动版本号
      5. cas与tomcat版本
    2. cas客户端
      1. 依赖版本号(cas和springboot的)
      2. @EnableCasClient注解
      3. 配置文件中的cas服务器地址、本机地址和需要验证的接口
      4. 通过request获取即可
  11. 当用户名登录不上时候,查看cas服务器命令行是否有异常。

  12. 参考:

    1. SpringBoot 搭建CAS 客户端 和CAS 服务端
    2. 从零开始部署CAS服务器
    3. cas5.3.1 从搭建到连接mysql(简而优美)
    4. cas服务端搭建
    5. CASServer5.3.* 返回多值及Spring Boot集成获取用户属性数据

CAS5.3服务器搭建与客户端整合SpringBoot以及踩坑笔记的更多相关文章

  1. CAS5.3服务器搭建及SpringBoot整合CAS实现单点登录

    1.1 什么是单点登录 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的 ...

  2. 【原】无脑操作:Gitblit服务器搭建及IDEA整合Git使用

    背景:虽然有GitHub.GitLab这样强大的Git仓库,但是涉及私有Git库要收费,所以自己动手搭建免费的用用 环境:windows 7 旗舰版.JDK 1.8.IDEA 2017 ------- ...

  3. 3-Gitblit服务器搭建及IDEA整合Git使用

    背景:虽然有GitHub.GitLab这样强大的Git仓库,但是涉及私有Git库要收费,所以自己动手搭建免费的用用 环境:windows 7 旗舰版.JDK 1.8.IDEA 2017 ------- ...

  4. solr服务器搭建与Tomact整合及使用

    一:solr服务器的搭建 1:搭建全新的为solr专用的solr服务器: 在自己电脑上搭建两台Tomact服务器,一台仍为应用服务器,一台作为solr服务器,应用服务器按照正常Tomact服务器搭建即 ...

  5. SVN服务器搭建及客户端配置

    为什么要使用SVN? 在程序的编写过程中,每个程序员都会负责开发一个或多个模块,且开发中会生成很多不同的版本, 这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subvers ...

  6. [源码管理] ubuntu中svn简明用法:服务器搭建+客户端使用

    本文是对网络上前人的优秀文章加以实践验证后所整理(修正或补充) 第一部分:svn服务器搭建(主要是四步走) 参考:http://www.son1c.cn/show/920.html 一,安装Subve ...

  7. Linux Centos6.5 SVN服务器搭建 以及客户端安装

    转载:http://www.cnblogs.com/mymelon/p/5483215.html /******开始*********/ 系统环境:Centos 6.5 第一步:通过yum命令安装sv ...

  8. SIP协议&开源SIP服务器搭建和客户端安装

    1. SIP SIP 是一个应用层的控制协议,可以用来建立,修改,和终止多媒体会话,例如Internet电话 SIP在建立和维持终止多媒体会话协议上,支持五个方面: 1)   用户定位: 检查终端用户 ...

  9. ntp服务器搭建与客户端设置

    服务器对时间要求非常严格,linux服务器文件的生成,日志,以及数据库的连接都是根据时间的逻辑 进行任务处理,如果时间不同步,那么,处理的任务,以及出问题的分析日志,时间不对,很难分析 直接更改lin ...

随机推荐

  1. ACdream 1007 a+b 快速幂 java秒啊,快速幂 避免 负数移位出错

    a + b ( sigma  (ai^x)  )  %  mod 1 import java.util.*; 2 import java.math.*; 3 import java.io.*; 4 p ...

  2. 资源:Kafka消息队列下载路径

    Kafka下载路径 http://kafka.apache.org/downloads.html

  3. Java 设置PDF跨页表格重复显示表头行

    在创建表格时,如果表格内容出现跨页显示的时候,默认情况下该表格的表头不会在下一页显示,在阅读体验上不是很好.下面分享一个方法如何在表格跨页是显示表格的表头内容,这里只需要简单使用方法 grid.set ...

  4. 不安装任何软件或脚本使用powershell快速计算文件的MD5/SHA1/SHA256等校验值

    有的时候在检查升级过程中需要对xml文件进行签名,而xml文件中一般都需要包含安装包的SHA256值,这里分享一个使用PowerShell快速计算SHA256等其他值的方法. 一.在需要计算文件SHA ...

  5. HAL库直流电机编码测速(L298N驱动)笔记

    主函数开始后的处理流程: 1.外设初始化:HAL_Init() 2.系统时钟配置 RCC振荡器初始化:HAL_RCC_OsConfig() RCC时钟初始化:HAL_RCC_ClockConfig() ...

  6. python mqtt通信(windows)

      一.消息队列服务器 这里我用到activemq,可到官网下载 http://activemq.apache.org/ 1. 若遇到点击apache-activemq-5.16.2\bin\acti ...

  7. 卧槽,原来不需要FQ就可以构建海外镜像

    一. 背景 使用docker或者k8s的过程中,我们可能遇到镜像无法下载的情况,例如:kubernetes的kube-apiserver镜像,这是因为其仓库在海外,我们的网络被墙,我发获取到该资源,使 ...

  8. python 06篇 常用模块

    一.模块 什么是模块? 模块实质上就是一个python文件,它是用来组织代码的,意思就是说把python代码写到里面,文件名就是模块的名称,test.py test就是模块名称. 1.1 导入模块 i ...

  9. Function.identity()

    Java 8允许在接口中加入具体方法.接口中的具体方法有两种,default方法和static方法,identity()就是Function接口的一个静态方法.Function.identity()返 ...

  10. linux相关的常用站点

    1  http://cdimage.ubuntu.com/ ubuntu各个发行版的总集服务器 2 http://www.rpmfind.net/ 各种RPM包