CAS (6) —— Nginx代理模式下浏览器访问CAS服务器网络顺序图详解

CAS (6) —— Nginx代理模式下浏览器访问CAS服务器网络顺序图详解


tomcat版本: tomcat-8.0.29

jdk版本: jdk1.8.0_65

nginx版本: nginx-1.9.8

cas版本: cas4.1.2
cas-client-3.4.1

参考来源:

jasig.github.io:CAS protocol

https://github.com/Jasig/java-cas-client

通过Proxy访问其它Cas应用

CAS负载均衡配置——SSL篇

CAS负载均衡配置

CAS客户端集群

  • 以下的示例采用我博客的另外两篇文章中搭建好的测试环境举例

CAS (1) —— Mac下配置CAS到Tomcat(服务端)

CAS (2) —— Mac下配置CAS到Tomcat(客户端)

CAS (3) —— Mac下配置CAS客户端经代理访问Tomcat CAS

Mac为nginx安装nginx-sticky-module

【高可用HA】Nginx (1) —— Mac下配置Nginx Http负载均衡(Load Balancer)之101实例

Nginx (2) —— Mac下配置Apache Httpd的Https/SSL (待出)

目标架构

613455-20151217095721396-1141430139.png

此代理非彼代理

在CAS官方网站上给出了一个“Proxy Web Flow Diagram”:

613455-20151217094901068-1204662614.png

613455-20151217094915162-1105993705.png

