Skip to content

Prefer dispatcher context for authorize tag beans#18822

Open
wonderfulrosemari wants to merge 1 commit intospring-projects:mainfrom
wonderfulrosemari:gh-8843-jsp-authorize-child-context
Open

Prefer dispatcher context for authorize tag beans#18822
wonderfulrosemari wants to merge 1 commit intospring-projects:mainfrom
wonderfulrosemari:gh-8843-jsp-authorize-child-context

Conversation

@wonderfulrosemari
Copy link
Contributor

Closes gh-8843

When both root and child web application contexts are present, JSP authorize
tags should resolve security beans from the DispatcherServlet context used for
the current request.

Previously, AbstractAuthorizeTag always resolved beans from
findRequiredWebApplicationContext(servletContext), which prefers the root
context. If security beans were defined only in the child context, this could
cause failures like missing WebSecurityExpressionHandler.

Changes include:

  • resolve application context from the current request's DispatcherServlet
    context attribute when available
  • fall back to SecurityWebApplicationContextUtils.findRequiredWebApplicationContext
    when no dispatcher context is present
  • add regression coverage for root+child context setup to ensure
    <sec:authorize> expression evaluation succeeds

Signed-off-by: wonderfulrosemari <whwlsgur1419@naver.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 28, 2026
Copy link
Contributor

@jzheaux jzheaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @wonderfulrosemari, for the PR! I've left feedback inline.

*/
public abstract class AbstractAuthorizeTag {

private static final String DISPATCHER_SERVLET_CONTEXT_ATTRIBUTE = "org.springframework.web.servlet.DispatcherServlet.CONTEXT";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's please make this public so it's clear that Spring Security expects applications to set this attribute themselves. WebAttributes is a good home for this. Please also take a look at the naming convention used in WebAttributes for both the name and the value.

*/
public abstract class AbstractAuthorizeTag {

private static final String DISPATCHER_SERVLET_CONTEXT_ATTRIBUTE = "org.springframework.web.servlet.DispatcherServlet.CONTEXT";
Copy link
Contributor

@jzheaux jzheaux Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, it doesn't seem to me that what is being retrieved here is the dispatcher servlet's context, per se. The point of Rob's suggestion is to decouple the application context from its source. Thus, a better name might be APPLICATION_CONTEXT_ATTRIBUTE.

}

private ApplicationContext getApplicationContext() {
Object dispatcherContext = getRequest().getAttribute(DISPATCHER_SERVLET_CONTEXT_ATTRIBUTE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point in the code, we aren't sure what it is, so perhaps value is a better variable name.


private ApplicationContext getApplicationContext() {
Object dispatcherContext = getRequest().getAttribute(DISPATCHER_SERVLET_CONTEXT_ATTRIBUTE);
if (dispatcherContext instanceof ApplicationContext applicationContext) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of silently falling back, let's please error if the attribute is being misused:

if (value == null) {
    return SecurityWebApplicationContextUtils.findRequiredWebApplicationContext(getServletContext());
}
if (value instanceof ApplicationContext context) {
    return context;
}
throw new IllegalArgumentException("WebAttributes.APPLICATION_CONTEXT_ATTRIBUTE value must be of type ApplicationContext, found type " + value.getClass());

The reason for this is to alert the application developer to the misconfiguration as early as possible.


@Test
@SuppressWarnings("rawtypes")
public void expressionFromDispatcherContextWhenRootContextPresent() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the dispatcher-specific language in this test.

this.tag.setAccess("permitAll");
assertThat(this.tag.authorize()).isTrue();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test to confirm an error if the attribute is not of type ApplicationContext.

given(dispatcher.getBeanNamesForType(SecurityContextHolderStrategy.class)).willReturn(new String[0]);
this.request.setAttribute("org.springframework.web.servlet.DispatcherServlet.CONTEXT", dispatcher);
this.tag.setAccess("permitAll");
assertThat(this.tag.authorize()).isTrue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please verify here that the mocked context was the one that was used.


@Test
@SuppressWarnings("rawtypes")
public void expressionFromDispatcherContextWhenRootContextPresent() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name here seems to indicate that we would only expect looking up the "dispatcher context" when the "root context" is present. However, the logic you are adding is about using the WebAttribute-based Application Context when one is specified. Will you please adjust the name?

@jzheaux jzheaux self-assigned this Mar 5, 2026
@jzheaux jzheaux added in: taglibs An issue in spring-security-taglibs type: enhancement A general enhancement status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: taglibs An issue in spring-security-taglibs status: waiting-for-feedback We need additional information before we can continue type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JspAuthorizeTag cannot be used if spring-security configurations (beans) is not put in ROOT context (XML config).

3 participants