自定义ajax登录的时候出现的问题
我写了个简单的ajaxlogin登录
情况描述
打开2个页面
第一个页面是ajax登录的页面
第二个页面是登录后收保护的页面
登录第一个页面, 打开受保护的页面2, 在第一个页面登出 并刷新第2个页面由于已经登出就会出现跳到拦截页面CAS的login(这是已经把要跳转的页面存在session里了)
然后在第一个登录页面登录由于第一个页面时ajax请求,并且在session中已经存在他要跳转的页面,所以会返回该跳转的页面,ajax中设置了只能接受json对象因此就会出现登录error其实已经登入了
session中存的跳转URL的格式是
session
{SPRING_SECURITY_SAVED_REQUEST_KEY=SavedRequest[http://localhost:8080/AVerPortal/resourceAction/resourceIndex.action]}
其实这是个请求封装即对于页面http://localhost:8080/AVerPortal/resourceAction/resourceIndex.action的请求
由于是自定义ajaxlogin因此当有拦截目标的时候返回的是一个请求不是一个JSON对象。
解决方案:重写源代码中的AbstractProcessingFilter在验证成功时候使用的 RedirectUtils.sendRedirect类:这个方法顾名思义是像页面发送一个对服务器端页面的请求。response.sendRedirect(response.encodeRedirectURL(finalUrl));
改写成:
StringBuffer str = new StringBuffer();
str.append("{");
str.append("status: \"true\",");
str.append("url: \"");
str.append(finalUrl);
str.append("\"}");
response.getWriter().write(str.toString());
注意这里的我原来写成
StringBuffer str = new StringBuffer();
str.append("{");
str.append("status: true,");
str.append("url: ");
str.append(finalUrl);
str.append("}");
response.getWriter().write(str.toString());
结果在前台却得到
{status: true,url: http://localhost:8080/AVerPortalTest/resourceAction/resourceIndex.action}
{status: true,url: http://localhost:8080/AVerPortalTest/resourceAction/resourceIndex.action}
2份JSON对象原因是因为JSON格式,正确格式如下
{
status:"true",
&nbs
相关文档:
最近网上提的很多的一个新概念就是 AJAX 了, 那么, AJAX 是什么呢? 以下内容引用网上资料:
AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。它有机地包含了以下几种技术:
Ajax(Asynchronous JavaScript + XML)的定义
基于 web标准(sta ......
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<mce:style type="text/css"><!--
.n{TEXT-DECORATION:none;cursor:pointer} a{color:black} a:hover{color:blue}
.m{TEXT-DECORATION:none;c ......
function CreateXmlHttp()
{
if(window.ActiveXObject)
{
try
{
XmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){};
try
{
XmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){};
}
else if(window.XMLHt ......
在构造url是用javascript自带的encodeURIComponent方法将参数进行编码,下面是我的代码
var url = "handel.jsp?name="+encodeURIComponent(document.form1.name.value);
httpRequest = createHttpRequest();
httpReque ......
asp.net ajax学习笔记
一、 登堂入室——基本概念
http://blog.csdn.net/soldierluo/archive/2009/11/18/4830758.aspx
二、 小试身手——第一个Ajax程序
http://bl ......