如何在Java Spring Boot中通过@Requestparam传递加密值

huangapple go评论56阅读模式
英文:

How to pass encrypted value in @Requestparam java spring boot

问题

我创建了一个带有参数 @RequestParam String Id 的 Java 服务,当我传递精确值时,它正常工作;但是当我尝试传递加密值时,它无法正常工作。我在下面提供了我的解决方法。

@GetMapping("fetch")
@Produces(value = "application/json")
@ApiOperation("Get user detail from Okta")
public ResponseEntity<UserInfoRespone> getUserDetailsFromOkta(@RequestParam String agentId) {
  
    System.out.println("Actual:" + "c4njCC2/reRud+O/I41w3w==");
    System.out.println("Incoming agent Id:" + agentId);
}

当我使用 Postman 调用该服务并传递加密值 c4njCC2/reRud+O/I41w3w== 时,但是当我尝试打印传入的代理 ID 时,我发现实际文本中缺少了一些字符,看起来像是 c4njCC2/reRud O/I41w3w==,它丢失了 + 符号并在自身中添加了空格。因此,当我尝试解密该字符串时,结果为 null。

期望值:c4njCC2/reRud+O/I41w3w==
实际值:c4njCC2/reRud O/I41w3w==

是否有人可以帮助我解决这个问题?

英文:

I have created a java service with parameter @Requestparam String Id when i was passing the Exact value it's working fine when i try to pass the encrypted value it is not working properly. I have given my work around below.

@GetMapping(&quot;fetch&quot;)
@Produces(value = &quot;application/json&quot;)
@ApiOperation(&quot;Get user detail from Okta&quot;)
public ResponseEntity&lt;UserInfoRespone&gt; getUserDetailsFromOkta(@RequestParam String agentId) {
  
    System.out.println(&quot;Actual:&quot; +&quot;c4njCC2/reRud+O/I41w3w==&quot;);
    System.out.println(&quot;Incoming agent Id:&quot; + agentId);
	
}

When i call the service using postman passed the encrypted value c4njCC2/reRud+O/I41w3w== but when i was trying to print the incoming agent id i found few characters were missing in the actual text looks like c4njCC2/reRud O/I41w3w== it has missed + symbol and added space to itself. so when i tried to decrypt the String it's coming as null.

Expected : c4njCC2/reRud+O/I41w3w==
Actual   : c4njCC2/reRud O/I41w3w==

Could some one help me to resolve this?

答案1

得分: 1

你需要像这样对查询参数进行URL编码:

c4njCC2%2FreRud%2BO%2FI41w3w%3D%3D

有几个网站可以用来对这样的值进行编码/解码(例如 https://www.urlencoder.org/)。使用纯Java代码,你可以使用 java.net.URLEncoder

URLEncoder.encode("c4njCC2/reRud+O/I41w3w==", StandardCharsets.UTF_8);

然而,对于加密数据来说,请求参数可能不是最佳选择,因为即使在编码形式下,您可能会遇到其他字符的问题。

我建议将这些数据提交到请求体中。

英文:

You need to URL encode your query parameter like this:

c4njCC2%2FreRud%2BO%2FI41w3w%3D%3D

There are several websites where you can encode/decode values like this (for example https://www.urlencoder.org/). Using plain java you can use the java.net.URLEncoder:

URLEncoder.encode(&quot;c4njCC2/reRud+O/I41w3w==&quot;, StandardCharsets.UTF_8);

However a request parameter is probably not the best choice for encrypted data as you might get into trouble with other characters (even in encoded form) as well.

I'd suggest to submit this data in the request body instead.

huangapple
  • 本文由 发表于 2020年4月10日 21:18:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61141129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定