-
Notifications
You must be signed in to change notification settings - Fork 6k
Merge pull request #6901 from eleftherias/gh-6885-http-basic-dsl #6901
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
Merge pull request #6901 from eleftherias/gh-6885-http-basic-dsl #6901
Conversation
* @param <C> the configurer type | ||
*/ | ||
@FunctionalInterface | ||
public interface HttpConfigurerConsumer<C extends AbstractHttpConfigurer> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to use this vs using java.util.function.Consumer
?
NOTE: Much of the java configuration was written when we supported JDK 1.5+. Now that we require at least JDK 8 I think Consumer
makes sense. Perhaps you were following the patterns that existed in the older code is why you created this interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The java.util.function.Consumer
does't allow exceptions to be thrown from the accept
method.
In our case the realmName
method on HttpBasicConfigurer
throws an exception, so you would not be able to use that method in a lambda with the Java Consumer
.
I have seen a custom consumer in Spring Boot and I assume it is for the same reason.
https://github.com/spring-projects/spring-boot/blob/36c1c051b8106371d37f8a747cea0e369a8e5c84/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ContextConsumer.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pointer. That makes sense. A few thoughts:
- I think we should change the signature to allow throwing
Throwable
rather thanException
- I'm thinking we may want to make this more central (i.e. within core) since it is likely we will need to use this in other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rwinch I moved the consumer to the core module and renamed it. I also made the Throwable
a type argument so that it doesn't need to bubble up as a Throwable
in the calling methods.
* @return {@link HttpBasicConfigurer} for additional customization | ||
* @throws Exception | ||
*/ | ||
public HttpBasicConfigurer<B> withDefaults() throws Exception { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd prefer to revert and potentially use a generic noop Consumer. The reason is that withDefaults
is exposed for those not using lambda's and becomes confusing since the defaults are already applied by default. Another reason is that way we don't need a withDefaults for every configurer that we have.
I know we already talked about creating a method reference for users, but in hindsight I'd suggest we just require users to create an empty lambda. The JDK doesn't even provide one. We can always introduce a generic no op consumer later on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
I believe users could still do the following to keep the defaults.
http
.httpBasic(httpBasicConfigurer -> {});
4aeaff7
to
5f3cc01
Compare
491a6e2
to
986a4ca
Compare
* | ||
* @Override | ||
* protected void configure(HttpSecurity http) throws Exception { | ||
* http.authorizeRequests().antMatchers("/**").hasRole("USER").and().httpBasic(httpBasicConfig -> {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd fix this formatting to have newlines in it. I know there are other places we are missing newlines, but that occurred by mistake.
* } | ||
* | ||
* @Override | ||
* protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd just omit this from the configuration as we now recommend exposing a UserDetailsService via a Bean. This also allows the user to focus on this specific configuration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the Javadoc with both suggestions.
be1f362
to
0e25aef
Compare
*/ | ||
@FunctionalInterface | ||
public interface Customizer<T> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry this went in a full circle. However, now that we have a custom interface again, perhaps we can reintroduce something like
<T> Customizer<T> withDefaults() {
return t -> {};
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would need to be static
-- right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0e25aef
to
b32f7c1
Compare
b32f7c1
to
e1eaaa9
Compare
Thanks @eleftherias! This is now merged into master |
Issue: gh-5557
Fixes: gh-6885