Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apache HttpClient 5.0 followup #1357

Closed

Conversation

cachescrubber
Copy link
Contributor

@cachescrubber cachescrubber commented May 15, 2023

@gregturn Thanks for the quick merge of #1356. There are some issued we need to address. I already spotted a Bug in HttpComponents5MessageSender#afterPropertiesSet. This is fixed in this MR.

In my original MR I noted incompatibilities in the HttpClient Api regarding configuring Authentication on the HttpClient.
Previously, AuthScope has been AuthScope.ANY which is removed in httpclient 5.x. So basically this way to configure preemptive http authentication will no longer work as before (or at all, not sure). At least I updated the Javadoc to reflect the change.

Another Idea would be to remove credentials and authScope altogether and guide users to handle authentication in a client interceptor. WDYT?

@cachescrubber
Copy link
Contributor Author

caused by #1164

Copy link
Contributor

@gregturn gregturn left a comment

Choose a reason for hiding this comment

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

We aren't adopting this plugin. Essentially, I am using https://github.com/spring-projects/spring-data-build/tree/main/etc/ide as the basis for any/all polishing going forwarda.

Copy link
Contributor

@gregturn gregturn left a comment

Choose a reason for hiding this comment

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

On top of the specific comments, in general we try to keep polishing commits separate from actual coded commits. It makes it easier to sort through real changes while still making format changes that meets our standards.

pom.xml Outdated
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>0.0.38</version>
</plugin>
Copy link
Contributor

Choose a reason for hiding this comment

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

We aren't adopting this plugin. Essentially, I am using https://github.com/spring-projects/spring-data-build/tree/main/etc/ide as the basis for any/all polishing going forward.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No offense, I just accidentally committed the pom ...

@gregturn
Copy link
Contributor

Another Idea would be to remove credentials and authScope altogether and guide users to handle authentication in a client interceptor. WDYT?

I would favor this approach. If we can make security a separate task, I think that would simplify this part.

@cachescrubber
Copy link
Contributor Author

How to proceed? Should we supply/sketch an Interceptor as a documentation snippet? Oder provide actual Code?

public class BasicAuthClientInterceptor extends ClientInterceptorAdapter {
	private final String credentials;

	public BasicAuthClientInterceptor(String username, String password) {
		this.credentials = basicAuth(username, password);
	}

	private static String basicAuth(String username, String password) {
		return Base64.getEncoder().encodeToString(String.format("%s:%s", username, password).getBytes());
	}

	@Override
	public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
		try {
			TransportContext context = TransportContextHolder.getTransportContext();
			if (context.getConnection() instanceof AbstractHttpSenderConnection httpSenderConnection) {
				httpSenderConnection.addRequestHeader("Authorization", "Basic " + credentials);
			} else {
				logger.warn("web service connection is not http; back-off");
			}
		} catch (IOException e) {
			throw new WebServiceIOException("BasicAuthHeaderInterceptor " + e.getMessage(), e);
		}
		return true;
	}
}

@gregturn
Copy link
Contributor

gregturn commented May 16, 2023

Instead of coming up with something more to maintain, I'd rather point them toward https://docs.spring.io/spring-ws/docs/current/reference/html/#security-wss4j-security-interceptor as well as https://github.com/spring-projects/spring-ws-samples/blob/main/airline/client/spring-ws/src/main/java/org/springframework/ws/samples/airline/client/sws/WsConfiguration.java#L51, an example we already maintain, that illustrates how to secure things using Wss4j. (And...I'd rather use existing interceptors so we don't hand roll it).

@cachescrubber
Copy link
Contributor Author

cachescrubber commented May 17, 2023

Full blown Wss is a different kind of story IMO. I was thinking about this section in the client documentation: how to use Apache HfttpClient to authenticate with HTTP authentication. This might no longer work as documented using HttpClient 5. So unfortunately spring-ws would loose the ability to do simple http basic auth. If you don't want to add new code, let's just add a snippet to the documentation or add it to some example project or even release notes.

@cachescrubber
Copy link
Contributor Author

cachescrubber commented May 17, 2023

I just checked the AuthScope source code and api doc. Another possibility would be to set the default AuthScope in HttpComponents5ClientFactory to new AuthScope(null, null, -1, null, null ); As it seems, they removed the static ANY instance, but functionality is still there and documented. We would then have a drop in replacement for HttpComponentsMessageSender.

@cachescrubber
Copy link
Contributor Author

My last commit redefines AuthScope.ANY in HttpComponents5ClientFactory. Probably means least effort overall. WDYT?

@gregturn
Copy link
Contributor

I like that approach over simply using a null.

Only thing I'd suggest is including a little extra in the JavaDocs explaining that HttpClient dropped ANY and that this value object is a flyweight replacement.

@gregturn
Copy link
Contributor

Regarding docs, I think a simple example of using it for Basic Auth in the client-side is fine.

@cachescrubber
Copy link
Contributor Author

I pushed a draft.

@gregturn gregturn closed this in 24b466a May 23, 2023
gregturn added a commit that referenced this pull request May 23, 2023
@gregturn
Copy link
Contributor

Thanks @cachescrubber.

I squashed your changes, polished, and merged to main.

@gregturn gregturn added this to the 4.0.5 milestone May 23, 2023
kmccarp pushed a commit to kmccarp/spring-projects__spring-ws that referenced this pull request Jun 5, 2023
* The clientFactory could be null, so this must be handled.
* AuthScope.ANY no longer exists, so we need a suitable value object to take its place.
* Polish documentation and javadocs to support the community.

Resolves spring-projects#1357.
kmccarp pushed a commit to kmccarp/spring-projects__spring-ws that referenced this pull request Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants