-
Notifications
You must be signed in to change notification settings - Fork 6k
Make it easier to create a WebExpressionAuthorizationManager with a custom expressionHandler #12359
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
Comments
Hi, @ghusta, for the suggestion. Spring Security prefers to have optional fields as setters and required fields in the constructor. Further, this class is intended to give folks who wish to continue using SpEL authorization expressions a way to do so, even as the codebase migrates away from it. Given these two considerations, I'm not inclined to make configuring a The recommended path at this point is that filter-based authorization rules get evaluated programmatically. I understand you may have legacy reasons to stick with SpEL authorization rules; however, I'd be happy to look into this with you and help you migrate away from that. Can you explain what you need a custom expression handler for? |
Hi @jzheaux Thank you for giving some details about the orientation of Spring Security. I understand your point of view. I'll take some time to give you more context about my current use case, after the Christmas holidays. Best regards |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue. |
Hi @jzheaux. I'd be interested if you have any additional documentation or samples available for moving away from large scale SpEL expressions to a filter-based authorization rule. I think my app is in the same boat as @ghusta .. i have a large number of endpoints all secured via SpEL expressions and approaching this with the WebExpressionAuthorizationManager seems like I'd have to clutter the code up even more with a unique WebExpressionAuthorizationManager for each request. for example I have a setup such as this:
Based on the current WebExpressionAuthorizationManager constructor it seems I'd have to construct a new WebExpressionAuthorizationManager for each URI I'm trying to secure, where as in the past I could just do this:
|
Thanks for reaching out @JohnZ1385. My recommendation is that you move to programmatic authorization. In your case, you'd do: @Component("authz")
public class RequestAuthorizationManagerFactory {
private final WebAuthorizationService service;
public AuthorizationManager<RequestAuthorizationContext> isChangePasswordEnabled() {
return (authentication, context) -> new AuthorizationDecision(this.service.isChangePasswordEnabled());
}
// ...
public AuthorizationManager<RequestAuthorizationContext> canViewItem(String item) {
return (authentication, context) -> new AuthorizationDecision(this.service.canViewItem(context.getRequest(), item));
}
} And then declare your rules like so: import static org.springframework.security.authorization.AuthorizationManagers.anyOf;
.requestMatchers("/changepassword").access(anyOf(authz.isChangePasswordEnabled(), authz.userMustChangePassword())) Alternatively, you could achieve something closer to what you have now using the same pattern: @Component("authz")
public class RequestAuthorizationManagerFactory {
private final WebSecurityExpressionHandler expressionHandler;
public AuthorizationManager<RequestAuthorizationContext) spel(String expression) {
WebExpressionAuthorizationManager manager = new WebExpressionAuthorizationManager(expression);
manager.setExpressionHandler(expressionHandler);
return manager;
}
} And then change to: .requestMatchers("/changepassword").access(authz.spel("isChangePasswordEnabled() or userMustChangePassword()")) |
that's not so bad, thanks for your help @jzheaux |
It's not easy to customize
WebExpressionAuthorizationManager
with a custom expressionHandler, when defining the web security.For example if we want to secure multiple uri in Spring Security 5.8 we have to do something like this :
It would be easier to have either a new
WebExpressionAuthorizationManager
constructor with this signature :Or a factory method like :
References
The text was updated successfully, but these errors were encountered: