Destiny.Lin
4 months ago
9 changed files with 162 additions and 104 deletions
@ -0,0 +1,124 @@
|
||||
package com.fr.design.data.datapane.preview; |
||||
|
||||
import com.fr.data.auth.AuthenticationType; |
||||
import com.fr.data.auth.kerberos.KerberosAuthentication; |
||||
import com.fr.data.auth.kerberos.KerberosUtils; |
||||
import com.fr.data.impl.Connection; |
||||
import com.fr.data.impl.JDBCDatabaseConnection; |
||||
import com.fr.data.pool.DBCPConnectionPoolAttr; |
||||
import com.fr.data.security.ssh.BaseSsh; |
||||
import com.fr.data.security.ssh.SshType; |
||||
import com.fr.data.security.ssh.impl.KeyVerifySsh; |
||||
import com.fr.data.security.ssl.BaseSsl; |
||||
import com.fr.data.security.ssl.SslType; |
||||
import com.fr.data.security.ssl.impl.NormalSsl; |
||||
import com.fr.decision.privilege.TransmissionTool; |
||||
import com.fr.decision.webservice.bean.datasource.ConnectionInfoBean; |
||||
import com.fr.decision.webservice.bean.datasource.JDBCConnectionBean; |
||||
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
||||
import com.fr.decision.webservice.v10.datasource.connection.processor.impl.ConnectionProcessorFactory; |
||||
import com.fr.decision.webservice.v10.datasource.connection.processor.impl.JDBCConnectionProcessor; |
||||
import com.fr.security.encryption.transmission.TransmissionEncryptors; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.third.fasterxml.jackson.databind.ObjectMapper; |
||||
|
||||
/** |
||||
* 数据连接传输工具类 |
||||
* |
||||
* @author Destiny.Lin |
||||
* @since 11.0 |
||||
* Created on 2024/7/25 |
||||
*/ |
||||
public class ConnectionInfoBeanHelper { |
||||
private static ObjectMapper objectMapper = new ObjectMapper(); |
||||
|
||||
/** |
||||
* 创建数据连接Bean,可自定义是否携带密码 |
||||
*/ |
||||
public static ConnectionInfoBean createConnectionInfoBean(String name, Connection connection, boolean withPassword) throws Exception { |
||||
if (JDBCConnectionProcessor.KEY.acceptConnections().contains(connection.getClass())) { |
||||
ConnectionInfoBean bean = new ConnectionInfoBean(); |
||||
bean.setConnectionData(objectMapper.writeValueAsString(convertToJDBCConnectionBean(connection, withPassword))); |
||||
bean.setConnectionType(JDBCConnectionProcessor.CONNECTION_TYPE); |
||||
bean.setConnectionType(JDBCConnectionProcessor.KEY.getConnectionType(connection)); |
||||
bean.setConnectionName(name); |
||||
bean.setCreator(connection.getCreator()); |
||||
return bean; |
||||
} else { |
||||
return ConnectionProcessorFactory.createConnectionInfoBean(name, connection); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 创建数据连接Bean,默认携带密码 |
||||
*/ |
||||
public static ConnectionInfoBean createConnectionInfoBean(String name, Connection connection) throws Exception { |
||||
return createConnectionInfoBean(name, connection, true); |
||||
} |
||||
|
||||
/** |
||||
* 创建数据连接bean |
||||
*/ |
||||
public static ConnectionInfoBean createConnectionInfoBean(Connection connection) throws Exception { |
||||
return createConnectionInfoBean(connection.getConnectionName(), connection, true); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* jdbc的特殊处理 |
||||
*/ |
||||
private static JDBCConnectionBean convertToJDBCConnectionBean(Connection connection, boolean withPassword) { |
||||
JDBCDatabaseConnection jdbcConnection = (JDBCDatabaseConnection) connection; |
||||
JDBCConnectionBean jdbcConnectionBean = new JDBCConnectionBean(); |
||||
DBCPConnectionPoolAttr poolAttr = new DBCPConnectionPoolAttr(); |
||||
try { |
||||
poolAttr = (DBCPConnectionPoolAttr) jdbcConnection.getDbcpAttr().clone(); |
||||
} catch (CloneNotSupportedException ignore) { |
||||
} |
||||
poolAttr.setValidationQuery(TransmissionTool.encrypt(poolAttr.getValidationQuery())); |
||||
KerberosAuthentication kerberosAuthentication = null; |
||||
if (jdbcConnection.getAuthentication().type() == AuthenticationType.KERBEROS) { |
||||
//获取数据连接信息的时候,做一下兼容处理(兼容旧的kerberos形式)
|
||||
KerberosUtils.compatibilityProcess(((KerberosAuthentication) jdbcConnection.getAuthentication())); |
||||
kerberosAuthentication = ((KerberosAuthentication) jdbcConnection.getAuthentication()); |
||||
} |
||||
//因为设计器创建连接时database属性为空,所以这里直接取数据库保存的fetchSize值,默认值为-1
|
||||
return jdbcConnectionBean |
||||
.newCharsetName(jdbcConnection.getNewCharsetName()) |
||||
.originalCharsetName(jdbcConnection.getOriginalCharsetName()) |
||||
.database(jdbcConnection.getDatabase()) |
||||
.user(jdbcConnection.getUser()) |
||||
.driver(jdbcConnection.getDriver()) |
||||
// 关键中的关键,由设计器发出的请求如果要携带密码,不能传明文,必须必须必须传加密后的密码
|
||||
.password(withPassword ? TransmissionEncryptors.getInstance().encrypt(jdbcConnection.getPassword()) : DecisionServiceConstants.DEFAULT_PASSWORD) |
||||
.schema(jdbcConnection.getSchema()) |
||||
.url(jdbcConnection.getURL()) |
||||
.creator(jdbcConnection.getCreator()) |
||||
.source(jdbcConnection.getDriverSource()) |
||||
.connectionPoolAttr(poolAttr.create()) |
||||
.authType(kerberosAuthentication != null ? "kerberos" : StringUtils.EMPTY) |
||||
.principal(kerberosAuthentication != null ? kerberosAuthentication.getPrincipal() : StringUtils.EMPTY) |
||||
.keyPath(kerberosAuthentication != null ? kerberosAuthentication.getKeyPath() : StringUtils.EMPTY) |
||||
.krb5Path(kerberosAuthentication != null ? KerberosUtils.getKrb5Path(kerberosAuthentication.getKeyPath(), kerberosAuthentication.getPrincipal()) : StringUtils.EMPTY) |
||||
.useJaas(jdbcConnection.getAuthentication().type() == AuthenticationType.KERBEROS && ((KerberosAuthentication) jdbcConnection.getAuthentication()).getUseJaas()) |
||||
.fetchSize(jdbcConnection.getFetchSize()).identity(connection.getIdentity()) |
||||
.sshType(jdbcConnection.getSsh().getSshType().toString()) |
||||
.sshIp(((BaseSsh) jdbcConnection.getSsh()).getIp()) |
||||
.usingSsh(jdbcConnection.getSsh().isUsingSsh()) |
||||
.sshUser(((BaseSsh) jdbcConnection.getSsh()).getUser()) |
||||
.sshPort(((BaseSsh) jdbcConnection.getSsh()).getPort()) |
||||
.redirectPort(jdbcConnection.getSsh().getRedirectPort()) |
||||
.redirectIp((jdbcConnection.getSsh()).getRedirectIp()) |
||||
.sshTimeOut(((BaseSsh) jdbcConnection.getSsh()).getTimeOut()) |
||||
.sshSecret(withPassword ? ((BaseSsh) jdbcConnection.getSsh()).getSecret() : DecisionServiceConstants.DEFAULT_PASSWORD) |
||||
.sshPrivateKeyPath(jdbcConnection.getSsh().getSshType() == SshType.KEY ? ((KeyVerifySsh) jdbcConnection.getSsh()).getPrivateKeyPath() : StringUtils.EMPTY) |
||||
.usingSsl((jdbcConnection.getSsl()).isUsingSsl()) |
||||
.sslType(jdbcConnection.getSsl().getSslType().toString()) |
||||
.sslClientCertificate(((BaseSsl) jdbcConnection.getSsl()).getClientCertificate()) |
||||
.sslClientPrivateKey(((BaseSsl) jdbcConnection.getSsl()).getClientPrivateKey()) |
||||
.caCertificate(((BaseSsl) jdbcConnection.getSsl()).getCaCertificate()) |
||||
.properties(jdbcConnection.getProperties()) |
||||
.verifyCa(jdbcConnection.getSsl().getSslType() == SslType.NORMAL && ((NormalSsl) jdbcConnection.getSsl()).isVerifyCa()); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue