ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CAS 入门实战(2)--服务端安装

2022-05-08 11:00:33  阅读:220  来源: 互联网

标签:cas jdbc CAS server -- authn query 服务端


本文主要介绍 CAS 服务端的安装,使用到的软件版本:JDK 1.8.0_191、Tomcat 8.5.76、CAS 5.3.16。

1、单机安装

1.1、生成部署包

通过官方提供的 cas-overlay-template(https://github.com/apereo/cas-overlay-template/tree/5.3) 模板来生成部署包。

先 clone 项目到本地:

git clone https://github.com/apereo/cas-overlay-template.git -b 5.3

执行打包命令:

build.cmd package

命令执行完成之后会在 target 目录下生成 cas 的 web 应用及 cas.war 的包。linux 环境可以使用 build.sh 脚本。

1.2、生成密钥库

CAS 默认需要使用 Https 来访问,可使用 Java 的 keytool 工具来生成密钥库,然后使用该密钥库在 Tomcat 中配置 SSL。

D:\tmp>keytool -genkeypair -alias cas-tomcat -keyalg RSA -keystore casServer.keystore

1.3、Tomcat 中配置 SSL

在 conf/server.xml 中注释掉原来的 Connector,新增一个 Connector:

<!--Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" /-->

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
       maxThreads="150" SSLEnabled="true">
  <SSLHostConfig>
      <Certificate certificateKeystoreFile="d:/tmp/casServer.keystore"
                   certificateKeystoreType="JKS" certificateKeystorePassword="123456" />
  </SSLHostConfig>
</Connector>

1.4、修改日志目录

修改 WEB-INF\classes\log4j2.xml 文件中的日志文件保存目录:

<Properties>
    <Property name="baseDir">d:/tmp/logs</Property>
</Properties>

1.5、部署应用

把 1.1 生成的 cas 应用目录或 cas.war 拷贝到 Tomcat 的 webapps 目录下并启动 Tomcat;启动完成后访问应用:https://127.0.0.1:8443/cas,默认用户名密码为:casuser/Mellon

1.6、查看 Dashboard

目前情况下是没有权限查看 Dashboard 的,需要在 WEB-INF\classes\application.properties 中放开权限:

#修改如下配置,开启监控端点
cas.monitor.endpoints.enabled=true
cas.monitor.endpoints.sensitive=false

#新增如下配置,设置能访问的ip,.+ 表示任意ip
cas.adminPagesSecurity.ip=127.0.0.1

修改完后重启 Tomcat,重新登录:

1.7、JDBC 认证登录

默认情况下用户信息配置在 WEB-INF\classes\application.properties 中:

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

CAS 支持通过 JDBC 方式认证登录,以满足实际生成的需要。

1.7.1、引入 JDBC 的组件

在 clone 项目的 pom.xml 文件中增加如下配置(红字部分):

<dependencies>
    <dependency>
        <groupId>org.apereo.cas</groupId>
        <artifactId>cas-server-webapp${app.server}</artifactId>
        <version>${cas.version}</version>
        <type>war</type>
        <scope>runtime</scope>
    </dependency>
    <!--
    ...Additional dependencies may be placed here...
    -->
    <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.28</version>
    </dependency>
</dependencies>

重新打包 build.cmd package,再把应用重新部署到 Tomcat 中。

1.7.2、创建用户表

个应用系统一般都有用户表,这一步是不需要的;这里为了演示创建一个测试的用户表:

CREATE TABLE `cas_user`  (
  `id` bigint PRIMARY KEY,
  `user_name` varchar(32) NOT NULL COMMENT '用户名',
  `password` varchar(64) NOT NULL COMMENT '密码',
  `create_time` datetime COMMENT '创建时间',
  `expired_flag` int(1) NOT NULL DEFAULT 0 COMMENT '是否过期',
  `disabled_flag` int(1) NOT NULL DEFAULT 0 COMMENT '是否有效'
)

并插入测试数据,密码使用 MD5 加密,这里密码为 123456,MD5 加密后用十六进制表示为:e10adc3949ba59abbe56e057f20f883e

insert into cas_user(id,user_name,password,create_time)
values (1,'test','e10adc3949ba59abbe56e057f20f883e',now());

1.7.3、应用中配置数据库信息

在 WEB-INF\classes\application.properties 中修改配置:

#该行注释掉
#cas.authn.accept.users=casuser::Mellon

#增加下列配置
#查询用户信息的SQL,会把用户名作为参数传进来
cas.authn.jdbc.query[0].sql=select * from cas_user where user_name=?
#指定密码字段
cas.authn.jdbc.query[0].fieldPassword=password
#指定过期字段
cas.authn.jdbc.query[0].fieldExpired=expired_flag
#指定是否可用字段
cas.authn.jdbc.query[0].fieldDisabled=disabled_flag

cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.jdbc.query[0].driverClass=com.mysql.cj.jdbc.Driver
cas.authn.jdbc.query[0].url=jdbc:mysql://10.49.196.10:3306/cas?useUnicode=true&characterEncoding=UTF-8&useSSL=false
cas.authn.jdbc.query[0].user=root
cas.authn.jdbc.query[0].password=123456

#默认加密策略,NONE 不加密
cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5

重启 Tomcat,使用 test/123456 登录 CAS 系统。

1.8、使用 Http 方式登录

由于 Https 需要配置证书,比较麻烦,也不方便 CAS 客户端和 CAS 服务端的通信,可以修改为使用 Http 访问系统。

1.8.1、修改 HTTPSandIMAPS-10000001.json 文件

修改 WEB-INF\classes\services\HTTPSandIMAPS-10000001.json,增加 http 协议:

{
  "@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
}

1.8.2、修改 application.properties 文件

修改 WEB-INF\classes\application.properties 文件,增加如下配置:

cas.tgc.secure=false
cas.warningCookie.secure=false
cas.serviceRegistry.initFromJson=true

1.8.3、Tomcat 改回 Http 协议

删除原来 Https 的配置,复原原始配置:

<Connector port="8080" protocol="HTTP/1.1"
     connectionTimeout="20000"
     redirectPort="8443" />

重启 Tomcat,通过 Http 协议访问 CAS:

2、集群安装

2.1、集群架构

官方推荐的集群架构如下:

2.2、集群规划

ip 用途
10.49.196.10 CAS 服务端
10.49.196.11 CAS 服务端
10.49.196.12 nginx,redis

这里 redis 使用单机版,实际应用中可以使用 redis 的哨兵模式,防止单点故障。

2.3、票据持久化

默认票据保存在内存中,集群中个节点无法共享;CAS 提供多种票据持久化的方法,如:JMS、JPA、MongoDB、Redis、Cassandra 等等,这里使用 Redis 来持久票据。

2.3.1、引入依赖

在 cas-overlay-template 的 pom.xml 文件中引入相关依赖:

<profile>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <id>default</id>
    <dependencies>
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-webapp${app.server}</artifactId>
            <version>${cas.version}</version>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
        ......
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-support-redis-ticket-registry</artifactId>
            <version>${cas.version}</version>
        </dependency>
    </dependencies>
</profile>

重新打包 build.cmd package,再把应用重新部署到 Tomcat 中。

2.3.2、配置 Redis 信息

在 WEB-INF\classes\application.properties 中增加配置:

cas.ticket.registry.redis.host=10.49.196.12
cas.ticket.registry.redis.database=0
cas.ticket.registry.redis.port=6379
cas.ticket.registry.redis.usePool=true

详细说明可参考官方文档:https://apereo.github.io/cas/6.5.x/ticketing/Redis-Ticket-Registry.html (5.3 版本的文档已经没有了,这里使用 6.5 版本的文档);配置完后,重启应用即可。

2.4、Session 持久化(可选)

Session 持久化用于 CAS 实例之间共享会话状态和会话故障转移;这一步是可选的,不建议使用,因为用户 CAS 会话往往是短期的,并且体验更像是请求-响应风格,而不是面向会话的。

2.4.1、引入依赖

 在 cas-overlay-template 的 pom.xml 文件中引入相关依赖:

<profile>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <id>default</id>
    <dependencies>
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-webapp${app.server}</artifactId>
            <version>${cas.version}</version>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
        ......
        <dependency>
             <groupId>org.apereo.cas</groupId>
             <artifactId>cas-server-webapp-session-redis</artifactId>
             <version>${cas.version}</version>
        </dependency>
    </dependencies>
</profile>

重新打包 build.cmd package,再把应用重新部署到 Tomcat 中。

2.4.2、配置 Session 持久信息

在 WEB-INF\classes\application.properties 中增加配置:

cas.webflow.session.storage=true
spring.session.store-type=redis
spring.redis.host=10.49.196.12
spring.redis.port=6379

详细说明可参考官方文档:https://apereo.github.io/cas/6.5.x/webflow/Webflow-Customization-Sessions.html (5.3 版本的文档已经没有了,这里使用 6.5 版本的文档);配置完后,重启应用即可。

2.5、代理配置

这里使用 Nginx 作为代理服务器,配置如下:

http {
    ......

    upstream cas {
        server 10.49.196.10:8080 weight=1;
        server 10.49.196.11:8081 weight=1;
        ip_hash;
    }
    server {
        listen       8080;
        server_name  localhost;
        ......

        location /cas {
            proxy_pass http://cas/cas;
        }
    }
}

启动 Nginx 后,就可以通过 http://10.49.196.12:8080/cas 来访问 CAS 了。

标签:cas,jdbc,CAS,server,--,authn,query,服务端
来源: https://www.cnblogs.com/wuyongyin/p/16034502.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有