【问题标题】:Spring Security intercept-url is not matching WildcardSpring Security 拦截 URL 不匹配通配符
【发布时间】:2011-03-25 10:52:18
【问题描述】:

我尝试使用 spring security 为我的应用程序实现安全性。 我使用intercept-url截取页面,例如:

<http auto-config='true'>
    <intercept-url pattern="/logList*" access="ROLE_ADMIN" />
    <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" />
    <logout />
    <remember-me/>
</http> 

我第一次尝试使用 url 访问匿名用户的日志:localhost/projectname/logList 并且页面会自动重定向到登录页面

但是当我尝试使用 url localhost/projectname/logList/ 访问日志页面时,匿名用户可以访问日志页面

当模式/logList* 正确时为什么会发生这种情况?

【问题讨论】:

  • 您是否确定第二次访问该 URL 时,您还没有经过身份验证的会话?对于第一次尝试点击登录页面但第二次不会重定向到登录页面的原因,没有其他解释。你能澄清一下你正在采取的具体步骤吗?

标签: jakarta-ee spring-security


【解决方案1】:

默认情况下,使用 AntPathRequestMatcher。如果你添加另一个模式

&lt;intercept-url pattern="/logList/*" access="ROLE_ADMIN" /&gt; 那么它会工作。

这里是测试(请注意,对于 RegexRequestMatcher,相同的模式适用于 /logList/ 和 /logList):

    @Test
public void antTest1() throws Exception {

    AntPathRequestMatcher pathMatcher = new AntPathRequestMatcher("/loglist*");
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
     mockRequest.setScheme("http");
     mockRequest.setPathInfo("/logList");
    Assert.assertThat(pathMatcher.matches(mockRequest), is(true));
}

@Test
public void antTest2() throws Exception {


    AntPathRequestMatcher pathMatcher = new AntPathRequestMatcher("/loglist/*");
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
     mockRequest.setScheme("http");
     mockRequest.setPathInfo("/logList/");
    Assert.assertThat(pathMatcher.matches(mockRequest), is(true));
}

@Test
public void regexTest3() throws Exception {

    RegexRequestMatcher pathMatcher = new RegexRequestMatcher("/logList.*", "GET");
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
     mockRequest.setScheme("http");
     mockRequest.setMethod("GET");
     mockRequest.setPathInfo("/logList/");
    Assert.assertThat(pathMatcher.matches(mockRequest), is(true));
}

@Test
public void regexTest4() throws Exception {

    RegexRequestMatcher pathMatcher = new RegexRequestMatcher("/logList.*", "GET");
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
     mockRequest.setScheme("http");
     mockRequest.setMethod("GET");
     mockRequest.setPathInfo("/logList");
    Assert.assertThat(pathMatcher.matches(mockRequest), is(true));
}

要使用 RegexRequestMatcher,请将属性 'request-matcher' 添加到 http 并将其值设置为 'regex':

&lt;http auto-config="true" request-matcher="regex"&gt;

【讨论】:

  • 请注意,“request-matcher”需要 Spring Sec 3.1+。低于此的版本中的等价物是“路径类型”。
  • 我在 spring sec 3.0 中使用了 path-type="regex",但出现了错误 NullPointerExecption。我还在下载 spring sec 3.1+
【解决方案2】:

pattern="/logList/**" 有影响吗?

【讨论】:

  • 我尝试过使用**模式,但没有区别
猜你喜欢
  • 2017-06-15
  • 1970-01-01
  • 2016-11-08
  • 2012-08-15
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
相关资源
最近更新 更多