apache-maven-3.5.2
apache-tomcat-8.5.16
JDK 1.8

服务端官方下载:https://github.com/apereo/cas-overlay-template
客户端官方下载:https://github.com/cas-projects/cas-sample-java-webapp
客户端配置官方文档:https://github.com/apereo/java-cas-client
application.properties配置官方文档CAS Configuration-Properties :
https://apereo.github.io/cas/5.0.x/installation/Configuration-Properties.html
CAS REST-protocol认证官方文档:https://apereo.github.io/cas/5.0.x/protocol/REST-Protocol.html
CAS 类说明 http://static.javadoc.io/org.apereo.cas/cas-server/5.1.0-RC1/overview-summary.html
CAS 5.1.X 官网 :https://apereo.github.io/cas/5.1.x/

1.服务器搭建

https://github.com/apereo/cas-overlay-template
下载cas-overlay-template,在解压后的文件路径下执行maven命令build package,会下载依赖构建war包,在target文件夹下生成cas.war
复制cas.war包到tomcat的webapp目录下,运行start.up,访问http://localhost:8080/cas/login
可以看到,登录用户名和密码可以输入casuser和Mellon,这是初始配置文件默认的,文件位于apache-tomcat-8.5.16\webapps\cas\WEB-INF\classes下的application.properties
这里写图片描述
登录成功可以看到:
这里写图片描述

2.数据库连接

配置application.properties文件属性;
注释掉cas.authn.accept.users=casuser::Mellon

##
# CAS Authentication Credentials
#
#注释掉默认的ID,password
#cas.authn.accept.users=casuser::Mellon

添加jdbc认证的配置:

#表名和查询条件字段名
cas.authn.jdbc.query[0].sql=select * from user where username=?
cas.authn.jdbc.query[0].healthQuery=
cas.authn.jdbc.query[0].isolateInternalQueries=false
#连接端口和数据库
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/CAS_user?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.authn.jdbc.query[0].failFast=true
cas.authn.jdbc.query[0].isolationLevelName=ISOLATION_READ_COMMITTED
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.jdbc.query[0].leakThreshold=10
cas.authn.jdbc.query[0].propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.jdbc.query[0].batchSize=1
cas.authn.jdbc.query[0].user=root
#cas.authn.jdbc.query[0].ddlAuto=create-drop
cas.authn.jdbc.query[0].maxAgeDays=180
#数据库连接密码
cas.authn.jdbc.query[0].password=1234567
cas.authn.jdbc.query[0].autocommit=false
#数据库驱动
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
cas.authn.jdbc.query[0].idleTimeout=5000
# cas.authn.jdbc.query[0].credentialCriteria=
# cas.authn.jdbc.query[0].name=
# cas.authn.jdbc.query[0].order=0
# cas.authn.jdbc.query[0].dataSourceName=
# cas.authn.jdbc.query[0].dataSourceProxy=false
cas.authn.jdbc.query[0].fieldPassword=password

# cas.authn.jdbc.query[0].fieldExpired=
# cas.authn.jdbc.query[0].fieldDisabled=
# cas.authn.jdbc.query[0].principalAttributeList=sn,cn:commonName,givenName

#cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
#cas.authn.jdbc.query[0].passwordEncoder.type=com.example.CustomPasswordEncoder
#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

# cas.authn.jdbc.query[0].principalTransformation.suffix=
# cas.authn.jdbc.query[0].principalTransformation.caseConversion=NONE|UPPERCASE|LOWERCASE
# cas.authn.jdbc.query[0].principalTransformation.prefix=

3、MD5前端加密

