在这里插入图片描述

文章demo在我的百度

链接https://pan.baidu.com/s/1W6DbxfrXfapHwMhkQzfR_w

提取码:wxih

CAS介绍

简介

CAS是Central Authentication Service的缩写,中央认证服务,一种独立开放指令协议。CAS 是 耶鲁大学(Yale University)发起的一个开源项目,旨在为 Web 应用系统提供一种可靠单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目

特点:

1、开源企业级单点登录解决方案

2、CAS Server 为需要独立部署的 Web 应用

3、CAS Client 支持非常多的客户端(这里单点登录系统中的各个 Web 应用),包括 Java, .Net, PHP, Perl, Apache, uPortal, Ruby 等。

4、CAS属于Apache 2.0许可证,允许代码修改,再发布(作为开源或商业软件)。

结构上看,CAS 包含两个部分: CAS Server 和 CAS Client。CAS Server 需要独立部署,主要负责用户认证工作;CAS Client 负责处理客户端保护资源访问请求需要登录时,重定向到 CAS Server。图1 是 CAS 最基本协议过程

在这里插入图片描述
CAS Client 与受保护客户端应用部署在一起,以 Filter 方式保护保护资源。对于访问保护资源每个 Web 请求,CAS Client分析请求的 Http 请求是否包含 Service Ticket,如果没有,则说明当前用户尚未登录,于是将请求重定向指定好的 CAS Server 登录地址,并传递 Service (也就是要访问的目的资源地址),以便登录成功过后转回该地址用户在第 3 步中输入认证信息,如果登录成功,CAS Server 随机产生一个相当长度唯一、不可伪造的 Service Ticket,并缓存以待将来验证,之后系统自动重定向到 Service 所在地址,并为客户端浏览器设置一个 Ticket Granted Cookie(TGC),CAS Client 在拿到 Service 和新产生的 Ticket 过后,在第 5,6 步中与 CAS Server 进行身份核实,以确保 Service Ticket 的合法性。

在该协议中,所有与 CAS 的交互采用 SSL 协议,确保,ST 和 TGC 的安全性。协议工作过程中会有 2 次重定向的过程,但是 CAS Client 与 CAS Server 之间进行 Ticket 验证的过程对于用户透明的。

另外,CAS 协议中还提供了 Proxy (代理模式,以适应更加高级复杂应用场景,具体介绍可以参考 CAS 官方网站上的相关文档。 [1]

搭建Tomcat HPPTS的支持

一步生成秘钥库

我们采用JDK自带keytool工具生成秘钥库

别名 java1234 存储路径 F:caskeystore
keytool -genkey -v -alias java1234 -keyalg RSA -keystore F:caskeystorejava1234.keystore
在这里插入图片描述
这里需要填写一些信息,注意填写域名

秘钥库密码我们用 666666

执行完,会生成一个秘钥库文件

第二步:从秘钥库里导出证书

keytool -export -trustcacerts -alias java1234 -file F:/cas/keystore/java1234.cer -keystore F:/cas/keystore/java1234.keystore
在这里插入图片描述
在这里插入图片描述

第三步:将证书导入到JDK证书库

keytool -import -trustcacerts -alias java1234 -file F:/cas/keystore/java1234.cer -keystore "D:/Program Files/Java/jdk/jre/lib/security/cacerts"
在这里插入图片描述
密码:changeit

第四步:tomcat配置https支持

在这里插入图片描述

	<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"

               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"

               clientAuth="false" sslProtocol="TLS" 

               keystoreFile="F:caskeystorejava1234.keystore"

               keystorePass="666666" /&gt;

启动bin下的startup.bat

默认中文乱码,可以我们来到tomcat目录conf子目录中,找到一个名为 “logging.properties” 的文件打开这个本文件,找到如下配置项:

java.util.logging.ConsoleHandler.encoding = UTF-8

将 UTF-8 修改为 GBK,修改后的效果为:

java.util.logging.ConsoleHandler.encoding = GBK

保存后,重启tomcat!)

