SSO单点登录详解-------八、搭建CAS Client服务端

一、前言

目前为止我们已经搭建好了CAS Server端,我们需要来搭建客户端配合使用,完成单点登录和单点注销的功能.我们将讲两种方式来搭建CAS Client端.一种是普通项目搭建,另一种是基于SpringBoot的项目搭建.

二、普通项目搭建CAS Client

官方提供了一个简易的模板来搭建CAS Client.
Github地址:https://github.com/cas-projects/cas-sample-java-webapp
下载导入项目之后,我们只需要稍作修改,运行起来就可以使用了.
步骤:
1.下载之后导入到工具中,导入之后项目结构如下图:
在这里插入图片描述

项目结构

2.项目默认使用的jetty的启动插件.如果使用tomcat的插件,需要在pom.xml中把jetty配置去掉,添加tomcat的插件.

<plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
               <port>8088</port>
                <path>/</path>
          </configuration>
</plugin>

3.修改web.xml,把里面所有的``casServerUrlPrefix对应的value值换成你统一认证中心的域名,把所有serverName对应的value值换成你当前项目的域名.下方配置也标记了什么地方需要修改.
一定得改对,否则在访问的时候会出现票根’ST-1-SSAyf0nFMvmVBvEBSXyg-cas’不符合目标服务的错误.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!--
   <context-param>
       <param-name>renew</param-name>
       <param-value>true</param-value>
   </context-param>
-->

    <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>https://www.sso.com:8443/cas</param-value>
        </init-param>
    </filter>

    <listener>
        <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
    </listener>

    <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://www.sso.com:8443/cas/login</param-value>
        </init-param>
        <init-param>
            <param-name>serverName</param-name>
            <!--此处需要修改-->
            <param-value>http://www.crm.com:8088</param-value>
        </init-param>
    </filter>

    <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>https://www.sso.com:8443/cas</param-value>
        </init-param>
        <init-param>
            <param-name>serverName</param-name>
            <!--此处需要修改-->
            <param-value>http://www.crm.com:8088</param-value>
        </init-param>
        <init-param>
            <param-name>redirectAfterValidation</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>useSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <!--
        <init-param>
            <param-name>acceptAnyProxy</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>proxyReceptorUrl</param-name>
            <param-value>/sample/proxyUrl</param-value>
        </init-param>
        <init-param>
            <param-name>proxyCallbackUrl</param-name>
            <param-value>https://mmoayyed.unicon.net:9443/sample/proxyUrl</param-value>
        </init-param>
        -->
        <init-param>
            <param-name>authn_method</param-name>
            <param-value>mfa-duo</param-value>
        </init-param>
    </filter>

    <filter>
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
        <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>CAS Single Sign Out Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>CAS Validation Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>CAS Authentication Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>
            index.jsp
        </welcome-file>
    </welcome-file-list>
</web-app>

三、测试

运行tomcat7:run命令,把客户端启动起来.浏览器输入http://www.crm.com:8088/index.jsp,此时页面会跳转到统一认证中心的登陆页面.
但是会看到如下的错误信息:未认证授权的服务,
这是因为CAS Server端默认只接收https的客户端请求,如果想让CAS Server支持http协议的客户端请求,需要在服务端添加一些配置.
在这里插入图片描述

未认证授权的服务

服务端修改如下:
操作步骤(1):

1.1 拷贝overlays/org.apereo.cas.cas-server-webapp-tomcat-5.2.3/WEB-INF/classes/services/HTTPSandIMAPS-10000001.json文件到/src/main/resources/services目录.

1.2 修改services\HTTPSandIMAPS-10000001.json文件
“serviceId” : “^(https|imaps)😕/.”
改为==>
“serviceId” : “^(https|http|imaps)😕/.”,

操作步骤(2)

在application.properties文件中添加:
cas.tgc.secure=false
cas.serviceRegistry.initFromJson=true

配置好之后,重新启动客户端和服务端.登陆之后会自动跳转到客户端的首页.看到如下这张图说明CAS Server端和CAS Client端已经整合成功.
在这里插入图片描述

四、单点注销

CAS框架已经集成了单点注销的功能,只需要统一认证中心注销之后,所有的子系统都统一的注销了.
启动两个客户端,端口分别为8088,8089,在访问的时候只需要访问一次即可.
当统一认证中心注销的时候,所有的子系统都统统注销了.
这需要在客户端中的退出按钮,指向统一认证中心的注销方法即可

<a href="https://www.sso.com:8443/cas/logout?service=http://www.crm.com:8088/index.jsp">注销</a>

当我们访问完这个方法之后,所有的子系统会自动的注销.
1.默认请求访问统一认证中心的logout方法后,响应的是统一认证中心的注销页面.
2.如果我们希望注销之后,能回到子系统中的某个页面,此时可以在地址栏后面添加service的参数,统一认证中心会在注销之后,会重定向到http://www.crm.com:8088/index.jsp这个地址.这个需要在服务端配置一个参数:
cas.logout.followServiceRedirects=true

五、基于SpringBoot的客户端集成CAS

我们要在SpringBoot的环境下使用CAS的功能,首先需要想到的是有没有对应的starter可以帮我们快速构建.我们使用使用了cas-client-autoconfig-support项目的集成能力.Github地址:
https://github.com/Unicon/cas-client-autoconfig-support
步骤:
1.在SpringBoot应用的Maven配置文件(pom.xml)中添加依赖:

<dependency>
  <groupId>net.unicon.cas</groupId>
  <artifactId>cas-client-autoconfig-support</artifactId>
  <version>1.5.0-GA</version>
</dependency>

2.在应用启动入口类上添加配置@EnableCasClient,示例:

@SpringBootApplication
@EnableCasClient
public class CrmApplication{ .. }

3.在应用的配置文件中(application.properties)添加如下配置:

# 项目端口
server.port=8088
# 填CAS服务器的前缀
cas.server-url-prefix=https://www.sso.com:8443/cas
# 填CAS服务器的登录地址
cas.server-login-url=https://www.sso.com:8443/cas/login
# 填客户端的访问前缀 www.crm.com是在host文件中配置的映射,映射到127.0.0.1
cas.client-host-url=http://www.crm.com:8088

启动项目即可,这样SpringBoot就和CAS集成完成了.



原文链接:https://www.jianshu.com/p/61c20a94c873

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值