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

Document How to Handle Method Security in Native Image #13226

Closed
asavov opened this issue May 25, 2023 · 0 comments
Closed

Document How to Handle Method Security in Native Image #13226

asavov opened this issue May 25, 2023 · 0 comments
Assignees
Labels
in: docs An issue in Documentation or samples type: enhancement A general enhancement
Milestone

Comments

@asavov
Copy link

asavov commented May 25, 2023

Expected Behavior

Security SPEL (such as @PreAuthozied) needs custom native-image hints (such as @RegisterReflectionForBinding) when using/referring custom beans by SPEL.

The expectation is to enhance the doc, providing guidelines to consumers of SPEL support within Security.

Current Behavior

Security SPEL (such as @PreAuthozied) works out-of-the-box only for default beans managed by Spring framework.

Context

The problem is reproduced with this simplified snippet run within @jlong’s spring-boot-3-aot project as repro env.

@Configuration
@EnableMethodSecurity(prePostEnabled = true)

// It works in native-image if this line is uncommented
// @RegisterReflectionForBinding(CustomAuthenticationImpl.class)

public class SecurityConfiguration {

	public interface CustomAuthentication extends Authentication {

		boolean isCloudAdmin();
	}

	public static class CustomAuthenticationImpl extends TestingAuthenticationToken implements CustomAuthentication {

		static CustomAuthentication CLOUD_ADMIN = new CustomAuthenticationImpl(true);

		static CustomAuthentication NOT_CLOUD_ADMIN = new CustomAuthenticationImpl(false);

		private final boolean isCloudAdmin;

		private CustomAuthenticationImpl(boolean isCA) {
			super("alex-principle-" + (isCA ? "CA" : "nonCA"), "alex-credentials");
			this.isCloudAdmin = isCA;
		}

		@Override
		public boolean isCloudAdmin() {
			return isCloudAdmin;
		}

	}

	@Service
	public static class MyService {

		public String basicMethod() {
			return "OK";
		}

		// Use method on our CustomAuthentication which seems to need Hint
		@PreAuthorize("authentication.isCloudAdmin()")
		public String isCloudAdminMethod() {
			return "OK";
		}

	}

	@Bean
	ApplicationListener<ApplicationReadyEvent> withSecurity(MyService myService) {
		return event -> {
			SecurityContextHolder.getContext().setAuthentication(CustomAuthenticationImpl.NOT_CLOUD_ADMIN);

			System.out.println("[Security] basicMethod with NOT_CLOUD_ADMIN: " + myService.basicMethod());

			try {
				myService.isCloudAdminMethod();

				System.out.println("[Security] isCloudAdminMethod with NOT_CLOUD_ADMIN: should not happen");
			}
			catch (Exception e) {
				System.out.println("[Security] isCloudAdminMethod with NOT_CLOUD_ADMIN: was secured");
			}

			SecurityContextHolder.getContext().setAuthentication(CustomAuthenticationImpl.CLOUD_ADMIN);

			System.out.println("[Security] isCloudAdminMethod with CLOUD_ADMIN: " + myService.isCloudAdminMethod());
		};
	}

}

which works on JRE and fails on native-image with:

java.lang.IllegalArgumentException: Failed to evaluate expression 'authentication.isCloudAdmin()'
        at org.springframework.security.access.expression.ExpressionUtils.evaluateAsBoolean(ExpressionUtils.java:33) ~[na:na]
        at org.springframework.security.authorization.method.PreAuthorizeAuthorizationManager.check(PreAuthorizeAuthorizationManager.java:68) ~[na:na]
at Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method isCloudAdmin() cannot be found on type com.example.aot.security.SecurityConfiguration$CustomAuthenticationImpl
        at org.springframework.expression.spel.ast.MethodReference.findAccessorForMethod(MethodReference.java:225) ~[na:na]

Here's link to the slack channel discussion.

@asavov asavov added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels May 25, 2023
@marcusdacoregio marcusdacoregio self-assigned this May 25, 2023
@marcusdacoregio marcusdacoregio added in: core An issue in spring-security-core and removed status: waiting-for-triage An issue we've not yet triaged labels May 25, 2023
@marcusdacoregio marcusdacoregio added in: docs An issue in Documentation or samples and removed in: core An issue in spring-security-core labels May 26, 2023
@marcusdacoregio marcusdacoregio added this to the 6.0.4 milestone May 26, 2023
@marcusdacoregio marcusdacoregio changed the title Enhance doc w.r.t. AOT/native-image when using custom beans by the SPEL Document How to Handle Method Security in Native Image May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: docs An issue in Documentation or samples type: enhancement A general enhancement
Projects
Status: Done
Development

No branches or pull requests

2 participants