顺序图:(来源于http://jasig.github.io/cas/4.0.x/protocol/CAS-Protocol.html)

这个方案主要适用一种场景:

有两个应用App1和App2,它们都是受Cas Server保护的,即请求它们时都需要通过Cas Server的认证。现需要在App1中通过Http请求访问App2,显然该请求将会被App2配置的Cas的AuthenticationFilter拦截并转向Cas Server,Cas Server将引导用户进行登录认证,这样我们也就不能真正的访问到App2了。针对这种应用场景,Cas也提供了对应的支持。通过Proxy访问其它Cas应用

无论是用中文关键字在“度娘”,还是用英文关键字再“谷哥”上搜索,多数文章都是描述上面这样一个场景。

而我这里介绍的“代理”,并非是上述场景——依靠代理去验证ticket,“代理”在此的角色是:

  • 只做分发反向代理(未来的负载均衡器)
* 注意:所以说“此代理非彼代理”

准备

要搭建上面这个环境会相对复杂,我们需要参照之前的文章准备以下必备的组件或环境:

CAS (5) —— Nginx代理模式下浏览器访问CAS服务器配置详解

测试

  1. 访问“https://app1.hoau.com:8413/cas1

    613455-20151217112353990-397652019.png

    会重定向到“https://proxy.sso.hoau.com/cas/login?service=https%3A%2F%2Fapp1.hoau.com%3A8413%2Fcas1

  2. 然后输入用户明密码(test01/psw01)

    如果验证成功,则会将浏览器重定向到app1的登陆成功页面。

    613455-20151217112407771-1654378095.png

  3. 再次访问“https://app1.hoau.com:8413/cas1

    可以直接进入登陆成功页,而无需输入用户名密码。

  4. 访问另一应用

    同样可以通过test01用户直接进入登陆成功页,而无需输入用户名密码。

    613455-20151217112423193-1368642199.png

上面基本测试通过,我们接着分析网络顺序。

代理下的网络顺序分析

由于这个场景较为简单,所以jasig官网上并没有给出这个场景下的网络顺序图。于是我自己画了一个。

613455-20151217151617881-1176076397.png

仔细观察可以发现:上图与不通过nginx代理时,官网的序列图类似,只有代理出多了转发和反向的工作。

首次访问

https://app1.hoau.com:8413/cas1
(0)用户通过浏览器访问上面地址

613455-20151217155515443-1838044077.png

(1)浏览器访问Protected App #1
(2)Protected App #1尝试验证身份

首次登陆,身份验证未通过,返回重定向消息

302 Location 
https://proxy.sso.hoau.com:443/cas/login
    ?service=https://app1.hoau.com:8413/cas1
(3)浏览器重定向以上地址至代理服务器Proxy
(4)Proxy将次请求分发至CAS服务器

613455-20151217155714490-288424364.png

https://sso.hoau.com:8433/cas/login
    ?service=https://app1.hoau.com:8413/cas1
    

CAS服务器发现用户没有SSO的Session,然后返回CAS的登陆页面内容

(5)返回CAS的登陆页面内容至代理服务器
(6)代理将响应的内容返回给浏览器
(7)浏览器为用户呈现登陆页面

613455-20151217155757724-1380009767.png

(8)用户提交用户名和密码

613455-20151217155859615-411815250.png

(9)浏览器提交Form内容至代理
(10)代理转发提交的内容至CAS服务器
(11)CAS服务器验证用户身份
(12)CAS服务器返回相应至代理

613455-20151217160040693-254130040.png

(13)代理将响应返回给浏览器

并且设置Cookie

CASTGC

由于身份验证成功,此刻浏览器被要求重定向至Protected App #1的登陆页

(14)浏览器被重定向至Protected App #1

并且携带ticket信息。

613455-20151217160120912-743606401.png

*注意:以下(15)、(16)、(17)和(18)步为Protected App #1与CAS Server背后的交互。

Protected App #1 收到请求后,会再次访问CAS Server以验证ticket。

(15)(16)Protected App #1通过Proxy请求serviceValidate
(17)(18)CAS服务器经由代理返回响应信息
(19)如果CAS服务器校验通过

则会设置Cookie JSESSIONID,然后将重定向信息返回到浏览器

(20)浏览器再次访问Protected App #1

613455-20151217160259662-409971803.png

并携带有效的JSESSIONID,这时

如果验证通过,则

(22)返回目标页面的内容

613455-20151217160226302-1137370308.png

再次访问

https://app1.hoau.com:8413/cas1

另一个应用访问

https://app2.hoau.com:8423/cas2

以上“再次访问”和“另一应用访问”不在赘述,大家可以自行测试,或参考无代理情况下的序列图:

CAS (4) —— CAS浏览器SSO访问顺序图详解(CAS Web Flow Diagram by Example)

附一个Charles的http顺序图

613455-20151217150616271-1726676114.png

结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1,tomcat8的配置: 1.1修改tomcat8.x/conf/context.xml的配置如下: <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <Context> <!-- Default set of monitored resources. If one of these changes, the --> <!-- web application will be reloaded. --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> <Resources cachingAllowed="true" cacheMaxSize="100000" /> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:127.0.0.1:11211" username="root" password="" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> --> <Resources cachingAllowed="true" cacheMaxSize="100000" /> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:127.0.0.1:11211" username="root" password="" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" sessionBackupTimeout="18000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" /> </Context> 1.2添加memcached如下依赖的jar包到tomcat8.x/lib/: asm-5.1.jar couchbase-client-1.4.12.jar javolution-5.5.1.jar kryo-4.0.0.jar kryo-serializers-0.38.jar memcached-session-manager-2.0.0.jar memcached-session-manager-tc8-2.0.0.jar minlog-1.3.jar msm-javolution-serializer-2.0.0.jar msm-kryo-serializer-2.0.0.jar msm-xstream-serializer-2.0.0.jar objenesis-2.1.jar reflectasm-1.09.jar spymemcached-2.12.1.jar 2,nginx的配置: #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; #设定负载均衡的服务器列表 upstream 127.0.0.1 { #设定负载均衡的服务器列表 #ip_hash; #同一机器在多网情况下,路由切换,ip可能不同 #weigth参数表示权值,权值越高被分配到的几率越大 server 127.0.0.1:8085 weight=1 max_fails=20 fail_timeout=600s; server 127.0.0.1:8086 weight=1 max_fails=20 fail_timeout=600s; } map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { listen 80; server_name localhost; charset UTF-8; #设定本虚拟主机的访问日志 access_log logs/host.access.log main; #对 "/" 所有应用启用负载均衡 location / { proxy_pass http://127.0.0.1; #保留用户真实信息 proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; index index.html index.htm index.aspx; } #对 "/Dossm3RabbitMQConsumer/" 启用负载均衡 location /Dossm3RabbitMQConsumer/ { proxy_pass http://localhost:8086; #保留用户真实信息 proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; index index.html index.htm index.aspx; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 3,cas的配置(): 3.1 修改/CAS/WEB-INF/spring-configuration/ticketRegistry.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to Jasig under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. Jasig licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at the following location: http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <description> Configuration for the default TicketRegistry which stores the tickets in-memory and cleans them out as specified intervals. </description> <!-- memcached 配置开始 --> <!-- Ticket Registry --> <bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.MemCacheTicketRegistry"> <constructor-arg index="0"> <bean class="net.spy.memcached.spring.MemcachedClientFactoryBean" p:servers="127.0.0.1:11211" p:protocol="BINARY" p:locatorType="ARRAY_MOD" p:failureMode="Redistribute" p:transcoder-ref="serialTranscoder"> <property name="hashAlg"> <util:constant static-field="net.spy.memcached.DefaultHashAlgorithm.FNV1A_64_HASH" /> </property> </bean> </constructor-arg> <!-- TGT timeout in seconds --> <constructor-arg index="1" value="36000" /> <!-- ST timeout in seconds --> <constructor-arg index="2" value="2" /> </bean> <bean id="serialTranscoder" class="net.spy.memcached.transcoders.SerializingTranscoder" p:compressionThreshold="2048" /> <!-- memcached 配置结束 --> <!--Quartz --> <!-- 默认配置开始 --> <!-- Ticket Registry --> <!-- <bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.DefaultTicketRegistry" />--> <!-- TICKET REGISTRY CLEANER --> <!-- <bean id="ticketRegistryCleaner" class="org.jasig.cas.ticket.registry.support.DefaultTicketRegistryCleaner" p:ticketRegistry-ref="ticketRegistry" p:logoutManager-ref="logoutManager" /> <bean id="jobDetailTicketRegistryCleaner" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" p:targetObject-ref="ticketRegistryCleaner" p:targetMethod="clean" /> <bean id="triggerJobDetailTicketRegistryCleaner" class="org.springframework.scheduling.quartz.SimpleTriggerBean" p:jobDetail-ref="jobDetailTicketRegistryCleaner" p:startDelay="200000" p:repeatInterval="50000000" /> --> <!-- 默认配置结束 --> </beans> 3.2 添加cas和memcached整合的如下依赖jar包到/CAS/WEB-INF/lib: cas-server-integration-memcached-4.0.0.jar mockito-core-2.1.0-RC.1.jar spymemcached-2.11.2.jar 参考CAS官方配置:https://apereo.github.io/cas/4.2.x/installation/Memcached-Ticket-Registry.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值