cas 单点登录出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接 Connection refused

cas 单点登录出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接 Connection refused

环境:

  • CentOS Linux release 7.2.1511 (Core)
  • cas-client-core-3.1.12.jar

最近在对cas环境进行切换,原来好好的环境。部署在新环境下却出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接异常。

2016-08-18 17:05:08.718 [http-bio-8080-exec-9] ERROR org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接
java.net.ConnectException: 拒绝连接
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.7.0_80]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[na:1.7.0_80]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[na:1.7.0_80]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[na:1.7.0_80]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.7.0_80]
        at java.net.Socket.connect(Socket.java:579) ~[na:1.7.0_80]
        at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625) ~[na:1.7.0_80]
        at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160) ~[na:1.7.0_80]
        at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.7.0_80]
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) ~[na:1.7.0_80]
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) ~[na:1.7.0_80]
        at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264) ~[na:1.7.0_80]
        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367) ~[na:1.7.0_80]
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) ~[na:1.7.0_80]
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933) ~[na:1.7.0_80]
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) ~[na:1.7.0_80]
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301) ~[na:1.7.0_80]
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) ~[na:1.7.0_80]
        at org.jasig.cas.client.util.CommonUtils.getResponseFromServer(CommonUtils.java:313) [cas-client-core-3.1.12.jar:na]
        at org.jasig.cas.client.util.CommonUtils.getResponseFromServer(CommonUtils.java:291) [cas-client-core-3.1.12.jar:na]
        at org.jasig.cas.client.validation.AbstractCasProtocolUrlBasedTicketValidator.retrieveResponseFromServer(AbstractCasProtocolUrlBasedTicketValidator.java:32) [cas-client-core-3.1.12.jar:na]
        at org.jasig.cas.client.validation.AbstractUrlBasedTicketValidator.validate(AbstractUrlBasedTicketValidator.java:187) [cas-client-core-3.1.12.jar:na]
        at org.jasig.cas.client.validation.AbstractTicketValidationFilter.doFilter(AbstractTicketValidationFilter.java:164) [cas-client-core-3.1.12.jar:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.68]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.68]
        at org.jasig.cas.client.authentication.AuthenticationFilter.doFilter(AuthenticationFilter.java:102) [cas-client-core-3.1.12.jar:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.68]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.68]
        at org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:65) [cas-client-core-3.1.12.jar:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.68]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.68]
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) [spring-web-4.2.2.RELEASE.jar:4.2.2.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.2.RELEASE.jar:4.2.2.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.68]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.68]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) [catalina.jar:7.0.68]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [catalina.jar:7.0.68]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) [catalina.jar:7.0.68]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) [catalina.jar:7.0.68]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) [catalina.jar:7.0.68]
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) [catalina.jar:7.0.68]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) [catalina.jar:7.0.68]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) [catalina.jar:7.0.68]
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) [tomcat-coyote.jar:7.0.68]
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) [tomcat-coyote.jar:7.0.68]
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) [tomcat-coyote.jar:7.0.68]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_80]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_80]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-coyote.jar:7.0.68]
        at java.lang.Thread.run(Thread.java:745) [na:1.7.0_80]

找了很久也找不到是什么原因,最后换了一种链接方式后问题才得以解决。估计这是个bug。

修改org.jasig.cas.client.util.CommonUtils类的public static String getResponseFromServer(final URL constructedUrl, final HostnameVerifier hostnameVerifier, final String encoding)方法。

将原有的

public static String getResponseFromServer(final URL constructedUrl, final HostnameVerifier hostnameVerifier, final String encoding) {
        URLConnection conn = null;
        try {
        conn = constructedUrl.openConnection();
        if (conn instanceof HttpsURLConnection) {
            ((HttpsURLConnection)conn).setHostnameVerifier(hostnameVerifier);
        }
        final BufferedReader in;

        if (CommonUtils.isEmpty(encoding)) {
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        } else {
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
        }

        String line;
        final StringBuilder stringBuffer = new StringBuilder(255);

        while ((line = in.readLine()) != null) {
            stringBuffer.append(line);
            stringBuffer.append("\n");
        }

        return stringBuffer.toString();

    } catch (final Exception e) {
        LOG.error(e.getMessage(), e);
        throw new RuntimeException(e);
    } finally {
        if (conn != null && conn instanceof HttpURLConnection) {
            ((HttpURLConnection)conn).disconnect();
        }
    }

}

cas客户端作如下修改

  • 添加远程连接类:
public class HttpRemote {
    /**
     * 以Post方法访问
     * @param url 请求地址
     * @param argsMap 携带的参数
     * @return String 返回结果
     * @throws Exception
     */
    public static String POSTMethod(String url, Map<String, Object> argsMap) throws Exception {
        byte[] dataByte = null;

        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient httpClient = httpClientBuilder.build();

        HttpPost httpPost = new HttpPost(url);
        // 设置参数
        UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(setHttpParams(argsMap), "UTF-8");
        httpPost.setEntity(encodedFormEntity);
        // 执行请求
        HttpResponse httpResponse = httpClient.execute(httpPost);
        // 获取返回的数据
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpPost.abort();
        }
        // 将字节数组转换成为字符串
        String result = bytesToString(dataByte);
        return result;
    }
}
  • getResponseFromServer方法修改为:
