项目开发中安全问题以及解决办法——客户请求需要校验

本文讨论了一个JavaWeb应用中如何防止用户获取白名单之外的数据库数据,通过示例展示了`@RequestParam`和SpringValidation的使用,以及隐藏域可能带来的安全风险。
摘要由CSDN通过智能技术生成
@Slf4j
@RequestMapping("trustclientdata")
@Controller
public class TrustClientDataController {
 //所有支持的国家
 private HashMap<Integer, Country> allCountries = new HashMap<>();
 public TrustClientDataController() {
 allCountries.put(1, new Country(1, "China"));
 allCountries.put(2, new Country(2, "US"));
 allCountries.put(3, new Country(3, "UK"));
 allCountries.put(4, new Country(4, "Japan"));
 }
 @GetMapping("/")
 public String index(ModelMap modelMap) {
 List<Country> countries = new ArrayList<>();
 //从数据库查出ID<4的三个国家作为白名单在页面显示
 countries.addAll(allCountries.values().stream().filter(country -> country.getId()<4).collect(Collectors.toList()));
 modelMap.addAttribute("countries", countries);
 return "index";
 }
} 
...
<form id="myForm" method="post" th:action="@{/trustclientdata/wrong}">
 <select id="countryId" name="countryId">
 <option value="0">Select country</option>
 <option th:each="country : ${countries}" th:text="${country.name}" th:value="${country.id}"></option>
 </select>
 <button th:text="Register" type="submit"/>
</form>
...

比如上面的情况,我们从数据库存储的国家中选择其中三个作为白名单进行展示,选择条件是id<4

前端展示如下:

 正常情况下 用户只能看到id<4的数据

@PostMapping("/wrong")
@ResponseBody
public String wrong(@RequestParam("countryId") int countryId) {
 return allCountries.get(countryId).getName();
}

这时候添加了新的网关接口,如果此时用户传入了4 ,比如下面的请求

curl http://localhost:45678/trustclientdata/wrong\?countryId=4 -X POST

 很明显这样就会拿到了不是白名单中的数据

解决办法:

@PostMapping("/right")
@ResponseBody
public String right(@RequestParam("countryId") int countryId) {
 if (countryId < 1 || countryId > 3)
 throw new RuntimeException("非法参数");
 return allCountries.get(countryId).getName();
}

或者使用spring validation,代码如下:

@Validated
public class TrustClientParameterController {
 @PostMapping("/better")
 @ResponseBody
 @ResponseBody
 public String better(
 @RequestParam("countryId")
 @Min(value = 1, message = "非法参数")
 @Max(value = 3, message = "非法参数") int countryId) {
 return allCountries.get(countryId).getName();
 }
}

客户端提交的参数需要校验的问题,可以引申出一个更容易忽略的点是,我们可能会把一些服务端的数据暂存在网页的隐藏域中,这样下次页面提交的时 候可以把相关数据再传给服务端。虽然用户通过网页界面的操作无法修改这些数据,但这些数据对于 HTTP 请求来说就是普通数据,完全可以随时修改为 任意值。所以,服务端在使用这些数据的时候,也同样要特别小心。

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ADRU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值