gpt4 book ai didi

java - 我如何告诉我的数据库某些更改是由 hibernate 而不是其他应用程序进行的?

转载 作者:行者123 更新时间:2023-11-30 08:00:01 25 4
gpt4 key购买 nike

我是 hibernate 的新手,我遇到了这个问题。我使用 Oracle 数据库和 Hibernate+Maven+Netbeans。我的目的是只能使用我授权的应用程序对我的数据库进行更改。不可能从 SQL 控制台或其他程序进行更改。为此,我在数据库中创建了一个表:

CREATE TABLE DATA(
name char(30),
day integer,
month integer,
year integer );

像这样的触发器限制对我的数据库的任何访问:

  CREATE OR REPLACE TRIGGER tri_block
BEFORE INSERT OR UPDATE OR DELETE ON data
BEGIN
IF TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) < 12
OR TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) >= 12
OR TO_CHAR(SYSDATE,'dy') in ('sun','sat') THEN
RAISE_APPLICATION_ERROR (-20000, 'changes can not be made');
END IF;
END;

这是我的 HibernateUtil.java :

 package com.scooby.util;

import com.scooby.DATA;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure()
.addAnnotatedClass(DATA.class).buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}

}

我听说过一些有关 ORACLE 上下文的内容以及有关 hibernate 中 session 的内容。有人可以解释一下吗?

最佳答案

触发器可能不是防止未经授权的写入访问的好解决方案。标准解决方案是为您的应用程序创建一个新用户,然后向所有用户授予/撤销必要的 Oracle 权限,以确保只允许您的应用程序写入某些表。

如果您对这个解决方案不满意,还有sys_context solution ,这绝对比另一个不太安全。虽然它提供了更广泛的审计功能,但也需要在数据库端进行更多工作,以及从应用程序调用存储过程。

关于java - 我如何告诉我的数据库某些更改是由 hibernate 而不是其他应用程序进行的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32097023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com