前言:

利用数据库视图,实现web接口查询敏感信息时动态脱敏。

具体目标:某接口为用户信息查询接口,返回敏感用户信息(id,姓名、手机号【敏感】、身份证号【敏感】),如果web用户为管理员角色,则查询后返回明文用户信息,如果用户为普通用户信息,则查询后返回脱敏后的用户信息。

具体步骤:

一、在mysql中新建一个用户信息表,并添加几条数据

二、对上表创建脱敏后的视图,利用掩码技术脱敏,脱敏字段为phone和id_card

create view  user_info_view as select id,name,concat(left(phone,3),'****',right(phone,3)) as phone,concat(left(id_card,4),'**************') as id_card from user_info;

三、SpringBoot项目启用SpringSecurity,在配置文件中,内存新建账号的时候添加admin和normal两个角色

package Eleven.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; @Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("123456")).roles("admin");
auth.inMemoryAuthentication().withUser("user").password(passwordEncoder().encode("123456")).roles("normal");
} @Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests() // 定义哪些URL需要被保护、哪些不需要被保护
.antMatchers("/login").permitAll()// 设置所有人都可以访问登录页面
.anyRequest().authenticated() // 任何请求,登录后可以访问
.and()
.formLogin().loginPage("/login")
; }
}

四、创建UserInfo的domain类

package Eleven.domain;

public class UserInfo {

    private long id;
private String name;
private String phone;
private String id_card; public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getId_card() {
return id_card;
} public void setId_card(String id_card) {
this.id_card = id_card;
}
}

五、创建Mapper,访问数据库的接口,两个查询方法,以便按不同的角色区分查询原始表还是脱敏后的视图

package Eleven.mapper;

import Eleven.domain.UserInfo;
import org.apache.ibatis.annotations.*; @Mapper
public interface UserMapper { //根据用户名查询,查询原始表,明文显示敏感信息
@Select("select * from user_info where name=#{name}")
UserInfo findByName(String name); //根据用户名查询,查询脱敏后的视图表,脱敏显示敏感信息
@Select("select * from user_info_view where name=#{name}")
UserInfo findByNameSec(String name);
}

六、创建Service 和Impl文件

package Eleven.service;

import Eleven.domain.UserInfo;

public interface UserService {

    public UserInfo find(String name);

    public UserInfo findSec(String name);

}
package Eleven.impl;

import Eleven.domain.UserInfo;
import Eleven.mapper.UserMapper;
import Eleven.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper; @Override
public UserInfo find(String name){
return userMapper.findByName(name);
} public UserInfo findSec(String name){
return userMapper.findByNameSec(name);
}
}

七、创建controller文件,其中Get请求中,获取登录用户的角色,不同的角色调用Service中不同的函数,最终admin用户在数据库原始表中执行SQL查询,普通用户在脱敏后的视图中执行SQL查询

package Eleven.controller;

import Eleven.domain.User;
import Eleven.domain.UserInfo;
import Eleven.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserController {
@Autowired
private UserService userService; @GetMapping("/findByName")
public Object findByName(String name){
/**
* 获取登录用户的角色,不同角色调用Service中不同的函数,最终执行不同的Sql查询
*/
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth.getAuthorities().toString().equals("[ROLE_admin]")){
UserInfo userInfo = userService.find(name);
return userInfo;
}
else if (auth.getAuthorities().toString().equals("[ROLE_normal]")){
UserInfo userInfo = userService.findSec(name);
return userInfo;
}
else {
return auth.getAuthorities().toString();
} } }

八、测试验证

1、访问SpringBoot WEB,进入登录页面

2、使用admin用户登录后,访问查询用户信息接口,如下图所示,返回的是明文用户信息

3、使用user普通用户登录后访问,如下图所示,返回的是脱敏后的用户信息

利用数据库视图技术实现动态脱敏,适用于web应用,适用于生产环境,可按用户角色返回脱敏前后的查询结果。但是所有敏感数据表均需生成视图,额外占用数据库存储空间;为了实现目标,研发人员需要额外的开发工作量。

