sql语句拦截检查

拦截器

  • 对不含 where 的 update 语句进行拦截
/**
 * SQL 监听器
 * <p>
 * 对不含 where 的 update 语句进行拦截,防止批量误操作
 */
public class QueryFilter extends SqlExcuteListener {

	private static final long serialVersionUID = -3172666378145934837L;

	@Override
    public void renderEnd(ExecuteContext ctx) {
        super.renderEnd(ctx);
        if (ctx.sql().matches("^(?i:(UPDATE|DELETE)(?!.* WHERE ).*)$")) {
            throw new DeleteOrUpdateWithoutWhereException();
        }
    }

    private class DeleteOrUpdateWithoutWhereException extends RuntimeException {
		private static final long serialVersionUID = 1263216828793967446L;

		@Override
        public String getMessage() {
            return "Delete or update operation without where cannot be executed.";
        }
    }
}
发布了121 篇原创文章 · 获赞 3 · 访问量 4155

猜你喜欢

转载自blog.csdn.net/Q10CAU/article/details/105096719