因为公司项目需求,要实现两个系统的单点登录(SSO)联动。例如,在A系统登录,再从A系统跳转至B系统时无需再次登录。
在完成SSO登录之前,需要先实现跨域的数据传输,在网上找了半天,全是半拉子工程,参差不齐,写的一点儿也全。摸索了一天才弄出来一个小成品。所以赶在下班之前分享出来。
先建立两个web项目(取名A项目和B项目。A和B的tomcat端口号要不一致,模拟跨域)。
1.首先是Ajax+Servlet
A为请求端,请求从B项目获取数据。请求方式为Ajax。代码如下:
function test(){
$.ajax({
//接收端使用Servlet
url:"http://localhost:8089/testServlet",
type:"get",
dataType:"text",
success:function(data){
alert(data);
}
})
}
B为接收端,获取A的请求,并返回数据,方式为Servlet。代码如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException,IOException{
//添加响应头,让其允许跨域
response.addHeader("Access-Control-Allow-Origin","*");
//返回数据"Hello World"
response.getWriter().write("Hello World");
}
这样A系统执行test()方法后就会收到B系统的 回应,并alert“Hello World”。
2.然后是jsonp+Servlet
jsonp和Ajax的区别是 把 dataType 改为 ‘jsonp’。 也有可选的两个其他参数jsonp,和jsonCallback。
function test2(){
alert("2");
$.ajax({
url:"http://localhost:8089/testServlet",
type:"get",
dataType:"jsonp",
//(1)
//jsonp:"call",
//(2)
//jsonpCallback:"otherMethod",
success:function(data){
alert(data);
}
})
}
function otherMethod(){
alert("otherMethod");
}
发现后面带了一个叫 ‘callback’的参数 ,这个参数名称可以修改,上面代码中(1)的jsonp:‘ call’,这个'call'就是请求参数的名称,当不写也是可以的,系统会默认添加一个‘callback’。代码中(2)的jsonpCallback‘otherMethod’是指当服务器收到请求并且相应完成后,执行名称为‘otherMethod’方法,而且要比success方法要先执行。
Serlvet 的代码为
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str= "123";
//前端会默认生成一个callback参数,当然你也可以自己指定参数名称
//当前端的jsonp设定为‘call’时,下面需修改为request.getParameter("call")
String method = request.getParameter("callback");
str= method + "(" + str+ ")";
response.getWriter().write(str);
}
前端代码不变
function test3(){
$.ajax({
url:"http://localhost:8081/testJsonp",
data:{param1:"1"},
type:"get",
dataType:"jsonp",
jsonp:"call",
//jsonpCallback:"otherMethod",
success:function(data){
alert(data);
}
})
}
Action 代码
package com.keerqin; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; public class login extends ActionSupport{ HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); private String result; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String sout(){ String param1 = request.getParameter("param1"); System.out.println(param1); result = "Hello World"; /*String callback = request.getParameter("call"); result = callback + "(" + result + ")";*/ return SUCCESS; } } 这里注释的部分即使注释,跨域请求依旧好用。打开的话,前端的success函数alert时会将‘callback’的值和result值一并输出。 struts.xml代码
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default,json-default"> <action name="testJsonp" class="com.keerqin.login" method="sout"> <result type="json" name="success"> <param name="root">result</param> <param name="callbackParameter">call</param> </result> </action> </package> </struts>