【问题标题】:Servlet authentication using Shiro, Is it possible to filter URLs having some keyword?使用 Shiro 的 Servlet 身份验证,是否可以过滤具有某些关键字的 URL?
【发布时间】:2023-03-19 19:47:02
【问题描述】:

我有一个 servlet 应用程序在 tomcat 中运行,并带有 shiro 身份验证。 我的 servlet URL 如下所示

 http://builds/Query/User?which_option=ui_data&which_out=json

上述 URL 中的 "which_option" 可以采用各种值。 我只想验证那些在 shiro 中具有 "which_option=ui_data" 的 URL。 我在 shiro.ini 的 URL 过滤中使用正则表达式尝试了以下操作。

[urls]
/Query/User*ui_data* = authcBuilds

但这不起作用。 Shiro URL configuration 页面提到 URL 表达式必须是 URL_Ant_Path_ExpressionANT path expression 似乎只适用于匹配文件名,而不是 URL 字符串的一部分。

还有其他方法可以做到这一点(网址正则表达式匹配)吗?否则我必须将我的代码转移到另一个 servlet,例如

http://builds/Query/UI_Data

并在 shiro.ini 中使用以下身份验证

[urls]
/Query/UI_Data* = authcBuilds

【问题讨论】:

    标签: java tomcat servlets ant shiro


    【解决方案1】:

    Shiro 的 AntMacher 实现 org.apache.shiro.util.AntMatcher 确实根据需要匹配您的 URL。

    import org.apache.shiro.util.AntPathMatcher;
    import org.junit.Test;
    
    import static org.junit.Assert.assertFalse;
    import static org.junit.Assert.assertTrue;
    
    public class AntMatcherTest {
    
        @Test
        public void match() {
            final AntPathMatcher matcher = new AntPathMatcher();
    
            assertTrue(matcher.match("/Query/User*ui_data*", "/Query/User?which_option=ui_data"));
        }
    
        @Test
        public void noMatch() {
            final AntPathMatcher matcher = new AntPathMatcher();
    
            assertFalse(matcher.match("/Query/User*ui_data*", "/Query/User?which_option="));
        }
    }
    

    Shiro 的 javax.servlet.http.HttpServletRequest 实现将 URL 拆分为多个部分:查询字符串的所有内容都进入匹配中使用的 URL。因此它不会匹配查询参数。

    编写您自己的 FormAuthenticationFilter,您将可以访问 ServletRequest 以检查查询参数。

    【讨论】:

      猜你喜欢
      • 2019-01-05
      • 2015-06-05
      • 2015-04-01
      • 2017-07-02
      • 1970-01-01
      • 2014-12-20
      • 2013-12-29
      • 2017-04-28
      • 2012-10-27
      相关资源
      最近更新 更多