跨域请求session共享问题

服务端代码

     @RequestMapping("/checkImgCode")
     @ResponseBody
    public String checkImgCode(String imgCode,String mobile,HttpServletRequest request ,HttpServletResponse response) throws IOException {
        String message="fail";
        Integer respCode=Constants.SERVER_INNER_ERRORCODE;
        Result result=new Result(respCode,message);
       

        //这里省略一些业务代码.........

        

        // 指定允许其他域名访问
        response.setHeader("Access-Control-Allow-Origin", "*");
        // 响应类型
        response.setHeader("Access-Control-Allow-Methods", "POST");
        // 响应头设置

        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type"); 

        //设置允许跨域传输cookie 

        response.setHeader("Access-Control-Allow-Credentials","true");
        String s = JSON.toJSONString(result);
        //配合ajax跨域请求
        String string="
successCallback ("+s+")";
        return string;

    }


前端代码

 $.ajax({
        type:"get",
        async : true,
        dataType:'jsonp',
        jsonp:'callback',
       
jsonpCallback:"successCallback",//这个名字跟后台名字需要匹配
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        url:springbootUrl+'/book/checkImgCode?mobile='+mobile+'&imgCode='+imgCode,/*url写异域的请求地址*/

        success:function(data){

        }

Tips:

    经过测试windows谷歌浏览器等常用浏览器无问题,大部分安卓设备也无问题,可是到了ios系统safari浏览器出现了session不一致问题,每跨域请求一次都刷新了sessionId,可在ios系统设置里safari浏览器>隐私与安全>阻止跨网站跟踪(关闭此选项)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class Session { //mysql的主机地址 const db_host = "localhost"; //需要第三方指定ip地址 //数据库用户名 const db_user = "root"; //需要第三方指定自己的用户名 //数据库密码 const db_pwd = ""; //需要第三方指定自己的库据库密码 //数据库 const db_name = "thinkphp"; //需要第三方指定数据库 //数据库表 const db_table = "tbl_session"; //需要第三方指定数据表 //mysql-handle private $db_handle; //session-lifetime private $lifeTime; function open($savePath, $sessName) { // get session-lifetime $this--->lifeTime = get_cfg_var("session.gc_maxlifetime"); // open database-connection $db_handle = @mysql_connect(self::db_host, self::db_user, self::db_pwd); $dbSel = @mysql_select_db(self::db_name, $db_handle); // return success if(!$db_handle || !$dbSel) return false; $this->db_handle = $db_handle; return true; } function close() { $this->gc(ini_get('session.gc_maxlifetime')); // close database-connection return @mysql_close($this->db_handle); } function read($sessID) { // fetch session-data $res = @mysql_query("SELECT session_data AS d FROM ".self::db_table." WHERE session_id = '$sessID' AND session_expires > ".time(), $this->db_handle); // return data or an empty string at failure if($row = @mysql_fetch_assoc($res)) return $row['d']; return ""; } function write($sessID, $sessData) { // new session-expire-time $newExp = time() + $this->lifeTime; // is a session with this id in the database? $res = @mysql_query("SELECT * FROM ".self::db_table." WHERE session_id = '$sessID'", $this->db_handle); // if yes, if(@mysql_num_rows($res)) { // ...update session-data @mysql_query("UPDATE ".self::db_table." SET session_expires = '$newExp', session_data = '$sessData' WHERE session_id = '$sessID'", $this->db_handle); // if something happened, return true if(@mysql_affected_rows($this->db_handle)) return true; } else // if no session-data was found, { // create a new row @mysql_query("INSERT INTO ".self::db_table." ( session_id, session_expires, session_data) VALUES( '$sessID', '$newExp', '$sessData')", $this->db_handle); // if row was created, return true if(@mysql_affected_rows($this->db_handle)) return true; } // an unknown error occured return false; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值