2013-06-03 185 views
0

我正在尝试使用春季安全创建登录用例(我指的是this示例)。登录注销春季安全用例

如果点击login按钮输入用户名和密码后,它被请求

j_spring_security_check.htm 

URL和我得到HTTP状态404 - 对于上面的网址。

正在使用netbeans IDE。我不知道哪里出了问题。请帮忙。

这里是web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 

    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>redirect.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

内容这是login.jsp

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


      <form name='f' action="<c:url value='j_spring_security_check.htm' />" method="POST"> 

       <div style="margin-left: 27%"> 
        <input type="text" name="j_username" value="" placeholder="Username" class="login_hint" id="username" title="Username" /> 
        <br/> 
        <input type="password" name="j_password" value="" placeholder="Password" class="login_hint" id="password" title="Password" /> 
        <br /> 
        <input type ="submit" value ="Login" class ="btn btn-primary loginBtn"/> 
       </div> 
      </form> 

代码来登录表单这里是applicationContext-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security" 
     xmlns:beans="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.0.3.xsd"> 

     <http auto-config="true"> 
       <intercept-url pattern="/admin*" access="ROLE_USER" /> 
       <form-login login-page="/login" default-target-url="/admin/home.htm" 
         authentication-failure-url="/loginfailed" /> 
       <logout logout-success-url="/logout" /> 
     </http> 

     <authentication-manager> 
      <authentication-provider> 
       <user-service> 
         <user name="admin" password="root" authorities="ROLE_USER" /> 
       </user-service> 
      </authentication-provider> 
     </authentication-manager> 

</beans:beans> 

回答

2

UsernamePasswordAuthenticationFilter拦截内容请求发送到/j_spring_security_check(默认情况下),所以最有可能你只需要删除.htm从行动网址结尾的login.jsp

<form name='f' action="<c:url value='j_spring_security_check'/>" method="POST"> 

哦,似乎有些东西是从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> 
+0

不,它不工作!再次是相同的404。这次,而不是'/ j_spring_security_check.htm'它的'/ j_spring_security_check' – codeMan

+0

我的web.xml是否正确?我应该包含关于'UsernamePasswordAuthenticationFilter'的任何内容吗? – codeMan

+0

查看我更新的答案。看起来你在你的问题中链接的教程并不完整(至少列出的6个步骤对于正在进行的项目来说是不够的)。根据评论,其他人也有同样的问题。通读评论,并下载该exapmle项目的来源应该工作,我猜。 – zagyi

1
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 

    <display-name>Sample</display-name> 
    <servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
       /WEB-INF/spring/appServlet/servlet-context.xml 
      </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
      /WEB-INF/spring/root-context.xml, 
      /WEB-INF/spring/spring-security.xml 
      </param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <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> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

</web-app> 

才有的servlet - URL映射为的* .htm 和过滤器映射URL模式为*

试试这个。它会工作。希望这可以帮助