You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by lp...@apache.org on 2022/12/06 22:19:28 UTC

[shiro] branch main updated: Performance optimization Change the acquisition of system properties to static variables to reduce contension in high concurrency environments. The Boolean.getBoolean method calls Hashtable to obtain system properties, and will lock.

This is an automated email from the ASF dual-hosted git repository.

lprimak pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shiro.git


The following commit(s) were added to refs/heads/main by this push:
     new 86f605b9 Performance optimization Change the acquisition of system properties to static variables to reduce contension in high concurrency environments. The Boolean.getBoolean method calls Hashtable to obtain system properties, and will lock.
     new 17ae4977 Merge pull request #526 from zenglzh/main
86f605b9 is described below

commit 86f605b971afc9cda39d0b1f287feffc2442109e
Author: lprimak <le...@flowlogix.com>
AuthorDate: Tue Dec 6 15:38:34 2022 -0600

    Performance optimization
    Change the acquisition of system properties to static variables to reduce contension in high concurrency environments.
    The Boolean.getBoolean method calls Hashtable to obtain system properties, and will lock.
---
 .../apache/shiro/web/filter/InvalidRequestFilter.java    |  2 +-
 .../main/java/org/apache/shiro/web/util/WebUtils.java    | 16 +++++++++++++++-
 .../org/apache/shiro/web/RestoreSystemProperties.java    |  4 +++-
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/web/src/main/java/org/apache/shiro/web/filter/InvalidRequestFilter.java b/web/src/main/java/org/apache/shiro/web/filter/InvalidRequestFilter.java
index 63da6d41..3795d7f4 100644
--- a/web/src/main/java/org/apache/shiro/web/filter/InvalidRequestFilter.java
+++ b/web/src/main/java/org/apache/shiro/web/filter/InvalidRequestFilter.java
@@ -50,7 +50,7 @@ public class InvalidRequestFilter extends AccessControlFilter {
 
     private boolean blockSemicolon = true;
 
-    private boolean blockBackslash = !Boolean.getBoolean(WebUtils.ALLOW_BACKSLASH);
+    private boolean blockBackslash = !WebUtils.isAllowBackslash();
 
     private boolean blockNonAscii = true;
 
diff --git a/web/src/main/java/org/apache/shiro/web/util/WebUtils.java b/web/src/main/java/org/apache/shiro/web/util/WebUtils.java
index 992f7ade..0ed5b110 100644
--- a/web/src/main/java/org/apache/shiro/web/util/WebUtils.java
+++ b/web/src/main/java/org/apache/shiro/web/util/WebUtils.java
@@ -58,6 +58,7 @@ public class WebUtils {
     public static final String SERVLET_RESPONSE_KEY = ServletResponse.class.getName() + "_SHIRO_THREAD_CONTEXT_KEY";
 
     public static final String ALLOW_BACKSLASH = "org.apache.shiro.web.ALLOW_BACKSLASH";
+    private static boolean IS_ALLOW_BACKSLASH = Boolean.getBoolean(ALLOW_BACKSLASH);
 
     /**
      * {@link org.apache.shiro.session.Session Session} key used to save a request and later restore it, for example when redirecting to a
@@ -165,7 +166,7 @@ public class WebUtils {
      * @return normalized path
      */
     public static String normalize(String path) {
-        return normalize(path, Boolean.getBoolean(ALLOW_BACKSLASH));
+        return normalize(path, IS_ALLOW_BACKSLASH);
     }
 
     /**
@@ -689,4 +690,17 @@ public class WebUtils {
         WebUtils.issueRedirect(request, response, successUrl, null, contextRelative);
     }
 
+    public static boolean isAllowBackslash(){
+        return IS_ALLOW_BACKSLASH;
+    }
+
+    /**
+     * Reload System properties.
+     * If relevant system property is modified ,this method needs to be called to take effect.
+     * There is a property <code>org.apache.shiro.web.ALLOW_BACKSLASH</code> that needs attention.
+     */
+    public static void reloadSystemProperties(){
+        IS_ALLOW_BACKSLASH = Boolean.getBoolean(ALLOW_BACKSLASH);
+    }
+
 }
diff --git a/web/src/test/java/org/apache/shiro/web/RestoreSystemProperties.java b/web/src/test/java/org/apache/shiro/web/RestoreSystemProperties.java
index 882e2e97..19ce4a3d 100644
--- a/web/src/test/java/org/apache/shiro/web/RestoreSystemProperties.java
+++ b/web/src/test/java/org/apache/shiro/web/RestoreSystemProperties.java
@@ -19,6 +19,7 @@
 package org.apache.shiro.web;
 
 import groovy.lang.Closure;
+import org.apache.shiro.web.util.WebUtils;
 
 import java.io.Closeable;
 import java.util.Collections;
@@ -41,6 +42,7 @@ public class RestoreSystemProperties implements Closeable {
 
     public void restore() {
         System.setProperties(originalProperties);
+        WebUtils.reloadSystemProperties();
     }
 
     private Properties copyOf(Properties source) {
@@ -57,7 +59,7 @@ public class RestoreSystemProperties implements Closeable {
 
         try (RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties()) {
             properties.forEach(System::setProperty);
-
+            WebUtils.reloadSystemProperties();
             return closure.call();
         }
     }