话不多说了,直接上代码
第一种方式:
$.getJSON("http://localhost:7777/vc/login?jsoncallback=?",
{
userId:'SCMHUBIAO',tokenId:'1'
},
function(data){
alert(data);
});
第二种方式:
$.ajax({
type : "get",
async:false,
url : "http://10.9.116.235:7777/vc/login?userId=eee&tokenId=111",
dataType : "jsonp",//数据类型为jsonp
jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数
success : function(data){
}
});
服务端代码:
/**
* 第三方验证
*
* @param userId
* @param tokenId
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@RequestMapping(value = "/login")
public void login(String userId, String tokenId, HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
PrintWriter out = response.getWriter();
SsoLoginResult ssoLoginResult = new SsoLoginResult();
response.setContentType("application/x-json");
//保证返回数据中的中文信息能够正常解码
response.setCharacterEncoding("utf-8");
ExecuteResult<VendorUserDTO> executeResult = this.vcUserServiceFacade.queryByUserName(userId);
if (executeResult.isSuccess()) {
// Http 请求参数
Map<String, Object> checkParamsMap = new HashMap<String, Object>();
checkParamsMap.put("userId", userId);
checkParamsMap.put("tokenId", tokenId);
//获取惠买token验证请求地址
String httpUrl = ConfigUtil.getProperty("huimaiServer");
//Map 2 JSON Object
String params = JSONArray.toJSONString(checkParamsMap);
String postParams = "data=" + params;
//post请求返回值,验证TOKEN是否有效
// String tokenResult = HttpClientHelper.getInstance().sendHttpPost(httpUrl,postParams);
// fastjosn 解析
// JSONObject object = JSON.parseObject(tokenResult);
// String status = object.getString("flag");
/*就是代表当前的用户*/
Subject currentUser = SecurityUtils.getSubject();
if ("1".equals("1")) {
VendorUserDTO vendorUserDTO = executeResult.getResult();
//创建认证令牌
DefaultUsernamepasswordToken aShareLoginToken = new DefaultUsernamepasswordToken();
aShareLoginToken.setUsername(vendorUserDTO.getUserName());
aShareLoginToken.setLoginType("huimai");
//执行登录
currentUser.login(aShareLoginToken);
//获取供应商门户登录跳转首页
String url = ConfigUtil.getProperty("vendorIndex");
ssoLoginResult.setForwardURL(url);
ssoLoginResult.setStatus(1);
ssoLoginResult.setSuccessMsgInfo("{" + vendorUserDTO.getUserName() + "}认证通过");
} else {
//ssoLoginResult.setErrorMsgInfo("<TOKEN验证>:" + object.getString("msg"));
}
} else {
huiMaiSsoLoginResult.setErrorMsgInfo("<用户身份验证>:" + executeResult.getErrorMessages().get(0));
}
//设置ajax回调函数以及返回值
String resultJSON = JSONObject.toJSONString(huiMaiSsoLoginResult); //根据需要拼装json
String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数
out.println(jsonpCallback + "(" + resultJSON + ")");//返回jsonp格式数据
out.flush();
out.close();
}