public static String getResponseFromServer(final URL constructedUrl, final HostnameVerifier hostnameVerifier, final String encoding) {
    URLConnection conn = null;
    try {
//            conn = constructedUrl.openConnection();
//            if (conn instanceof HttpsURLConnection) {
//                ((HttpsURLConnection)conn).setHostnameVerifier(hostnameVerifier);
//            }
//            final BufferedReader in;
//
//            if (CommonUtils.isEmpty(encoding)) {
//                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//            } else {
//                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
//            }
//
//            String line;
        final StringBuilder stringBuffer = new StringBuilder(255);
//
//            while ((line = in.readLine()) != null) {
//                stringBuffer.append(line);
//                stringBuffer.append("\n");
//            }

        stringBuffer.append(HttpRemote.POSTMethod(constructedUrl.toString(),null));

        return stringBuffer.toString();

    } catch (final Exception e) {
        LOG.error(e.getMessage(), e);
        throw new RuntimeException(e);
    } finally {
        if (conn != null && conn instanceof HttpURLConnection) {
            ((HttpURLConnection)conn).disconnect();
        }
    }

}

重新打包重启即可。

时间: 2024-11-08 14:50:13

cas 单点登录出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接 Connection refused的相关文章

JAVA CAS单点登录之二:CAS普通模式1演练

前言 经过上一JAVA CAS单点登录之一:搭建CAS服务器 这一章,CAS服务器已经搭建好了.当然这时候的CAS服务器仅仅是最初版本的.比如密码验证,页面美观度都需要进一步调整.但这都是无关紧要的. 最主要的是先把整个一套认证流程走下来,至于完善的工作,都是没个点的工作了,相对比较简单. 主要内容    1. 新建一个web应用mywebapp1,测试与CAS服务器的认证效果     2.如果上一步骤1认证成功的话,将mywebapp1复制一份,调整若干参数,继续测试.     如果步骤1,步

JAVA CAS单点登录之一:搭建CAS服务器

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://dba10g.blog.51cto.com/764602/1753086 前言 这一段时间,心血来潮研究了下CAS这个应用.对它感兴趣,几年前就有过,就是一直没有踏实好好研究,一直是断断续续的纸上谈兵,东看看,西看看.CAS说白了主要涉及权限认证相关,逻辑非常简单.但真正部署起来,也不是一件容易事.我想主要原因如下. 由于知名原因,访问官网非常有难度,系统全面地获取资料非常难,且

JAVA CAS单点登录之三:CAS代理模式演练

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://dba10g.blog.51cto.com/764602/1753244 前言  JAVA CAS单点登录之一:搭建CAS服务器     JAVA CAS单点登录之二:CAS普通模式1演练     代理模式相相对上一节的普通模式,更加复杂了.但配置起来也会稍微有些差别.所谓难者不会,会者不难.如果遇到一个从来没有遇到的问题,解决起来也是非常棘手的,当然解决之后就不是事了.我就遇到

CAS单点登录之mysql数据库用户验证及常见问题

前面已经介绍了CAS服务器的搭建,详情见:搭建CAS单点登录服务器.然而前面只是简单地介绍了服务器的搭建,其验证方式是原始的配置文件的方式,这显然不能满足日常的需求.下面介绍下通过mysql数据库认证的方式. 一.CAS认证之mysql数据库认证 1.在mysql中新建一个cas数据库并创建user表 CREATE DATABASE /*!32312 IF NOT EXISTS*/`cas` /*!40100 DEFAULT CHARACTER SET gbk */; USE `cas`; /*

JAVA CAS单点登录之四:CAS服务器增加JDBC访问能力

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://dba10g.blog.51cto.com/764602/1753680 经过前面说明,已经完成了CAS服务器的搭建,代理客户端的搭建以及普通客户端,back-end-service客户端的搭建.如果不明白的,参照如下链接. JAVA CAS单点登录之一:搭建CAS服务器  JAVA CAS单点登录之二:CAS普通模式1演练 JAVA CAS单点登录之三:CAS代理模式演练  现

CAS单点登录(SSO)完整教程

CAS单点登录(SSO)完整教程(2012-02-01更新) 一.教程说明 前言 教程目的:从头到尾细细道来单点登录服务器及客户端应用的每个步骤 单点登录(SSO):请看百科解释猛击这里打开 本教程使用的SSO服务器是Yelu大学研发的CAS(Central Authentication Server), 官网:http://www.jasig.org/cas 本教程环境: Tomcat6.0.29 JDK6 CAS Server版本:cas-server-3.4.3.1.cas-server-

spring-Springsecurity cas单点登录,循环重定向问题

问题描述 Springsecurity cas单点登录,循环重定向问题 最近在弄springsecurity+cas实现单点登录,但配置完成测试,去发现在cas server端登录成功之后,竟出现了循环重定向问题,我springsecurity配置如下: <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/b

java-【急】求大神帮帮忙,CAS单点登录问题,登录成功后controller收不到请求(要哭了)

问题描述 [急]求大神帮帮忙,CAS单点登录问题,登录成功后controller收不到请求(要哭了) 首先说下问题,我们的项目之前SSO单点登录一直用的http的方式, 这一块登录一直都是正常的,现在要切上正式环境了,SSO服务器那边就改用了https的方式SSO配置如下: <listener> <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-cla

SpringSecurity+JPA+CAS单点登录

问题描述 SpringSecurity+JPA+CAS单点登录错误信息如下:ERRORContextLoader:220-Contextinitializationfailedorg.springframework.beans.factory.BeanDefinitionStoreException:UnexpectedexceptionparsingXMLdocumentfromServletContextresource[/WEB-INF/classes/applicationContext