java - Spring 3 安全性 j_spring_security_check

标签 java spring spring-mvc spring-security spring-3

我正在尝试了解 Spring Security 的工作原理,因此我下载了一些示例项目,然后尝试在我的项目中实现该解决方案。但是当我尝试登录时,我得到 404 错误,并且在地址栏中我有 http://localhost:8080/fit/j_spring_security_check。我试图在这里查看类似的问题,但我无法意识到如何将其应用于我的项目。如果有经验更丰富的人能帮助我,我将不胜感激。

我的应用结构如下所示:

enter image description here

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:security="http://www.springframework.org/schema/security"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<context:annotation-config/>

<context:component-scan base-package="cz.cvut.fit"/>

<import resource="classpath:applicationContext-security.xml"/>

</beans>

applicationContext-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:security="http://www.springframework.org/schema/security"
   xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<context:annotation-config/>

<context:component-scan base-package="cz.cvut.fit" />

<mvc:annotation-driven />

<security:global-method-security jsr250-annotations="enabled"
                                 proxy-target-class="true"/>
</beans>

applicationContext-security.xml:

<beans xmlns:security="http://www.springframework.org/schema/security"
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/security
             http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<security:http pattern="/css/**" security="none"/>
<security:http pattern="/views/login.jsp*" security="none"/>
<security:http pattern="/views/denied.jsp" security="none"/>

<security:http auto-config="true" access-denied-page="/denied.jsp" servlet-api-provision="false">
    <security:intercept-url pattern="/views/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
    <security:intercept-url pattern="/views/edit/**" access="ROLE_EDIT"/>
    <security:intercept-url pattern="/views/admin/**" access="ROLE_ADMIN"/>
    <security:intercept-url pattern="/**" access="ROLE_USER"/>
    <security:form-login login-page="/views/login.jsp" authentication-failure-url="/denied.jsp"
                         default-target-url="/home.jsp"/>
    <security:logout/>
</security:http>

<security:authentication-manager>
    <security:authentication-provider>
        <security:user-service>
            <security:user name="adam" password="adampassword" authorities="ROLE_USER"/>
            <security:user name="jane" password="janepassword" authorities="ROLE_USER, ROLE_ADMIN"/>
            <security:user name="sue" password="suepassword" authorities="ROLE_USER, ROLE_EDIT"/>
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

</beans>

最佳答案

您正在尝试根据网页的当前上下文路径验证 uri。 JSTL 标记库可用于确保您根据应用程序的上下文轻松生成正确的 url。如果你想快速实现它,你可以使用标签库来做到这一点。为此,您可以将 jSTL 标记库添加到 jsp 的顶部:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

然后您可以使用以下内容发布到登录 servlet。

<form action="<c:url value="/j_spring_security_check"></c:url>" method="post" role="form">

这可确保您始终发布到 /j_spring_security_check。

jSTL 引用: http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/url.html

关于java - Spring 3 安全性 j_spring_security_check,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15365477/

相关文章:

java - spring-security 表单例份验证迁移到注释

java - Spring Bean 生命周期 - 构造函数中的 @value 属性为 null

java - 检查请求日期是否在特定时间内的简单方法。使用 for 循环和 if 语句

java - 云服务的 SecurityManager "sandbox"

Java 多项式加法

java - 使用 XStream 通过线路发送的 String 的反序列化问题

java - 如何为具有多个虚拟主机的 Tomcat 放置配置文件?

java - Spring MVC - HTTP 状态 500

java - Spring MVC 3.1 根据请求语言环境更改语言 i18n

java - 从不同的 JFrame 更新 JTable