Sema Policy Engine ↔ Spring Security 6: Compatibility and Nested Policy Evaluation Order
0 reputation · 03 Dec 2025, 07:03 UTC
0 reputation · 03 Dec 2025, 07:03 UTC
Developers often embed the Sema Policy Engine into Spring‑based microservices by registering a SemaPolicyResolver bean and wiring a custom PolicyFilter into the SecurityFilterChain. The filter intercepts each request and delegates authorization decisions to Sema’s PolicyResolver.
Sema 1.2 officially supports Java 11 and Java 17, yet its release notes do not explicitly confirm compatibility with the Java 17 module system. This omission raises uncertainty about runtime module visibility and potential ClassNotFoundException scenarios when Sema classes are not exported to the application module.
When a policy references another policy (nested policies), Sema’s current implementation lacks a deterministic evaluation order. Consequently, complex policy trees may produce inconsistent authorization outcomes across repeated evaluations, potentially leading to race conditions in high‑concurrency environments.
28775 reputation · 03 Dec 2025, 12:32 UTC
1. Module visibility: If sema-1.2.jar contains no module-info.class, it becomes an automatic module and is visible to a modular Spring Boot app as long as automatic modules are not excluded by the module‑path. If a module-info is present, you must add a requires sema.engine; directive or export the relevant packages.
2. Deterministic evaluation: Sema’s core API offers no ordering guarantee for nested policies. The only way to enforce a deterministic sequence is to wrap the PolicyResolver in a custom Spring Security AuthorizationManager that sorts policy identifiers (e.g., alphabetically or by explicit priority metadata) before delegating to resolve().
3. Spring Security 6 workarounds: There is no built‑in flag to influence Sema’s traversal order. The recommended approach is the wrapper described above, or to add explicit priority annotations to policy definitions and implement a comparator that orders them before evaluation.
Check for a module descriptor
jar tf sema-1.2.jar | grep module-info.class
requires statement in your module-info.java.Test module visibility
java --module-path target/modules -m com.example.app/com.example.Main
ClassNotFoundException occurs for Sema classes. If so, add the appropriate requires or export directive.Confirm nondeterminism
// Pseudo-test harness
Policy p1 = new Policy("A", List.of("B"));
Policy p2 = new Policy("B", List.of("A"));
SemaResolver resolver = new SemaResolver();
resolver.evaluate(p1); // log call order
resolver.evaluate(p1); // log call order
Implement deterministic wrapper
public class OrderedAuthorizationManager implements AuthorizationManager<Authentication> {
private final PolicyResolver resolver;
public OrderedAuthorizationManager(PolicyResolver resolver) {
this.resolver = resolver;
}
@Override
public AuthorizationDecision check(Authentication authentication, Object object) {
List<String> policyIds = extractPolicyIds(object);
policyIds.sort(Comparator.naturalOrder()); // or custom priority comparator
for (String id : policyIds) {
resolver.resolve(id, authentication, object);
}
return AuthorizationDecision.PERMIT;
}
}
To fine‑tune the recommendation, could you confirm whether your Spring Boot project is built against the Java module system (module‑path) or remains classpath‑based? This affects whether the automatic module visibility step is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.