apache-tomcat-8.5.16-2\webapps\cas\WEB-INF\classes\templates下的casLoginView.html是登录界面的html,引入fragments文件夹下的loginform.html即是登录表单页面。
可以在下面语句表单提交之前将输入的密码加密

    <script type="text/javascript" th:inline="javascript">
        var i = [[#{screen.welcome.button.loginwip}]]
        $("#fm1").submit(function() {
            $(":submit").attr("disabled", true);
            $(":submit").attr("value", i);
            return true;
        });
    </script>

更改

    <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript" th:inline="javascript">
          var i = [[#{screen.welcome.button.loginwip}]]
          $("#fm1").submit(function() {
                      var pwdbefore = $("#password").val();
          var pwdafter = md5(pwdbefore);
          $("#password").val(pwdafter);
          // alert(pwdafter);
          $(":submit").attr("disabled", true);
          $(":submit").attr("value", i);
          return true;
        });
    </script>

4、配置JDBC,MD5认证加密

配置application.properties文件属性:取消下面配置的注释

cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
#cas.authn.jdbc.query[0].passwordEncoder.type=com.example.CustomPasswordEncoder
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5

5、客户端搭建

下载实例:https://github.com/cas-projects/cas-sample-java-webapp
以maven导入eclipse工程,会报两个错:
①右键项目properties–project faces 把java 1.4 改成 1.7
②缺少servlet-api.jar java包,tomcat的lib文件夹下有,可以add build path
然后进入web.xml
a.更改成你的服务器和客户端的地址:我改成了HTTP访问方式

    <!-- 认证拦截 -->
    <filter>
        <filter-name>CAS Authentication Filter</filter-name>
        <!--<filter-class>org.jasig.cas.client.authentication.Saml11AuthenticationFilter</filter-class>-->
        <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
        <init-param>
        <!-- 要跳转的服务器地址 -->
            <param-name>casServerLoginUrl</param-name>
           <!--
            <param-value>https://mmoayyed.unicon.net:8443/cas/login</param-value>
            -->
             <param-value>http://localhost:8080/cas/login</param-value>
        </init-param>
        <init-param>
        <!-- 客户端项目所在地址,会议service参数get提交到服务器的地址 -->
            <param-name>serverName</param-name>
            <!--
            <param-value>https://mmoayyed.unicon.net:9443</param-value>
            -->
            <param-value>http://localhost:8118</param-value>
        </init-param>
    </filter>

b.和ticket认证的地址

<!-- ticket认证 -->
    <filter>
        <filter-name>CAS Validation Filter</filter-name>
        <!--<filter-class>org.jasig.cas.client.validation.Saml11TicketValidationFilter</filter-class>-->
        <filter-class>org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter</filter-class>
        <init-param>
        <!-- 认证服务中心的地址 -->
            <param-name>casServerUrlPrefix</param-name>
            <param-value>http://localhost:8080/cas/</param-value>
        </init-param>
                <!-- 客户端项目所在地址,会议service参数get提交到服务器的地址 -->
        <init-param>
            <param-name>serverName</param-name>
            <param-value>http://localhost:8118</param-value>
        </init-param>

c.因为CAS默认的HTTPS访问方式,要添加HTTP方式,要在服务端添加HTTP的服务
\apache-tomcat-8.5.16\webapps\cas\WEB-INF\classes\services下的HTTPSandIMAPS-10000001文件更改正则表达式 添加HTTP

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

d.然后在application.properties文件添加认证JSON服务的配置

#添加认证服务
cas.serviceRegistry.initFromJson=true

不然会报错:
这里写图片描述

然后跑起eclipse的客户端 认证成功 客户端返回成功的界面
这里写图片描述

6、实现两个客户端的单点登录

从eclipse导出客户端的war包部署在一个新的tomcat下,不把两个客户端放在eclipse下跑的原因是,有可能会共享一个session 不便于测试单点登录和全登出的效果。
这里服务器和客户端1,客户端2 的TOMCAT的shutdown,http,ajp的端口一定都设成不一样的,才能都在localhost下跑起来。
实现效果,客户端1进行了ID,密码验证后 ,进入客户端2不需要再输入账号密码。

7、全登出的配置

参考:http://blog.csdn.net/u010475041/article/details/78234027
http://blog.csdn.net/u010475041/article/details/78018100
首先全登出的原理是服务端选择登出后,先清除自己的session,再依次通知客户端清除客户端自己的session。
CAS是有全登出的配置,且默认打开。官方文档:
https://apereo.github.io/cas/5.1.x/installation/Logout-Single-Signout.html

service:
服务端通过service来允许每一个客户端接入认证,如HTTPSandIMAPS-10000001.json

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

serviceId就是允许去服务端认证的客户端的IP,”^(https|imaps)://.*”,因为没有HTTP,所以通过HTTP协议来访问服务端的就没有权限。所以需要对每一个客户端单独配置一个service。

实现Logout and Single Logout (SLO),需要:
a.首先配置客户端的web.xml的登出拦截器。

<!-- 登出拦截器 -->
    <filter>
        <filter-name>CAS Single Sign Out Filter</filter-name>
        <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
        <init-param>
            <param-name>casServerUrlPrefix</param-name>
            <param-value>http://localhost:8080/cas</param-value>
        </init-param>
    </filter>

b.然后在服务端添加服务,在apache-tomcat-8.5.16\webapps\cas\WEB-INF\classes\services下有几个json格式的service,要保证json的service被加载,需要在application.properties文件添加认证JSON服务的配置添加认证服务

cas.serviceRegistry.initFromJson=true

c.默认的两个json文件:Apereo-10000002.json和HTTPSandIMAPS-10000001.json,删除。我实验的是两个客户端,地址分别是:http://localhost:8118/cas-sample-java-webapphttp://localhost:8208/cas-sample-java-webapp。所以配置的两个service文件如下:

{
  "@class": "org.apereo.cas.services.RegexRegisteredService",
  "serviceId" : "http://localhost:8118/cas-sample-java-webapp.*",
  "name": "Clientdemo1",
  "id": 10000001,
   "description" : "Clientdemo1",
  "evaluationOrder" : 10000,
   "logoutType" : "BACK_CHANNEL",
  "logoutUrl" : "http://localhost:8118/cas-sample-java-webapp/logout.jsp"
}

{
  "@class": "org.apereo.cas.services.RegexRegisteredService",
  "serviceId" : "http://localhost:8208/cas-sample-java-webapp.*",
  "name": "Clientdemo2",
  "id": 10000002,
  "description" : "Clientdemo2",
  "evaluationOrder" : 10001,
  "logoutType" : "BACK_CHANNEL",
  "logoutUrl" : "http://localhost:8208/cas-sample-java-webapp/logout.jsp"
}

d.另外注意:json文件名字规则为 name n a m e − {id}.json,id必须为json文件内容id一致
json文件解释:

@class:必须为org.apereo.cas.services.RegisteredService的实现类,对其他属性进行一个json反射对象,常用的有RegexRegisteredService,匹配策略为id的正则表达式
serviceId:唯一的服务id
name: 服务名称,会显示在默认登录页
id:全局唯一标志
description:服务描述,会显示在默认登录页
evaluationOrder: 匹配争取时的执行循序

如果没问题,客户端1和客户端2的认证界面都能看到配置到的service:
这里写图片描述
这里写图片描述

实现效果:
并且在服务端登出后,客户端1和客户端2会自动登出。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