【系统安全】API 成批分配利用可能导致特权升级、数据篡改、绕过安全机制-偷懒方式
- 工作小总结
- 时间:2023-07-04 16:36
- 3689人已阅读
简介
在上一篇文章中《【系统安全】appscan扫出API成批分配问题解决方案》 有两种方式来解决。其中第一种方式,添加的话,请注意,这种配置方式适用于整个应用程序范围内的所有@RequestBody参数。那么有没有只针对指定接口进行严格的序列话校验呢?答案:有。思路:在Jackson配置中,配置多个bean对象,其中一个主的不进行严格校验,另一个进行严格校验。在需要使用严格校验的方法时候,使
🔔🔔好消息!好消息!🔔🔔
有需要的朋友👉:微信号
在上一篇文章中《【系统安全】appscan扫出API成批分配问题解决方案》 有两种方式来解决。其中第一种方式,添加的话,请注意,这种配置方式适用于整个应用程序范围内的所有@RequestBody参数。
那么有没有只针对指定接口进行严格的序列话校验呢?答案:有。
思路:
在Jackson配置中,配置多个bean对象,其中一个主的不进行严格校验,另一个进行严格校验。在需要使用严格校验的方法时候,使用严格校验的进行反序列化。具体如下:
一、Jackson配置类:
package com.kaigejava.common.config;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaigejava.login.entity.SysLoginModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
/**
* @author kaigejava
*/
@Configuration
public class JacksonConverters {
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.build();
return objectMapper;
}
@Bean
public ObjectMapper customObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.build();
// 自定义的ObjectMapper配置
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
return objectMapper;
}
}注意:代码中使用了@Primary注解。表示默认的
二、使用的地方
以登录为例。在登录的controller类中的登录方法中进行修改
思路:
private final ObjectMapper customObjectMapper;
@Autowired
public LoginController(@Qualifier("customObjectMapper") ObjectMapper customObjectMapper) {
this.customObjectMapper = customObjectMapper;
}
@ApiOperation("登录接口")
@PostMapping(value = "/login")
public ResultEntity login(@RequestBody Object json) {
ResultEntity result = new ResultEntity();
try {
// 使用自定义的ObjectMapper进行反序列化
SysLoginModel sysLoginModel = null;
try {
sysLoginModel = customObjectMapper.readValue(JSONUtil.toJsonStr(json), SysLoginModel.class);
} catch (JsonProcessingException e) {
log.error("登录的时候,属性不一直,反序列话异常了。",e);
result.setCode(400);
result.setMessage("参数异常。请确认参数是否正确!");
return result;
}
result.setCode(200);
return result;
}