SFTP Session Factory
从 3.0 版本开始,会话不再默认缓存。请参阅 SFTP Session Caching。
As of version 3.0, sessions are no longer cached by default. See SFTP Session Caching.
在配置 SFTP 适配器之前,您必须配置一个 SFTP 会话工厂。您可以使用常规 Bean 定义配置 SFTP 会话工厂,正如以下示例所示:
Before configuring SFTP adapters, you must configure an SFTP session factory. You can configure the SFTP session factory with a regular bean definition, as the following example shows:
<beans:bean id="sftpSessionFactory"
class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<beans:property name="host" value="localhost"/>
<beans:property name="privateKey" value="classpath:META-INF/keys/sftpTest"/>
<beans:property name="privateKeyPassphrase" value="springIntegration"/>
<beans:property name="port" value="22"/>
<beans:property name="user" value="kermit"/>
</beans:bean>
每次适配器从其 SessionFactory
请求一个会话对象时,都会创建一个新的 SFTP 会话。在底层中,SFTP 会话工厂依赖于 Apache MINA SSHD 库来提供 SFTP 功能。
Every time an adapter requests a session object from its SessionFactory
, a new SFTP session is created.
Under the covers, the SFTP Session Factory relies on the Apache MINA SSHD library to provide the SFTP capabilities.
但是,Spring Integration 还支持对 SFTP 会话进行缓存。请参阅 SFTP Session Caching了解更多信息。
However, Spring Integration also supports the caching of SFTP sessions. See SFTP Session Caching for more information.
|
The |
SshClient
支持通过与服务器的连接进行多个频道(操作)。默认情况下,Spring 集成会话工厂为每个频道使用一个单独的物理连接。从 Spring 集成 3.0 开始,您可以配置会话工厂(使用布尔构造函数参数 - 默认 false
)以使用到服务器的单个连接,并在该单个连接上创建多个 SftpClient
实例。
The SshClient
supports multiple channels (operations) over a connection to the server.
By default, the Spring Integration session factory uses a separate physical connection for each channel.
Since Spring Integration 3.0, you can configure the session factory (using a boolean constructor arg - default false
) to use a single connection to the server and create multiple SftpClient
instances on that single connection.
在使用此功能时,必须使用缓存会话工厂来封装会话工厂(如 described later),这样在某个操作完成时,不会物理关闭连接。
When using this feature, you must wrap the session factory in a caching session factory, as described later, so that the connection is not physically closed when an operation completes.
如果重置了高速缓存,则只会在关闭最后一个频道时断开会话。
If the cache is reset, the session is disconnected only when the last channel is closed.
当新操作获得会话时,如果连接要断开,则该连接将刷新。
The connection is refreshed if it to be disconnected when a new operation obtains a session.
现在,您只需将此 SFTP 会话工厂注入到适配器中即可。
Now all you need to do is inject this SFTP session factory into your adapters.
为 SFTP 会话工厂提供值的更实际的方式是使用 Spring 的 property placeholder support。 |
A more practical way to provide values for the SFTP session factory is to use Spring’s property placeholder support. |
从版本 6.1.3 开始,DefaultSftpSessionFactory
引入了 createSftpClient(…)
来支持自定义 SftpClient
。请参见下面在自定义 SftpClient
中覆盖 createSftpChannelSubsystem()
方法的示例,以便添加自定义 RequestHandler
以处理 SFTP 子系统请求和答复:
Starting with version 6.1.3, the DefaultSftpSessionFactory
introduces a createSftpClient(…)
to support a custom SftpClient
.
See a sample below of how to override createSftpChannelSubsystem()
method in your custom SftpClient
to add, for example, some custom RequestHandler
for SFTP sub-system requests and replies:
@Override
protected ChannelSubsystem createSftpChannelSubsystem(ClientSession clientSession) {
ChannelSubsystem sftpChannelSubsystem = super.createSftpChannelSubsystem(clientSession);
sftpChannelSubsystem.addRequestHandler((channel, request, wantReply, buffer) -> ...);
return sftpChannelSubsystem;
}
Configuration Properties
以下列表描述了 DefaultSftpSessionFactory
公开的所有属性。
The following list describes all the properties that are exposed by the DefaultSftpSessionFactory
.
isSharedSession
(构造函数参数):如果为 true
,则对所有请求的 SftpSession
实例都将使用单个 SftpClient
。默认为 false
。
isSharedSession
(constructor argument)::When true
, a single SftpClient
is used for all the requested SftpSession
instances.
It defaults to false
.
sftpVersionSelector
:用于 SFTP 协议选择的 SftpVersionSelector
实例。默认值为 SftpVersionSelector.CURRENT
。
sftpVersionSelector
::An SftpVersionSelector
instance for SFTP protocol selection.
The default one is SftpVersionSelector.CURRENT
.
host
:要连接到的主机的 URL。必须指定。
host
::The URL of the host to which to connect.
Required.
hostConfig
:作为用户/主机/端口选项的替代方案,org.apache.sshd.client.config.hosts.HostConfigEntry
实例。可以使用代理跳转属性进行配置。
hostConfig
::An org.apache.sshd.client.config.hosts.HostConfigEntry
instance as an alternative for the user/host/port options.
Can be configured with a proxy jump property.
port
:要建立 SFTP 连接的端口。如果未指定,则此值默认为 22
。如果指定,则此属性必须为整数。
port
::The port over which the SFTP connection shall be established.
If not specified, this value defaults to 22
.
If specified, this properties must be a positive number.
user
:要使用的远程用户。必须指定。
user
::The remote user to use.
Required.
knownHostsResource
:用于主机密钥存储库的 org.springframework.core.io.Resource
。资源的内容必须与 OpenSSH known_hosts
文件的格式相同,并且如果 allowUnknownKeys
为假,则必须预先填充。
knownHostsResource
::An org.springframework.core.io.Resource
that used for a host key repository.
The content of the resource has to be the same format as OpenSSH known_hosts
file and is required and must be pre-populated if allowUnknownKeys
is false.
password
:用于对远程主机进行身份验证的密码。如果未提供密码,则需要 privateKey
属性。
password
::The password to authenticate against the remote host.
If a password is not provided, then the privateKey
property is required.
privateKey
:一个 org.springframework.core.io.Resource
,表示用于对远程主机进行身份验证的私钥的位置。如果未提供 privateKey
,则需要 password
属性。
privateKey
::An org.springframework.core.io.Resource
that represents the location of the private key used for authenticating against the remote host.
If the privateKey
is not provided, then the password
property is required.
privateKeyPassphrase
:私钥的密码。如果设置了 userInfo
,则不允许使用 privateKeyPassphrase
。密码将从该对象中获取。可选。
privateKeyPassphrase
::The password for the private key.
If you set userInfo
, privateKeyPassphrase
is not allowed .
The passphrase is obtained from that object.
Optional.
timeout
:超时属性用作套接字超时参数,也是默认连接超时。默认为 30 秒
。设置为 0
表示没有超时;设置为 null
表示无限等待。
timeout
::The timeout property is used as the socket timeout parameter, as well as the default connection timeout.
Defaults to 30 seconds
.
Setting to 0
means no timeout; to null
- infinite wait.
allowUnknownKeys
:设置为 true
以允许连接到具有未知(或已更改)密钥的主机。其默认值为“false”。如果为 false
,则需要一个预填充的 knownHosts
文件。
allowUnknownKeys
::Set to true
to allow connections to hosts with unknown (or changed) keys.
Its default is 'false'.
If false
, a pre-populated knownHosts
file is required.
userInteraction
:一个自定义的 org.apache.sshd.client.auth.keyboard.UserInteraction
,用于在身份验证期间使用。
userInteraction
::A custom org.apache.sshd.client.auth.keyboard.UserInteraction
to be used during authentication.