第五步:测试

访问 https://localhost:8443
忽略安全
在这里插入图片描述

CAS Server 服务端的搭建

CAS Server war下载

在这里插入图片描述
cas架构cs架构图
在这里插入图片描述
github官网https://github.com/apereo/cas

cas服务端

https://github.com/apereo/cas-overlaytemplate

https://repo1.maven.org/maven2/cas/

https://repo1.maven.org/maven2/org/apereo/cas/cas-serverwebapptomcat/5.3.14/

下载:cas-serverwebapptomcat-5.3.14

CAS Server发布tomcat

war包放tomcat下,启动tomcat自动解压,我们把名称改成cas,方便访问;

注意点:日志修改

在这里插入图片描述

访问:https://localhost:8443/cas

cas 8443端口

配置hosts: C:WindowsSystem32driversetc

127.0.0.1 java1234.com

通过域名访问:

https://java1234.com:8443/cas
在这里插入图片描述
用户名:casuser

密码:Mellon

配置数据数据库认证

一步新建数据和表:

CREATE DATABASE /*!32312 IF NOT EXISTS*/`db_sso` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `db_sso`;
/*Table structure for table `t_cas` */
DROP TABLE IF EXISTS `t_cas`;
CREATE TABLE `t_cas` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `username` varchar(30) DEFAULT NULL,

  `password` varchar(100) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*Data for the table `t_cas` */
insert  into `t_cas`(`id`,`username`,`password`) values (1,'java1234','123456');

第二步:修改application.properties配置文件

注释掉写死的认证用户

加上jdbc数据源配置

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

# cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQL5Dialect

cas.authn.jdbc.query[0].url=jdbc:mysql://localhost:3306/db_sso?serverTimezone=GMT

cas.authn.jdbc.query[0].user=root

cas.authn.jdbc.query[0].password=123456

cas.authn.jdbc.query[0].sql=select * from t_cas where username=?

cas.authn.jdbc.query[0].fieldPassword=password

cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver

在这里插入图片描述
第三步:加上jdbc驱动包以及支持jar(可以找我要)

graphic

在这里插入图片描述
添加lib

然后使用自定义密码登录
在这里插入图片描述
第四步:测试OK

MD5加密

第一步:数据库生成md5密码

SELECT MD5(‘123456’);

第二步:修改application.properties配置文件

cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT

cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8

#MD5加密策略

cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5

在这里插入图片描述
在这里插入图片描述

第三步:测试OK
123456仍然可以登录成功
在这里插入图片描述

client+springboot客户端整合搭建

项目搭建,加入cas client依赖



<!-- https://mvnrepository.com/artifact/org.jasig.cas.client/cas-client-core --&gt;

<dependency&gt;

    <groupId&gt;org.jasig.cas.client</groupId&gt;

    <artifactId&gt;cas-client-core</artifactId>

    <version>3.6.1</version>

</dependency>

 

springboot项目整合cas

application.yml加配置:

cas:

  server-url-prefix: https://java1234.com:8443/cas

  server-login-url: https://java1234.com:8443/cas/login

  client-host-url: http://java1234.com:7777

  validation-type: cas3

启动类加注解

@EnableCasClient

在这里插入图片描述
报错原因是cas服务器默认不支持http请求,我们可以通过配置来让其支持http请求

D:casapachetomcat-9.0.38webappscasWEB-INFclassesservices

修改HTTPSandIMAPS-10000001.json文件

解决办法

1、修改配置文件WEB-INFclassesservicesHTTPSandIMAPS-10000001.json

"serviceId" : "^(https|imaps)://.*" 

改为

 "serviceId" : "^(https|http|imaps)://.*",

2、修改配置文件WEB-INFclassesapplication.properties

cas.tgc.secure=false

cas.serviceRegistry.initFromJson=true

显示用户名

我们cas客户端经过cas server认证后,cas客户端可以去cas server端拿到登录认证信息比如用户名

thymeleaf页面可以${session.const_cas_assertion.principal.name} 这么取值