利用数据库视图实现WEB查询敏感信息接口动态脱敏的更多相关文章

  1. IdentityServer4源码解析_5_查询用户信息接口

    协议简析 UserInfo接口是OAuth2.0中规定的需要认证访问的接口,可以返回认证用户的声明信息.请求UserInfo接口需要使用通行令牌.响应报文通常是json数据格式,包含了一组claim键 ...

  2. SPRING IN ACTION 第4版笔记-第九章Securing web applications-011-把敏感信息请求转为https(requiresChannel())

    1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() @Overri ...

  3. jboss eap 6.2+ 版本中 加密datasource密码等敏感信息

    默认情况下,在jboss eap 6.2+ 管理控制台创建datasource后,会在standalone.xml(独立模式)或host.xml(域模式)中以明文保存相关敏感信息. 这会给服务器留下安 ...

  4. jboss加密敏感信息

    默认情况下,我们配置在domain.xml或host.xml文件中的信息都是明文,对一些敏感信息就显得安全性不够,可以使用jboss提供的vault机制来进行加密 下面的内容来自 http://www ...

  5. 如何利用GitHub搜索敏感信息

    如何利用GitHub搜索敏感信息 背景: 最近总是能听到同事说在GitHub上搜到某个敏感信息,然后利用该信息成功的检测并发现某个漏洞,最后提交到对应的SRC(安全应急响应中心)换点money.顿时心 ...

  6. Web应用程序的敏感信息-隐藏目录和文件

    Web应用程序的敏感信息-隐藏目录和文件 0x1.场景 Web应用程序根文件夹中可能存在大量隐藏信息:源代码版本系统文件夹和文件(.git,.gitignore,.svn),项目配置文件(.npmrc ...

  7. Web应用安全之Response Header里的敏感信息

    Web应用安全之Response Header 文/玄魂 目录 Web应用安全之Response Header 前言 1.1  那些敏感的header 1.2 删除敏感的header 1.2.1 删除 ...

  8. Python与数据库[2] -> 关系对象映射/ORM[5] -> 利用 sqlalchemy 实现关系表查询功能

    利用 sqlalchemy 实现关系表查询功能 下面的例子将完成一个通过关系表进行查询的功能,示例中的数据表均在MySQL中建立,建立过程可以使用 SQL 命令或编写 Python 适配器完成. 示例 ...

  9. MSSQL·查询数据库中所有索引的相关信息

    阅文时长 | 0.45分钟 字数统计 | 784字符 主要内容 | 1.引言&背景 2.声明与参考资料 『MSSQL·查询数据库中所有索引的相关信息』 编写人 | SCscHero 编写时间 ...

随机推荐

  1. CF1253F Cheap Robot(神奇思路,图论,最短路,最小生成树/Kruskal 重构树/并查集)

    神仙题. 先考虑平方级别的暴力怎么做. 明显答案有单调性,先二分 \(c\). 先最短路预处理 \(dis_u\) 表示 \(u\) 到离它最近的充电站的距离(一开始把 \(1\) 到 \(k\) 全 ...

  2. 不用输入ssh -i命令行即可携带pem文件快速登录的方法

    如果要登录的服务器只允许pem认证 每次输入ssh -i xxxx.pem 用户@ip 地址  就很烦 这里有个一劳永逸的方法: 进入到自己的用户目录,例如/home/me 把pem文件放在当前目录 ...

  3. 07-selenium、PhantomJS(无头浏览器)

    selenium(自动化测试工具可用于在爬虫中解决js动态加载问题) 简介(本质就是模仿浏览器工作) Selenium 是什么?一句话,自动化测试工具.它支持各种浏览器,包括 Chrome,Safar ...

  4. Java正则表达式验证IP,邮箱,电话

     引言     java中我们会常用一些判断如IP.电子邮箱.电话号码的是不是合法,那么我们怎么来判断呢,答案就是利用正则表达式来判断了,废话不多说,下面就是上代码. 1:判断是否是正确的IP  1 ...

  5. mysql常用命令杂记

    查看版本 mysqladmin -uRootmaster -pRootmaster@777 versionselect version() 查看Log_bin是否开启 show variables l ...

  6. js-xlsx 实现前端 Excel 导出(支持多 sheet)

    之前写文章介绍了使用 js-xlsx 实现导入 excel 的功能,现在再介绍一下如何使用 js-xlsx 进行 excel 导出. [实现步骤] 1. 首先安装依赖 npm install xlsx ...

  7. RandomAccessFile实现简易记事本工具操作

    package seday03; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Scann ...

  8. ASP.Net MVC 路由及路由调试工具RouteDebug

    一.路由规则 1.可以创建多条路由规则,每条路由的name属性不相同 2.路由规则有优先级,最上面的路由规则优先级越高 App_Start文件下的:RouteConfig.cs public stat ...

  9. 1-4-JS基础-条件判断

    第一种 1.if(条件成立){ 执行某件事} 2.if(条件成立){执行某件事}else{执行另外一件事 } 3.if(条件1成立){执行某件事}else if(条件2成立){执行某件事}else i ...

  10. Dynamics 365中计算字段与Now进行计算实体导入报错:You can't use Now(), which is of type DateTime, with the current function.

    微软动态CRM专家罗勇 ,回复338或者20190521可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me. 计算字段是从Dynamics CRM 2015 SP1版本开始推 ...