-
Notifications
You must be signed in to change notification settings - Fork 168
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
fix: remove Lazy annotation from Flow security beans #18463
Changes from 1 commit
0277d4b
e619927
539b63a
0c4b416
124c117
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,10 +16,16 @@ | |||||||||
|
||||||||||
package com.vaadin.flow.spring; | ||||||||||
|
||||||||||
import java.io.ByteArrayInputStream; | ||||||||||
import java.io.ByteArrayOutputStream; | ||||||||||
import java.io.IOException; | ||||||||||
mcollovati marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
import java.io.ObjectInputStream; | ||||||||||
import java.io.ObjectOutputStream; | ||||||||||
import java.lang.reflect.Method; | ||||||||||
import java.security.Principal; | ||||||||||
import java.util.function.Function; | ||||||||||
|
||||||||||
import org.junit.jupiter.api.Assertions; | ||||||||||
import org.junit.jupiter.api.Test; | ||||||||||
import org.mockito.ArgumentMatchers; | ||||||||||
import org.mockito.Mockito; | ||||||||||
|
@@ -36,9 +42,11 @@ | |||||||||
import com.vaadin.flow.server.auth.AccessCheckDecision; | ||||||||||
import com.vaadin.flow.server.auth.AccessCheckResult; | ||||||||||
import com.vaadin.flow.server.auth.AccessPathChecker; | ||||||||||
import com.vaadin.flow.server.auth.AnnotatedViewAccessChecker; | ||||||||||
import com.vaadin.flow.server.auth.NavigationAccessChecker; | ||||||||||
import com.vaadin.flow.server.auth.NavigationAccessControl; | ||||||||||
import com.vaadin.flow.server.auth.NavigationContext; | ||||||||||
import com.vaadin.flow.server.auth.RoutePathAccessChecker; | ||||||||||
import com.vaadin.flow.spring.security.NavigationAccessControlConfigurer; | ||||||||||
import com.vaadin.flow.spring.security.SpringAccessPathChecker; | ||||||||||
import com.vaadin.flow.spring.security.SpringNavigationAccessControl; | ||||||||||
|
@@ -124,6 +132,29 @@ void customNavigationAccessCheckersConfigurerWithoutVaadinWebSecurityExtension() | |||||||||
.run(SpringSecurityAutoConfigurationTest::assertThatCustomNavigationAccessCheckerIsUsed); | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void securityBeansAreSerializable() { | ||||||||||
this.contextRunner.run((context) -> { | ||||||||||
|
||||||||||
// view access checker | ||||||||||
assertThat(context).getBean(AccessAnnotationChecker.class) | ||||||||||
.satisfies(this::assertObjectIsSerializable); | ||||||||||
|
||||||||||
assertThat(context).getBean(AnnotatedViewAccessChecker.class) | ||||||||||
.satisfies(this::assertObjectIsSerializable); | ||||||||||
|
||||||||||
assertThat(context).getBean(AccessPathChecker.class) | ||||||||||
.satisfies(this::assertObjectIsSerializable); | ||||||||||
|
||||||||||
assertThat(context).getBean(RoutePathAccessChecker.class) | ||||||||||
.satisfies(this::assertObjectIsSerializable); | ||||||||||
|
||||||||||
assertThat(context).getBean(NavigationAccessControl.class) | ||||||||||
.satisfies(this::assertObjectIsSerializable); | ||||||||||
}); | ||||||||||
|
||||||||||
} | ||||||||||
|
||||||||||
private static void assertThatCustomNavigationAccessCheckerIsUsed( | ||||||||||
AssertableWebApplicationContext context) { | ||||||||||
assertThat(context).hasSingleBean(NavigationAccessControl.class); | ||||||||||
|
@@ -151,11 +182,27 @@ private static void assertThatCustomNavigationAccessCheckerIsUsed( | |||||||||
Mockito.verify(navigationContext, Mockito.never()).neutral(); | ||||||||||
} | ||||||||||
|
||||||||||
private <T> void assertObjectIsSerializable(T instance) { | ||||||||||
Object deserialized = Assertions.assertDoesNotThrow(() -> { | ||||||||||
ByteArrayOutputStream bs = new ByteArrayOutputStream(); | ||||||||||
ObjectOutputStream out = new ObjectOutputStream(bs); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intellij like this version better:
Suggested change
Maybe we could use it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, Marco! |
||||||||||
out.writeObject(instance); | ||||||||||
byte[] data = bs.toByteArray(); | ||||||||||
ObjectInputStream in = new ObjectInputStream( | ||||||||||
new ByteArrayInputStream(data)); | ||||||||||
|
||||||||||
@SuppressWarnings("unchecked") | ||||||||||
T readObject = (T) in.readObject(); | ||||||||||
return readObject; | ||||||||||
}); | ||||||||||
Assertions.assertNotNull(deserialized, "Deserialized object is null"); | ||||||||||
} | ||||||||||
|
||||||||||
@TestConfiguration(proxyBeanMethods = false) | ||||||||||
static class CustomAccessPathChecker extends VaadinWebSecurity { | ||||||||||
|
||||||||||
@Bean | ||||||||||
AccessPathChecker customAccessPathChecker() { | ||||||||||
static AccessPathChecker customAccessPathChecker() { | ||||||||||
return DISABLED_PATH_CHECKER; | ||||||||||
} | ||||||||||
} | ||||||||||
|
@@ -164,7 +211,7 @@ AccessPathChecker customAccessPathChecker() { | |||||||||
static class CustomAccessAnnotationChecker extends VaadinWebSecurity { | ||||||||||
|
||||||||||
@Bean | ||||||||||
AccessAnnotationChecker accessAnnotationChecker() { | ||||||||||
static AccessAnnotationChecker accessAnnotationChecker() { | ||||||||||
return DISABLED_ANNOTATION_CHECKER; | ||||||||||
} | ||||||||||
} | ||||||||||
|
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.
Would it also make sense to add
proxyBeanMethods=false
to the@Configuration
so that all beans in that class aren't proxied? This would also ensure this isn't reintroduced by accident.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.
If I recall correctly,
proxyBeanMethods=false
prevents the configuration class to be proxied, not the exposed beans.Anyway, it makes sense to set that flag, since we have no direct method calls in
SpringSecurityAutoConfiguration
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 had the same assumption in the past :) until I've read the javadocs of the flag:
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 read it as "configuration class is proxied so that bean methods will always return the same instance when called by other methods of the class". My understanding is that when, for example, you call
annotatedViewAccessChecker()
from another method inside theSpringSecurityAutoConfiguration
class, it creates the instance at the first call, and subsequent invocation will return that one instead of a new instance how it would happen if the configuration class is not proxied.So,
SpringSecurityAutoConfiguration
methods are proxied, but not their return value.But I may be wrong. I'll double-check it
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.
Good thing to double check! I only remember an old issue where boot also switched all their configuration to false by default, also to increase performance: spring-projects/spring-boot#9068
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.
Before this is blocked: don't worry about it and do it later :)
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 added the flag anyway, since it completely makes sense to avoid proxying in this case.
Thanks for pointing out 👍