-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Some enhancement suggestions for new Authorization architecture classes for customizing the authorization logic #11024
Comments
Thanks for the analysis and suggestions, @fatihdogmus, I'm glad that you are trying out the new Given that the support is quite new, we anticipate enhancements being needed. That said, I'm a little confused about your usage of method security support, and I want to make sure I'm clear before we try and figure out how to change things. First, why are you trying to look at request headers during method invocation instead of in the filter chain? Second, do these headers affect both authentication and authorization? Maybe a sample request and its interpretation would help me understand better. |
In the meantime, I think I can simplify the code you have written. Have you already seen this sample in the documentation (slightly altered for your situation): @Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public Advisor preAuthorize(InternalPreAuthorizeAuthorizationManager rules) {
AnnotationMethodMatcher pattern = new AnnotationMethodMatcher(PreAuthorize.class);
AuthorizationManagerBeforeMethodInterceptor interceptor =
new AuthorizationManagerBeforeMethodInterceptor(pattern, rules);
interceptor.setOrder(AuthorizationInterceptorsOrder.PRE_AUTHORIZE.getOrder());
return interceptor;
} You can also take a look at |
Hello, Thanks for taking your time for answering, appreciate all the work you guys are doing on spring security side. Sorry that I couldn't write earlier, I was on site mission and couldn't access my PC.
We mostly use @PreAuthorize to secure our endpoints. AFAIU, authorization checks for annotations don't happen in filters and are executed via the mentioned interceptor, via AOP. That is why I tried to override the base interceptor. I don't know if that is completely true though, since I'm not very familiar with the new architecture.
Yes they do. Basically, we send a secret header that if present, should bypass all authentication and authorization checks. We have a filter for authenticating the user as an internal user if the header is present. Here is the code sample for what we use for authentication, key parts omitted and written as psuedocode. @Component
public class InternalRequestAuthenticationFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException {
String header = request.getHeader("someHeader");
if (headerIsInternalUser(header)) {
SecurityContextHolder.clearContext();
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
securityContext.setAuthentication(new AnAuthenticationObjectWithInternalUser());
SecurityContextHolder.setContext(securityContext);
}
filterChain.doFilter(request, response);
}
} This filter is injected in SecurityFilterChain and executed before our actual authentication checks are performed, so that the filters down the line will see that user is already authenticated.
Thank you for your suggestion, I didn't see that part. But when I tried it with my code, it doesn't compile since When I tried the same code with |
Thanks, @fatihdogmus, I see my mistake. Yes, please use |
Okay, gotcha. Given that the components that exist are already simple to use, I'd like to leave the API as-is to keep it simple. The constructor already takes an I'm open to taking another look in the future if such a need arises. |
@fatihdogmus Did you have this problem and how did you solve it ? - Thanks Note: If I creeate a CustomExpressionHandler and ExpressionRoot then it works but I wanted to avoid doing that. |
Hello,
I was trying to extend the current pre authorization behavior to suit our needs. Basically I wanted to look at the request headers, if some conditions are met, execute a custom authorization code and grant access from that logic, otherwise delegate to default pre authorize authorization manager.
But this was very hard to understand and do.
In the documents, for customizing the @PreAuthorize authorization logic, it is said to be done like this:
But this only shows how to replicate the default behavior, not how to customize it.
As I set to do customize my logic and add a custom AuthorizationManager, I was faced with these challanges:
Because of that, I had to create a default AuthorizationManagerBeforeMethodInterceptor, get its Pointcut, than create my own custom interceptor with this pointcut and my custom delegating AuthorizationManger.
Here is the code that I had to write:
And this is the custom AuthorizationManager:
The code is very small, but in order to get there, I had to fumble through a lot of source code.
There were a lot of pain points while trying to figure out how to do some basic thing.
Here our my suggestions:
I don't know if I over-complicated things with my limited knowledge of the new authorization architecture, but in other parts of the spring security, by just reading the source code and understanding the default behavior, I could extend or compose the existing classes with my own to suit my needs. But this was not that easy to do with the current authorization classes.
Thank you.
The text was updated successfully, but these errors were encountered: