【问题标题】:Spring Security春季安全
【发布时间】:2013-12-22 16:34:41
【问题描述】:

我遇到了弹簧安全问题并显示错误消息

这是我的 root-context.xml 中的内容

   `<context:property-placeholder location="classpath:config.properties" /> 
    <!-- Register the Customer.properties -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
       <property name="basename" value="mymessages" />
    </bean>

<security:http auto-config='true'>
<security:intercept-URL pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<security:intercept-URL pattern="/**" access="ROLE_USER" />
<security:form-login login-page='/login' default-target-url="/" 
    authentication-failure-URL="/loginfailed"/>
<security:logout logout-success-url="/" logout-URL="/j_spring_security_logout" />   

</security:http>
<security:authentication-manager> 
<security:authentication-provider>
<security:user-service>
<security:user name="billy" password="123456" authorities="ROLE_USER" />     
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>`

在我的 web.xml 中是

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <URL-pattern>/*</URL-pattern>
</filter-mapping>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

我的 .jsp 是

<body>
    <%@ include file="include/login1.jsp"%>

    <c:if test="${not empty error}">
    <div class="errorblock">Your login attempt was not successful, try again.<br/> Caused :
    </div>
    </c:if>

</body>
</html>

当我第一次使用错误的用户凭据登录时,它只是重新加载登录页面。然后,如果我使用正确的登录凭据登录,它将加载登录并显示文本“您的登录尝试不成功,请重试。原因:”但在原因之后没有提示错误凭据的弹簧消息: 非常感谢您对此事的任何帮助

【问题讨论】:

  • 对不起我的上一条评论,我贴错了。我的意思是为什么你的 web.xml 定义了两次springSecurityFilterChain
  • 嘿,谢谢您的回复。过滤器重复只是复制过去的错误。在我的实际代码中不是这样的(第一次发布:-/)

标签: java spring jsp spring-mvc


【解决方案1】:

您的身份验证失败的 URL 似乎需要身份验证。

<security:intercept-URL pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:intercept-URL pattern="/**" access="ROLE_USER"/>

此配置将只允许未经身份验证的人访问 /login 页面,而不是 /loginfailed 页面。尝试将登录拦截 URL 更改为:

<security:intercept-URL pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:intercept-URL pattern="/**" access="ROLE_USER"/>

或者,您可以添加另一个专门调用 /loginfailed url 的拦截 url。

可能发生的情况是,当您第一次尝试登录时,它会将您重定向到登录失败页面“/loginfailed”,由于身份验证失败,这会导致另一个重定向回登录页面。然后,当您正确登录时,它会将您重定向回“/loginfailed”页面,因为这是登录重定向之前的原始请求。

您可以使用另一个参数,该参数将始终将您发送到默认目标...

<form-login login-page='/login' default-target-url='/'
    authentication-failure-url="/loginfailed" always-use-default-target='true' />

试一试,看看它是否有效。

编辑:这是一个完整的安全设置示例。在这个设置中,我使用了额外的 http 声明而不是额外的拦截 URL。我的 login.jsp 会根据 login_error 参数更改其内容(例如,login_error=1 使其显示“用户名或密码不正确”的消息,而 login_error=2 使其显示会话已超时的消息,并且请重新登录)。

<!--  No security on js,css,image and other static resources -->
<http pattern="/resources/**" security="none" />

<!--  No security on error pages -->
<http pattern="/error/**" security="none" />

<!--  No security on pages starting with login -->
<http pattern="/login*" security="none" />

<http auto-config='true'>
    <!--  Everything else requires ROLE_USER -->
    <intercept-url pattern="/**" access="ROLE_USER" />  
    <access-denied-handler error-page="/error/403"/>

    <!-- Custom login page -->
    <form-login login-page='/login' default-target-url='/'
        authentication-failure-url="/login?login_error=1"
        always-use-default-target='false' />

    <!-- Allow user to stay logged in -->
    <remember-me />

    <!-- Custom logout page and remove the session id cookie -->
    <logout logout-url='/logout' delete-cookies="JSESSIONID"/>
    <session-management>
        <concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
    </session-management>

    <!-- Custom session timeout page -->
    <session-management invalid-session-url="/login?login_error=2" />
</http>

【讨论】:

  • 嗨@reblace,你是说用access="ROLL-USER"删除拦截吗??还可能值得注意的是,我的登录控制器 requestMapping 正在请求 /loginfailed 它将错误设置为 true 并返回 login.jsp 现在应该显示一个错误 div。 @RequestMapping(value = "/loginfailed", method = RequestMethod.GET) public String home2(Model model) { model.addAttribute("error", "true");返回“登录”; }
  • 不,抱歉,我没有建议您删除第二个。只是您需要将 'login' 更改为 'login*' 以便 /login 和 /loginfailed 都可以匿名访问。 (您还可以在登录后立即添加另一个拦截 URL,将 /loginfailed 显式设置为不需要身份验证)。如果某人的登录失败,他们仍然是匿名的,因此您希望他们重定向到的页面需要对匿名用户可见。当我回到电脑前时,我将更新帖子,详细说明我通常如何设置登录表单。
【解决方案2】:

去掉这行试试

<security:intercept-URL pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" />

 auto-config='true'

仅当您手动配置服务时才需要。可能是工作

【讨论】:

    猜你喜欢
    • 2014-02-02
    • 2012-12-13
    • 2017-11-30
    • 2021-05-12
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多