编码/解码播放后端和JavaScript客户端之间的URL
我玩过REST API和JavaScript客户端,它在URL中发送一个token:signature对,如下所示:编码/解码播放后端和JavaScript客户端之间的URL,javascript,scala,playframework,url-encoding,Javascript,Scala,Playframework,Url Encoding,我玩过REST API和JavaScript客户端,它在URL中发送一个token:signature对,如下所示: http://localhost:9000/auth/users/all/0/11?auth=eyJhbGciOiJIUzI1N...ciOiJIU:8so/gRFwOoPXp2x6RfyUpMYIMD4= signature = CryptoJS.HmacSHA1(token + method + obj.url.slice(baseUrl.length) + body, a
http://localhost:9000/auth/users/all/0/11?auth=eyJhbGciOiJIUzI1N...ciOiJIU:8so/gRFwOoPXp2x6RfyUpMYIMD4=
signature = CryptoJS.HmacSHA1(token + method + obj.url.slice(baseUrl.length) + body, apiKey).toString(CryptoJS.enc.Base64);
...
obj.url = obj.url + (obj.url.indexOf("?") > 0 ? "&" : "?") + "auth=" + encodeURIComponent(token + ":" + signature);
import java.nio.charset.{StandardCharsets => SC}
import play.utils.UriEncoding
// auth(0) => token, auth(1) => signature
val auth = UriEncoding.decodePathSegment(jwt, SC.US_ASCII.name).split(":")
在JavaScript客户端中,我对URL进行如下编码:
http://localhost:9000/auth/users/all/0/11?auth=eyJhbGciOiJIUzI1N...ciOiJIU:8so/gRFwOoPXp2x6RfyUpMYIMD4=
signature = CryptoJS.HmacSHA1(token + method + obj.url.slice(baseUrl.length) + body, apiKey).toString(CryptoJS.enc.Base64);
...
obj.url = obj.url + (obj.url.indexOf("?") > 0 ? "&" : "?") + "auth=" + encodeURIComponent(token + ":" + signature);
import java.nio.charset.{StandardCharsets => SC}
import play.utils.UriEncoding
// auth(0) => token, auth(1) => signature
val auth = UriEncoding.decodePathSegment(jwt, SC.US_ASCII.name).split(":")
然后,在Play后端,我对URL进行如下解码:
http://localhost:9000/auth/users/all/0/11?auth=eyJhbGciOiJIUzI1N...ciOiJIU:8so/gRFwOoPXp2x6RfyUpMYIMD4=
signature = CryptoJS.HmacSHA1(token + method + obj.url.slice(baseUrl.length) + body, apiKey).toString(CryptoJS.enc.Base64);
...
obj.url = obj.url + (obj.url.indexOf("?") > 0 ? "&" : "?") + "auth=" + encodeURIComponent(token + ":" + signature);
import java.nio.charset.{StandardCharsets => SC}
import play.utils.UriEncoding
// auth(0) => token, auth(1) => signature
val auth = UriEncoding.decodePathSegment(jwt, SC.US_ASCII.name).split(":")
问题是,UriEncoding
由于斜杠而崩溃:
play.api.Application$$anon$1:
Execution exception[[InvalidUriEncodingException: Cannot decode eyJhbGciOiJIUzI1N...vZ5v18d2EZik1ki5W9_6XABi-JA:8so/gRFwOoPXp2x6RfyUpMYIMD4=: illegal character at position 715.]]
现在我的问题是:
encodeURIComponent
编码URL之前,我应该使用base64吗US\u ASCII
或UTF\u 8
刚刚用
UriEncoding.decodePath
替换了UriEncoding.decodePath
,它可以工作:
import java.nio.charset.{StandardCharsets => SC}
import play.utils.UriEncoding
// auth(0) => token, auth(1) => signature
val auth = UriEncoding.decodePath(jwt, SC.US_ASCII.name).split(":")
我希望有帮助