Spring 弹簧靴&x2B;动态数据源
我有一种情况,我需要根据用户输入创建一个db连接,并且这个创建的连接应该用于所有事务,直到该特定用户注销为止。我需要在SpringBoot+JPA中执行此操作。默认情况下,我在服务器启动时从yaml文件获取连接详细信息 我不知道如何创建一个新的数据源并在整个会话中使用它 我不熟悉spring boot和JPA。有人能帮我举个例子吗。这不是一个快速解决方案,可能需要一天左右的时间才能正确实现。,但是使用它可以提供普通JDBC池的速度,但每个用户都有其特定的数据库权限。审核跟踪还将获取代理的用户 Spring登录用户用于确定代理哪个用户 您需要从下载两个依赖项。 确保版本相同。在Maven repo中安装它们Spring 弹簧靴&x2B;动态数据源,spring,spring-boot,jpa,Spring,Spring Boot,Jpa,我有一种情况,我需要根据用户输入创建一个db连接,并且这个创建的连接应该用于所有事务,直到该特定用户注销为止。我需要在SpringBoot+JPA中执行此操作。默认情况下,我在服务器启动时从yaml文件获取连接详细信息 我不知道如何创建一个新的数据源并在整个会话中使用它 我不熟悉spring boot和JPA。有人能帮我举个例子吗。这不是一个快速解决方案,可能需要一天左右的时间才能正确实现。,但是使用它可以提供普通JDBC池的速度,但每个用户都有其特定的数据库权限。审核跟踪还将获取代理的用户 S
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ucp</artifactId>
<version>12.2.0.1.0</version>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1.0</version>
<scope>provided</scope>
</dependency>
实施并理解成本的作用
import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import oracle.jdbc.OracleConnection;
import oracle.ucp.ConnectionLabelingCallback;
import oracle.ucp.jdbc.LabelableConnection;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
public class MyProxyConnectionLabelingCallback implements ConnectionLabelingCallback {
public static final String USER_PROXY = "USER_PROXY";
private final String noPrivilegesUser;
public ProxyConnectionLabelingCallback(String noPrivilegesUser) {
this.noPrivilegesUser = noPrivilegesUser;
}
@Override
public int cost(Properties reqLabels, Properties currentLabels) {
final Object request = reqLabels.get(USER_PROXY);
final Object current = currentLabels.get(USER_PROXY);
if (request.equals(current)) {
//Same user
return 0;
}
if (noPrivilegesUser.equals(current)) {
//No Priv User. No Little Cost
return 1;
}
//Different user request
return 1000;
}
@Override
public boolean configure(Properties reqLabels, Object conn) {
try {
LabelableConnection lConn = (LabelableConnection) conn;
String userName = getUserName(noPrivilegesUser);
final Properties connectionLabels = lConn.getConnectionLabels();
if (connectionLabels == null) {
setProxy((OracleConnection) conn, noPrivilegesUser, userName, null);
lConn.applyConnectionLabel(USER_PROXY, userName);
return true;
}
Object currentLabel = connectionLabels.get(USER_PROXY);
if (!userName.equals(currentLabel)) {
setProxy((OracleConnection) conn, currentLabel, userName, null);
lConn.applyConnectionLabel(USER_PROXY, userName);
} else {
//Label match, good
}
return true;
} catch (SQLException| RuntimeException sExp) {
return false;
}
}
public static String getUserName(String noPrivilegesUser) {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();// you are logged with this user using Spring Security
if ((authentication == null)) {
//Not logged in returning non privileged user
return noPrivilegesUser;
}
Object principal = authentication.getPrincipal();
if (principal.equals("anonymousUser")) {
//Not logged in returning non privileged user
return noPrivilegesUser;
}
if (principal instanceof UserDetails) {
final String username = ((UserDetails) (principal)).getUsername();
//proxy connection to user username
return username;
} else {
return principal.toString();
}
}
public void setProxy(OracleConnection oraCon, Object currentLabel, String proxyUserName, String proxyPassword) throws SQLException {
if (oraCon.isProxySession()) {
oraCon.close(OracleConnection.PROXY_SESSION);
}
Properties proxyProperties = new Properties();
proxyProperties.setProperty(OracleConnection.PROXY_USER_NAME, proxyUserName);
if (proxyPassword != null) {
proxyProperties.setProperty(OracleConnection.PROXY_USER_PASSWORD, proxyPassword);
}
//below is what can take a few ms
oraCon.openProxySession(OracleConnection.PROXYTYPE_USER_NAME, proxyProperties);
}
}
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import oracle.ucp.jdbc.PoolDataSource;
import org.springframework.jdbc.datasource.DelegatingDataSource;
class ProxyDelegatingDataSource extends DelegatingDataSource {
private final PoolDataSource pds;
private final String noPrivilegesUser;
public ProxyDelegatingDataSource(PoolDataSource pds, String noPrivilegesUser) {
super(pds);
this.pds = pds;
this.noPrivilegesUser = noPrivilegesUser;
}
@Override
public Connection getConnection() throws SQLException {
Properties prprts = new Properties();
prprts.put(ProxyConnectionLabelingCallback.USER_PROXY, getUserName());
return getConnection(prprts);
}
public Connection getConnection(Properties prprts) throws SQLException {
return pds.getConnection(prprts);
}
public Connection getConnection(String username, String password, Properties prprts) throws SQLException {
return pds.getConnection(username, password, prprts);
}
private String getUserName() {
return ProxyConnectionLabelingCallback.getUserName(noPrivilegesUser);
}
}
设置Oracle privs:
create user JDBCPROXY identified by pass123;
create user MY_NOPRIV identified by pass123;
GRANT CONNECT TO JDBCPROXY;
GRANT CONNECT TO MY_NOPRIV;
alter user MY_NOPRIV grant connect through JDBCPROXY;
alter user USER1 grant connect through JDBCPROXY;
创建池时,所有用户都将作为JDBCPROXY进行连接,代理为MY_NOPRIV。
当“USER1”登录时,它将切换到代理为USER1的JDBCPROXY
设置Spring安全性并以“USER1”身份登录。我不想讨论这个问题。当用户登录时,您是否正在尝试连接到同一个数据库?Oracle,您正在使用哪个数据库?您希望在用户登录时创建一个DB连接,并在用户注销之前使用同一连接。这是你的要求吗?您可以尝试@rjdkolb-我正在尝试连接到同一个数据库,在本例中是Oracle DB。因此,您可以使用Oracle代理用户。它将为您提供一个真正的池的性能,但每个用户都有他们对数据库的特定权限。审核跟踪还将获取代理的用户。
create user JDBCPROXY identified by pass123;
create user MY_NOPRIV identified by pass123;
GRANT CONNECT TO JDBCPROXY;
GRANT CONNECT TO MY_NOPRIV;
alter user MY_NOPRIV grant connect through JDBCPROXY;
alter user USER1 grant connect through JDBCPROXY;