让前端控制台(console/network)不能显示密码
1 让前端控制台(console/network)不能显示密码
1.1 现状
前端向后端发起请求(填充页面表单数据):后端总会把整个User对象的值都返回,导致前端控制台都能看到细节,如下
1.2 原因:springboot–jackson
springboot–jackson = alibaba.fastjson :可以将对象序列化成一个字符串
1.2 解决:密码不显示=返回值不序列化密码
1.2.1 导jackson包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.0</version>
</dependency>
1.2.2 后端加注解 @JsonIgnore
不序列化的属性名上加@JsonIgnore
从后端像前端返回值,不序列化(不返回这个值)
/**
* 明文密码
*/
@TableField(exist = false)
@JsonIgnore
private String rawPassword;
/**
* 加密密码
*/
@TableField("PASSWORD")
@JsonIgnore
private String password;
1.2.3 前端:修改页面不展示密码v-if="!id"
<el-form-item v-if="!id" label="密码" prop="rawPassword">
<el-input v-model="form.rawPassword" show-password></el-input>
</el-form-item>
2 console只展示表单数据(没用的字段不显示)
console.log(this.form)
2.1 现状
getone() {
this.get(this.url.getone, {
id: this.id
}, response => {
this.form = response
console.log(this.form)
})
},
2.1 解决:遍历
getone() {
this.get(this.url.getone, {
id: this.id
}, response => {
for(let key in this.form){
if(response[key]){
this.form[key] = response[key]
}
}
console.log(this.form)
})
},
这种做法弊端:只能在控制台console隐藏冗余信息,但是network依旧可以看到response响应的实际的每一项。