【问题标题】:Spring Security Authentication is not working as expected in my configurationSpring Security Authentication 在我的配置中没有按预期工作
【发布时间】:2013-01-10 22:33:23
【问题描述】:

我已经如下配置了 spring 身份验证,但它没有按预期工作

<sec:http auto-config="true">
    <!-- Restrict URLs based on role -->
    <sec:intercept-url pattern="pages/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />

    <sec:intercept-url pattern="/css/style.css" access="IS_AUTHENTICATED_ANONYMOUSLY" />
     <sec:intercept-url pattern="pages/**" access="ROLE_USER" />

    <!-- Override default login and logout pages -->
    <sec:form-login login-page="/login.jsp" 
                         default-target-url="/pages/products.xhtml" 
                         authentication-failure-url="/login.html?login_error=1" />
    <sec:logout logout-url="/logout" logout-success-url="/login.jsp" />
</sec:http>

在服务器启动时,我被重定向到 login.jsp,如果我使用登录表单,到目前为止我被重定向到 products.xhtml,但如果我直接访问 products.xhtml,它只允许我访问 product.xhtml (即使在关闭 broser 甚至在服务器重新启动之后)而不是重定向到 login.jsp 。谁能告诉我我到底错过了什么?

感谢和问候 维杰

【问题讨论】:

  • 也许您还没有注销仍然有一个有效的会话?
  • @micha 我退出了。我测试了在服务器重新启动后直接访问 products.xhtml,它允许我访问而不是重定向到登录表单
  • 你在web.xml中添加spring安全过滤器了吗?
  • @Mich yes as org.springframework.security.web.session.HttpSessionEventPublisher springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxyspringSecurityFilterChain/* 60

标签: spring-mvc spring-security


【解决方案1】:

您的模式和网址不一致。登录页面有“/login.jsp”,intercept-url 模式中有“pages/login.jsp”。

尝试使用:

<http pattern="/css/**" security="none">

<http>
    <intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/**" access="ROLE_USER" />
    <form-login login-page="/login.jsp" 
                     default-target-url="/pages/products.xhtml" 
                     authentication-failure-url="/login.html?login_error=1" />
    <logout logout-url="/logout" logout-success-url="/login.jsp" />
</http>

特定请求的调试日志将准确解释其安全或不安全的原因。

【讨论】:

  • 虽然它不起作用。尽管我可以访问其余文件,但我尝试了很多方法,我猜这是 Spring Security 中的一个错误。
【解决方案2】:

确保您没有 Cookie 或有效会话...

【讨论】:

    猜你喜欢
    • 2012-03-06
    • 2018-09-23
    • 2020-01-18
    • 2016-10-25
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 2012-05-15
    • 2021-09-03
    相关资源
    最近更新 更多