具体我们通过源码找到Servlet3AuthenticationFilter

org.jasig.cas.client.jaas Servlet3AuthenticationFilter
在这里插入图片描述
从cas server端获取认证信息后,设置const_cas_assertion 里去,通过查看 principal.getAssertion() 可以发现有principal的name属性就是用户名

注销

cas客户端注销方式

我们通过重定向到cas服务端logout接口,来实现统一注销。

CAS Server界面修改

cas server用到springmvc+webflow实现的;

Spring Web Flow构建于Spring MVC之上,允许实现Web应用程序的“流程”。流程封装了一系列步骤,指导用户执行某些业务任务。它跨越多个HTTP请求,具有状态处理事务数据,可重用,并且可能动态的,并且本质上是长期运行的。

我们把登录界面tomcat部署的项目中修改
在这里插入图片描述

整体代码

在这里插入图片描述

package com.xii.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

    /**
     * 网站根目录请求
     * @return
     */
    @RequestMapping("/crm")
    public ModelAndView root(HttpSession session){
        ModelAndView mav=new ModelAndView();
        mav.setViewName("index");
        return mav;
    }

    /**
     * 注销
     * @return
     */
    @RequestMapping("/logout")
    public String logout(){
        return "redirect:https://localhost:8443/cas/logout";
    }
}


package com.xii;

import net.unicon.cas.client.configuration.EnableCasClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

/**
}

 * 开启CAS @EnableCasClient
 */

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableCasClient
public class CrmSysApplication {

    public static void main(String[] args) {
        SpringApplication.run(CrmSysApplication.class, args);
    }
}

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>crm客户关系</title>
</head>
<body>
<h2>crm客户关系</h2>
欢迎:&amp;nbsp;&amp;nbsp;<font th:text="${session._const_cas_assertion_.principal.name}"></font>&amp;nbsp;&amp;nbsp;进入crm客户关系<br/><br/>
<a href="http://java1234.com:7777/" target="_blank">crm客户关系</a>
<a href="http://java1234.com:8888/" target="_blank">fd财务系统</a>
&amp;nbsp;&amp;nbsp;<a href="/logout">安全退出</a>

</body>
</html>

application.yml

server:
  port: 7777

#开启cas 配置对应路由
cas:
  server-url-prefix: https://java1234.com:8443/cas
  server-login-url: https://java1234.com:8443/cas/login
  client-host-url: http://java1234.com:7777/
  validation-type: cas3

另一个子项目与之相似

测试:当一个子项目登陆成功之后,进入另一个子项目就不需要登录认证。

补充密码配置说明

##
# JDBC配置
#
#查询账号密码SQL,必须包含密码字段
cas.authn.jdbc.query[0].sql=select * from user where username=?

#指定上面的SQL查询字段名(必须)
cas.authn.jdbc.query[0].fieldPassword=password

#指定过期字段,1为过期,若过期不可用
cas.authn.jdbc.query[0].fieldExpired=expired

#为不可用字段段,1为不可用,需要修改密码
cas.authn.jdbc.query[0].fieldDisabled=disabled

#数据库连接
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/cas?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;useSSL=false

#数据库dialect配置
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect

#数据库用户名
cas.authn.jdbc.query[0].user=root

#数据库用户密码
cas.authn.jdbc.query[0].password=123

#数据库事务自动提交
cas.authn.jdbc.query[0].autocommit=false

#数据库驱动
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver

#超时配置
cas.authn.jdbc.query[0].idleTimeout=5000

#默认加密策略通过encodingAlgorithm来指定算法默认NONE不加密
# NONE|DEFAULT|STANDARD|BCRYPT|SCRYPT|PBKDF2
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
# 加密
#cas.authn.jdbc.query[0].passwordEncoder.secret=
# 加密字符长度
#cas.authn.jdbc.query[0].passwordEncoder.strength=16

原文地址:https://blog.csdn.net/wang121213145/article/details/124822899

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_30322.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注