springMvc 传递参数,特殊字符会被转义或过滤掉,造成后台拿到的数据不正确

项目开发中 遇到springMvc 传递参数,特殊字符会被转义或过滤掉,造成后台拿到的数据不正确。

比如cookie

前台cookie

JSESSIONID=JSESSIONID=3FED1F63756106B3578F28BE154380D8; b_account_username=9kXge%2BjwS7TvYnajLmkjHg%3D%3D; b_account_token=60095d6fcf267df46f74dddd58c43324.1537436740045; b_account_aid=EQbZysPljJc%3D; b_account_salt=8T%2BFp0ngOeT4fbJzhXKirg%3D%3D.1537436740045; b_account_cpd_uid=null

传递到后台

JSESSIONID=JSESSIONID=3FED1F63756106B3578F28BE154380D8; b_account_username=9kXge+jwS7TvYnajLmkjHg==; b_account_token=60095d6fcf267df46f74dddd58c43324.1537436740045; b_account_aid=EQbZysPljJc=; b_account_salt=8T+Fp0ngOeT4fbJzhXKirg==.1537436740045; b_account_cpd_uid=null

会发现==、+号被转义了、#号会被过滤掉

解决办法

js通过base64对参数进行加密

var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv"
+ "wxyz0123456789+/" + "=";
function encode64(input) {
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;
    do {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                    enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                    enc4 = 64;
            }
            output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
                            + keyStr.charAt(enc3) + keyStr.charAt(enc4);
            chr1 = chr2 = chr3 = "";
            enc1 = enc2 = enc3 = enc4 = "";
    } while (i < input.length);

    return output;
}

后台通过base64进行解密

Encodes.decodeBase64String(passwd);

后台的解密就不多说了,导个jar报就行了


	  commons-codec
	  commons-codec
	  20041127.091804
	

有喜欢的朋友可以关注下头条号《老徐聊技术》

 

 

你可能感兴趣的:(java,web)