我需要使用加密参数从客户端(JS)调用URL。然后,我需要从它去的服务器端控制器解密URL。
假设我有
window.location.href = "/app/submitted?docId=445";
我需要加密docId=445
部分。我可以使用atob/btoa
作为
window.location.href = "/app/submitted?" + atob("docId=445"); // Whole Param
window.location.href = "/app/submitted?docId=" + atob("445"); // Just the value
我更喜欢#1,但是在这两个方面,当我来到SpringMVC Controller时,我将如何提取和解密此参数?是否有与atob/btoa
等效的Java加密?
@GetMapping("/app/submitted")
public ModelAndView submitted(HttpServletRequest request, HttpServletResponse response,
@RequestParam("docId") Integer docId) {
//...
// How to decrypt here (either whole param or just the value) from an atob/btoa?
}
还是应该在这里使用编码而不是加密来保证两层的等效性?我的目标是避免在地址栏中的任何位置显示ID。
答案 0 :(得分:0)
我认为您要的是服务器端的base64编码/解码-是的,Java最肯定可以做到这一点
@GetMapping("/app/submitted")
public ModelAndView submitted(HttpServletRequest request, HttpServletResponse response,
@RequestParam("docId") String docId) {
//...
byte[] decodedDocIdBytes = Base64.getDecoder().decode(docId);
String decodedDocIdAsString = new String(decoodDocIdBytes);
}
请注意,docId已更改为字符串。
作为一个补充说明-base64代码不是加密的,因此,如果您真的想保守docId的秘密,我会寻找另一种方法。如果这是REST(ish)API,则资源ID的(docId)不应具有任何外部含义,因此不需要隐藏它们。