在使用CAS Server进行单点退出的实验时,一直不成功,最终跟终到的代码是cas server通过HttpURLConnection模拟了一个对客户端应用的POST请求,通过代码的调试发现,问题就出现在POST请求这里,客户端应用的SingleSignOutFilter过滤器接收到了这个请求,但是得到的请求却是GET请求,并且得不到所需的参数值。
为了进一步进行调试,把cas server中的请求过程拿出来做了一个测试类,如下:
public class TestConn { public static void main(String[] args) { String message = "<samlp:LogoutRequest xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\" ID=\"LR-8-3iFpmqdWBa5MAh2VW5u5FiPYvoOu2MOSe7q\" Version=\"2.0\" IssueInstant=\"2013-04-01T15:42:45Z\"><saml:NameID xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\">@NOT_USED@</saml:NameID><samlp:SessionIndex>ST-8-2PZRjTcDPWeBc6X4TVb6-cas</samlp:SessionIndex></samlp:LogoutRequest>"; String url = "http://localhost:8880/web"; int readTimeout = 50000; int connectionTimeout = 50000; HttpURLConnection connection = null; BufferedReader in = null; try { final URL logoutUrl = new URL(url); final String output = "logoutRequest=" + URLEncoder.encode(message, "UTF-8"); connection = (HttpURLConnection) logoutUrl.openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setReadTimeout(readTimeout); connection.setConnectTimeout(connectionTimeout); connection.setRequestProperty("Content-Length", Integer.toString(output.getBytes().length)); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); final DataOutputStream printout = new DataOutputStream( connection.getOutputStream()); printout.writeBytes(output); printout.flush(); printout.close(); in = new BufferedReader(new InputStreamReader( connection.getInputStream())); while (in.readLine() != null) { // nothing to do } } catch (final SocketTimeoutException e) { e.printStackTrace(); } catch (final Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (final IOException e) { // can't do anything } } if (connection != null) { connection.disconnect(); } } } }
这里的http://localhost:8880/web也配置了一个filter,内容如下:
@Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; System.out.println(request.getMethod()); System.out.println(request.getParameter("logoutRequest")); chain.doFilter(req, res); }
运行测试程序,在web端的控制台上面输出的内容为:
GET
null
null
得到这样的一个结果,实在是相当意外,这里测试程序里面可以看到,请求方法已经改为了POST,得到的却是GET请求。还以为是JDK的BUG,于是依次换成jdk1.5 1.6 1.7,多次进行测试,结果依旧。
终于,功夫不负苦心人,楼主一个不经意的举动换来了意想不到的收获,我把这里请求的URL不小心改成了
http://localhost:8880/web/
却在web控制台看到了这样的输出结果:
POST
<samlp:LogoutRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="LR-8-3iFpmqdWBa5MAh2VW5u5FiPYvoOu2MOSe7q" Version="2.0" IssueInstant="2013-04-01T15:42:45Z"><saml:NameID xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">@NOT_USED@</saml:NameID><samlp:SessionIndex>ST-8-2PZRjTcDPWeBc6X4TVb6-cas</samlp:SessionIndex></samlp:LogoutRequest>
<samlp:LogoutRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="LR-8-3iFpmqdWBa5MAh2VW5u5FiPYvoOu2MOSe7q" Version="2.0" IssueInstant="2013-04-01T15:42:45Z"><saml:NameID xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">@NOT_USED@</saml:NameID><samlp:SessionIndex>ST-8-2PZRjTcDPWeBc6X4TVb6-cas</samlp:SessionIndex></samlp:LogoutRequest>
居然正确了!!!
于是,果断把CAS client端的serverName后面增加“/”,再次进行单点退出的实验,成功!
相关推荐
主要介绍了java后台调用HttpURLConnection类模拟浏览器请求实例,该实例可用于接口调用,具有一定的实用价值,需要的朋友可以参考下
创建一个URL对象: URL url = new URL(http://www.baidu.com); 调用URL对象的openConnection( )来获取HttpURLConnection对象实例: HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 设置HTTP...
java网络请求工具类 HttpURLConnection post请求工具类HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式
HttpUrlConnection对象模拟POST带JSON字符串的AJAX请求失败! 我的目的是抓取响应结果字符串"301",但抓取的结果是一堆HTML代码,哪位大神看看哪有毛病?
HttpURLConnection请求和HttpClient请求...................................................................
分别使用HttpURLConnection OKHttp实现请求
android使用Java原生httpUrlConnection进行get请求
基于HttpURLConnection的封装http请求,进行了https校验忽略,对get和post进行了整合,更方便请求,对header以及超时的进行了可配置化处理,对返回也进行了封装,便于请求结果的处理。
Java利用HttpURLConnection发送post请求上传文件
//参数是a=""&b=""这样拼接的一个串 out.flush(); out.close(); out.write(parameters.getBytes(),0,parameters.getBytes().length); InputStream in = hc.getInputStream(); InputStreamReader ...
这个demo实现了Android网络请求添加header请求参数实例,包括HttpURLConnection,AsyncHttpClient两个大请求类实现方法
HttpURLConnection的(get请求、post请求)
使用HttpUrlConnection实现模拟提交表单的一个简单工具类,支持文件和普通参数
Java原生HttpUrlConnection访问WebService实例,附相关jar包...新手整理不喜勿喷
HttpURLConnection的demo,里面有网络请求get post 上传文件,下载文件,介绍HttpURLConnection这个类整体流程是怎样使用的,方便初学者学习
里面详细的写了post和get和HttpPost和HttpGet请求
HttpURLConnection文件下载\httpURLConnection文件下载
这个是我自己集成框架的时候写的一个HTTP请求的辅助类,主要是通过HttpURLConnection 来实现JAVA的HTTP请求,有的人会问、为什么有些网站发请求没问题,有些网站发请求却返回数据,在给网站发送请求的时候,请看清楚...
本人原创测试了百度,搜狗,360搜索都可以获取重定向后的真实地址,直接运行就可以,也可以根据自己项目具体修改功能。java使用HttpURLConnection获取百度搜狗360搜索链接重定向真实目标链接地址url。
请求http及数据返回解析,HttpURLConnection,HttpClient请求http及数据返回解析,HttpURLConnection,HttpClient