解决跨网场景下,CAS重定向无法登录的问题(无需修改现有代码)

 

 

背景:

使用CAS登录的过程中会涉及到三次重定向,如果在同一个局域网内,是没有任何问题的,但如果涉及到跨网访问(如内网部署、外网访问)就有问题了:

如下图两个红五角星处,外网浏览器要访问的登录页面IP和内网做ticket验证的IP,对cas客户端来说是同一个IP,如果配置的是内网IP,则浏览器无法访问登录页;如配置的是外网IP,则内网客户端无法验证ticket。

 

解决思路:

因系统已上线,且cas客户端应用比较多,通过改代码的方式比较麻烦,因此需要寻求不改代码的方式来解决问题:

思路一:内网应用配置成外网IP,然后通过iptables的方式在内网服务器上做个映射,将外网IP映射到内网IP上。此种方式可解决“验证ticket”不通的问题,但只能在外网访问,无法通过内网访问。

思路二:内网应用配置成内网IP,然后通过nginx对响应头和响应体中的内网IP替换成外网IP,这样就可以在浏览器上使用外网IP来访问系统了。此方法可在内外网都可以访问。

 思路三(推荐):通过域名的方式来访问cas,内外网各部署一个DNS服务器,外网的DNS将域名解析到外网能访问的IP上,内网DNS将域名解析到内网IP上

 

 

 

 

 

思路一解决办法:

iptables -t nat -I OUTPUT -d 218.94.x.x -p tcp --dport 8476 -j DNAT --to-destination 10.1.x.x:8088

service iptables save

该方法用于将外网IP和端口转成内网IP和端口

218.94.x.x 和 8476 分别为外网IP和端口
10.1.x.x:8088 为内网IP和端口

 

 

思路二解决办法:

通过Nginx对要访问的系统进行代理,把响应头中的重定向Location的地址改成外网能访问到的IP,实现跨网访问。

 

实现步骤:

1、安装Nginx,安装ngx_headers_more模块(下载路径:https://github.com/openresty/headers-more-nginx-module/tags

安装方式:进入nginx的tar包解压目录,执行./configure --prefix==/usr/local/nginx  --add-module=/home/nginx/ngx_headers_more解压后的目录  --add-module=其他模块如echo模块

上述命令执行完成后,执行make,make install 重新安装nginx

2、配置nginx如下:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    
    #高版本的Nginx用这种方式     注意:有的版本中,通过$upstream_http_Location会一直取不到值,可以使用$sent_http_location来代替,$sent_http_location是不带IP的请求路径
    map $sent_http_location $location{
        ~/xxx-cas([\S]+$) http://130.13.11.24:8888/xxx-cas$1;
        ~/xxx-auth([\S]*$) http://130.13.11.24:8888/xxx-auth$1;
        ~/zhcx([\S]*$) http://130.13.11.24:8888/zhcx$1;
        ~/sjpz([\S]*$) http://130.13.11.24:8888/sjpz$1;
        default abcd$sent_http_location;
    }
    
    #低版本的Nginx用这种方式     注意:有的版本中,通过$upstream_http_Location会一直取不到值,可以使用$sent_http_location来代替,$sent_http_location是不带IP的请求路径
    map $upstream_http_Location $location{
        ~http://192.168.0.10:8088/xxx-cas([\S]+$) http://130.13.11.24:8888/xxx-cas$1;
        ~http://192.168.0.10:8088/xxx-auth([\S]*$) http://130.13.11.24:8888/xxx-auth$1;
        ~http://192.168.0.10:8081/zhcx([\S]*$) http://130.13.11.24:8888/zhcx$1;
        ~http://192.168.0.10:8082/sjpz([\S]*$) http://130.13.11.24:8888/sjpz$1;
        default abcd$upstream_http_Location;
    }

    server {
        listen       8080;
        server_name  localhost;
        location /xxx-auth {
            proxy_pass http://192.168.0.10:8088;
            more_set_headers -s '302' "Location $location";
        }
        
        location /xxx-cas {
            proxy_pass http://192.168.0.10:8088;
            more_set_headers -s '302' "Location $location";
        }
        
        location /zhcx {
            proxy_pass http://192.168.0.10:8081;
            more_set_headers -s '302' "Location $location";
        }  
        
        location /sjpz {
            proxy_pass http://192.168.0.10:8082;
            more_set_headers -s '302' "Location $location";
        } 
    }
    
}

 

思路三解决办法(推荐):

通过域名的方式来访问cas,内外网各部署一个DNS服务器,外网的DNS将域名解析到外网能访问的IP上,内网DNS将域名解析到内网IP上

 

posted @ 2019-03-16 15:47  阿狸哥哥  阅读(5143)  评论(0编辑  收藏  举报