Use SecurityContextHolderStrategy for Taglibs
Issue gh-11060
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2004-2010 the original author or authors.
|
||||
* Copyright 2004-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -32,7 +32,9 @@ import org.springframework.expression.ParseException;
|
||||
import org.springframework.security.access.expression.ExpressionUtils;
|
||||
import org.springframework.security.access.expression.SecurityExpressionHandler;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
||||
import org.springframework.security.web.FilterInvocation;
|
||||
import org.springframework.security.web.WebAttributes;
|
||||
import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator;
|
||||
@@ -110,7 +112,7 @@ public abstract class AbstractAuthorizeTag {
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean authorizeUsingAccessExpression() throws IOException {
|
||||
if (SecurityContextHolder.getContext().getAuthentication() == null) {
|
||||
if (getContext().getAuthentication() == null) {
|
||||
return false;
|
||||
}
|
||||
SecurityExpressionHandler<FilterInvocation> handler = getExpressionHandler();
|
||||
@@ -131,7 +133,7 @@ public abstract class AbstractAuthorizeTag {
|
||||
FilterInvocation f = new FilterInvocation(getRequest(), getResponse(), (request, response) -> {
|
||||
throw new UnsupportedOperationException();
|
||||
});
|
||||
return handler.createEvaluationContext(SecurityContextHolder.getContext().getAuthentication(), f);
|
||||
return handler.createEvaluationContext(getContext().getAuthentication(), f);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,7 +144,7 @@ public abstract class AbstractAuthorizeTag {
|
||||
*/
|
||||
public boolean authorizeUsingUrlCheck() throws IOException {
|
||||
String contextPath = ((HttpServletRequest) getRequest()).getContextPath();
|
||||
Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
|
||||
Authentication currentUser = getContext().getAuthentication();
|
||||
return getPrivilegeEvaluator().isAllowed(contextPath, getUrl(), getMethod(), currentUser);
|
||||
}
|
||||
|
||||
@@ -170,6 +172,17 @@ public abstract class AbstractAuthorizeTag {
|
||||
this.method = (method != null) ? method.toUpperCase() : null;
|
||||
}
|
||||
|
||||
private SecurityContext getContext() {
|
||||
ApplicationContext appContext = SecurityWebApplicationContextUtils
|
||||
.findRequiredWebApplicationContext(getServletContext());
|
||||
String[] names = appContext.getBeanNamesForType(SecurityContextHolderStrategy.class);
|
||||
if (names.length == 1) {
|
||||
SecurityContextHolderStrategy strategy = appContext.getBean(SecurityContextHolderStrategy.class);
|
||||
return strategy.getContext();
|
||||
}
|
||||
return SecurityContextHolder.getContext();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private SecurityExpressionHandler<FilterInvocation> getExpressionHandler() throws IOException {
|
||||
ApplicationContext appContext = SecurityWebApplicationContextUtils
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.security.access.PermissionEvaluator;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
||||
import org.springframework.security.taglibs.TagLibConfig;
|
||||
import org.springframework.security.web.context.support.SecurityWebApplicationContextUtils;
|
||||
|
||||
@@ -57,6 +58,9 @@ public class AccessControlListTag extends TagSupport {
|
||||
|
||||
protected static final Log logger = LogFactory.getLog(AccessControlListTag.class);
|
||||
|
||||
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
|
||||
.getContextHolderStrategy();
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private Object domainObject;
|
||||
@@ -78,7 +82,7 @@ public class AccessControlListTag extends TagSupport {
|
||||
// Of course they have access to a null object!
|
||||
return evalBody();
|
||||
}
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
Authentication authentication = this.securityContextHolderStrategy.getContext().getAuthentication();
|
||||
if (authentication == null) {
|
||||
logger.debug("SecurityContextHolder did not return a non-null Authentication object, so skipping tag body");
|
||||
return skipBody();
|
||||
@@ -146,6 +150,12 @@ public class AccessControlListTag extends TagSupport {
|
||||
}
|
||||
this.applicationContext = getContext(this.pageContext);
|
||||
this.permissionEvaluator = getBeanOfType(PermissionEvaluator.class);
|
||||
String[] names = this.applicationContext.getBeanNamesForType(SecurityContextHolderStrategy.class);
|
||||
if (names.length == 1) {
|
||||
SecurityContextHolderStrategy strategy = this.applicationContext
|
||||
.getBean(SecurityContextHolderStrategy.class);
|
||||
this.securityContextHolderStrategy = strategy;
|
||||
}
|
||||
}
|
||||
|
||||
private <T> T getBeanOfType(Class<T> type) throws JspException {
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.security.taglibs.authz;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.PageContext;
|
||||
import javax.servlet.jsp.tagext.Tag;
|
||||
@@ -25,9 +26,12 @@ import javax.servlet.jsp.tagext.TagSupport;
|
||||
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
||||
import org.springframework.security.web.context.support.SecurityWebApplicationContextUtils;
|
||||
import org.springframework.security.web.util.TextEscapeUtils;
|
||||
import org.springframework.web.util.TagUtils;
|
||||
|
||||
@@ -42,6 +46,9 @@ import org.springframework.web.util.TagUtils;
|
||||
*/
|
||||
public class AuthenticationTag extends TagSupport {
|
||||
|
||||
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
|
||||
.getContextHolderStrategy();
|
||||
|
||||
private String var;
|
||||
|
||||
private String property;
|
||||
@@ -76,6 +83,18 @@ public class AuthenticationTag extends TagSupport {
|
||||
this.scopeSpecified = true;
|
||||
}
|
||||
|
||||
public void setPageContext(PageContext pageContext) {
|
||||
super.setPageContext(pageContext);
|
||||
ServletContext servletContext = pageContext.getServletContext();
|
||||
ApplicationContext context = SecurityWebApplicationContextUtils
|
||||
.findRequiredWebApplicationContext(servletContext);
|
||||
String[] names = context.getBeanNamesForType(SecurityContextHolderStrategy.class);
|
||||
if (names.length == 1) {
|
||||
SecurityContextHolderStrategy strategy = context.getBean(SecurityContextHolderStrategy.class);
|
||||
this.securityContextHolderStrategy = strategy;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int doStartTag() throws JspException {
|
||||
return super.doStartTag();
|
||||
@@ -86,12 +105,11 @@ public class AuthenticationTag extends TagSupport {
|
||||
Object result = null;
|
||||
// determine the value by...
|
||||
if (this.property != null) {
|
||||
if ((SecurityContextHolder.getContext() == null)
|
||||
|| !(SecurityContextHolder.getContext() instanceof SecurityContext)
|
||||
|| (SecurityContextHolder.getContext().getAuthentication() == null)) {
|
||||
SecurityContext context = this.securityContextHolderStrategy.getContext();
|
||||
if ((context == null) || !(context instanceof SecurityContext) || (context.getAuthentication() == null)) {
|
||||
return Tag.EVAL_PAGE;
|
||||
}
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
Authentication auth = context.getAuthentication();
|
||||
if (auth.getPrincipal() == null) {
|
||||
return Tag.EVAL_PAGE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user