MYBATIS Intercepter 数据库加密
来源:互联网 发布:辛集淘宝运营培训学校 编辑:程序博客网 时间:2024/05/21 17:54
项目中遇到要对数据库加密,简单的方法是在构建实体类时调用加密算法加密后,调用insert插入数据库,但这样做比较繁琐,mybatis 的拦截器可以解决这个问题:代码如下:
@Intercepts({@Signature(type=Executor.class,method="update",args={MappedStatement.class,Object.class}),@Signature(type=Executor.class,method="query",args={MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class})})public class DBInterceptor implements Interceptor{ private final Logger logger = LoggerFactory.getLogger(DBInterceptor.class);@Overridepublic Object intercept(Invocation invocation) throws Throwable {MappedStatement statement = (MappedStatement) invocation.getArgs()[0];String methodName = invocation.getMethod().getName();Object parameter = invocation.getArgs()[1];BoundSql sql = statement.getBoundSql(parameter);logger.info("sql is {}",sql.getSql());if(parameter instanceof SystemUser){SystemUser user = (SystemUser)parameter;if(methodName.equals("query")){}else if(methodName.equals("update")){user.setName(DBSecurityUtil.getInstance().encrypt(user.getName()));user.setPassword(Digests.md5(user.getPassword().getBytes()));}}Object returnValue = invocation.proceed();if(returnValue instanceof ArrayList<?>){List<?> list = (ArrayList<?>)returnValue;for(Object val:list){if(val instanceof SystemUser){SystemUser user = (SystemUser)val;logger.debug("user name is {}",user.getName());user.setName(DBSecurityUtil.getInstance().decrypt(user.getName()));logger.debug("user name is {}",user.getName());}}}return returnValue;}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {// TODO Auto-generated method stub}}
spring 配置如下:
<!-- MyBatis配置 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --><property name="typeAliasesPackage" value="com.xxxx.entity" /><!-- 显式指定Mapper文件位置 --><property name="mapperLocations" value="classpath:/com/xxx/repository/mybatis/mapper/*Mapper.xml" /><!-- 配置Mybatis的插件plugin--> <property name="plugins"> <array> <bean class="com.xxxx.utils.security.DBInterceptor"> <property name="properties"> <value> property-key=property-value </value> </property> </bean> </array> </property></bean>
1 0
- MYBATIS Intercepter 数据库加密
- Java intercepter
- unable to load Intercepter
- Logging Intercepter例子
- struts2之Intercepter
- SpringMVC-intercepter配置
- 数据库加密
- 数据库加密
- 数据库加密
- 数据库加密
- 数据库加密
- SpringMVC-intercepter(拦截器)配置
- Struts2拦截器(Intercepter)
- Android 数据库加密 SQL加密
- MyBatis之Base64加密数据源
- sql 2005数据库加密
- java加密数据库字段
- 常用数据库加密技术
- [单调栈] poj 3250 Bad Hair Day
- 解决警告——warning C4018: “<”: 有符号/无符号不匹配
- 除去在浏览器中自动完成表单后input出现黄色背景
- POJ3522-Slim Span
- POJ 1511 Invitation Cards 邻接表 spfa算法
- MYBATIS Intercepter 数据库加密
- NYOJ45:过河问题【贪心】
- HDOJ 1513 Palindrome(LCS)
- EC11旋转编码器电路和程序
- cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- POJ 1258--Agri-Net【最小生成树 && Kruskal && 水题】
- PC110102(扫雷)(Minesweeper)
- CorePlot通过cocoaPod安装报错的解决方法
- 课后习题 poj 2376 区间贪心