Skip to content

GH-6485: Add proxy support to the FTP session factories - #11535

Merged
artembilan merged 2 commits into
spring-projects:mainfrom
sharanggupta:GH-6485
Oct 15, 2026
Merged

artembilan merged 2 commits into
spring-projects:mainfrom
sharanggupta:GH-6485

Conversation

@sharanggupta

Copy link
Copy Markdown
Contributor

Fixes #6485

An FTP server behind a proxy could so far only be reached with the proxy system properties of the JVM (socksProxyHost and friends), which affect every connection of the application, or by writing a custom session factory. This adds the two options that Artem sketched on the issue.

What it adds

  • AbstractFtpSessionFactory#setProxy(java.net.Proxy), so DefaultFtpSessionFactory and DefaultFtpsSessionFactory connect through a SOCKS proxy, or through an HTTP proxy with the CONNECT tunneling of the JVM. The proxy is passed to FTPClient#setProxy(), which makes the client use it for the control connection and for the passive data connections. Because the server cannot connect back to the client through a proxy, the factory requires FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE when a proxy is set, and fails fast with a message that says so.
  • FtpHttpProxySessionFactory, based on FTPHTTPClient, for an HTTP proxy that requires credentials (sent with the basic scheme). It defaults to, and only accepts, the passive client mode. The name follows the Default*SessionFactory style; the issue mentioned FtpHttpSessionFactory, so please say if you prefer that one.
  • Reference documentation (a "Proxy Support" section in the FTP session factory chapter) and a What's New entry.

Testing

Developed test first: the new tests do not compile against main (the API does not exist), and pass with the change.

  • SessionFactoryTests: the proxy is applied to the client before connect(), the client is not proxied by default, and a proxy with the active client mode is rejected.
  • FtpHttpProxySessionFactoryTests: defaults and validation of the new factory.
  • FtpProxySessionFactoryTests: against the embedded Apache FtpServer of the module, through small in-test proxies (LocalProxyServer: a SOCKS5 proxy and an HTTP CONNECT proxy with optional basic authentication) that record the tunneled connections. The tests write, list and read a file through a SOCKS proxy, through an HTTP proxy with the JVM tunneling, and through an HTTP proxy with credentials, and assert that the control connection and the data connections all went through the proxy. Wrong proxy credentials fail the session creation with the proxy's 407 in the stack trace.
  • ./gradlew :spring-integration-ftp:check and ./gradlew antora pass locally.

Fixes: spring-projects#6485

An FTP (or FTPS) server behind a proxy could only be reached with the
proxy system properties of the JVM, which affect every connection of the
application, or with a custom session factory.

* Add the `proxy` property to the `AbstractFtpSessionFactory`, which
  applies a `java.net.Proxy` (SOCKS or HTTP) to the `FTPClient`, so it is
  used for the control connection and for the data connections
* Require the passive client mode when a proxy is used, because the server
  cannot connect back to the client through a proxy
* Add the `FtpHttpProxySessionFactory`, based on the `FTPHTTPClient`, for
  an HTTP proxy that requires credentials
* Test both against the embedded FTP server through in-test SOCKS5 and
  HTTP `CONNECT` proxies, which record the tunneled connections
* Document the options in the FTP session factory chapter

Signed-off-by: Sharang Gupta <[email protected]>

@artembilan artembilan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool!

Just couple nit-picks.

Thank you!

sessionFactory.setHost("localhost");
sessionFactory.setPort(port);
sessionFactory.setUsername("foo");
sessionFactory.setPassword("foo");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No foo/bar language in the code, please.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: the tests use ftpuser / ftppassword now, and the shared mock setup in the SessionFactoryTests moved to a helper method.

@artembilan

Copy link
Copy Markdown
Member

@sharanggupta ,

FYI, we have only couple days left in the current development cycle.
Otherwise, this would need to be postponed until we start 7.3 version in a couple month.

Thank you for understanding!

* Set the `clientMode` to the `FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE`
  in the `AbstractFtpSessionFactory.setProxy()`, since the server cannot
  connect back to the client through a proxy; the `getSession()` still
  fails when the mode is changed afterwards to another one
* Use descriptive credentials instead of the placeholder names in the tests

Signed-off-by: Sharang Gupta <[email protected]>
@sharanggupta

Copy link
Copy Markdown
Contributor Author

Thank you for the review and for the heads-up on the timeline, @artembilan. Both points are addressed in the new commit, and the spring-integration-ftp module check passes locally on JDK 25.

@artembilan
artembilan merged commit ed0fd1d into spring-projects:main Oct 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Proxy support to DefaultFtpSessionFactory [INT-2505]

2 participants