单击登录按钮后,找不到Spring安全性获取页面中的自定义Html登录页面

时间:2017-03-26 13:21:56

标签: spring-security

以前可能会问过这个问题。但经过几次搜索,我无法找到任何解决方案。下面是我遵循的程序。 我有一个index.html页面,它是一个主页。在那个页面中有LogIn超链接。点击登录login.html页面打开。当我要提交下面的页面时发生了错误。

Error message

**web.xml**

<web-app 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"
    version="3.0">

    <display-name>Ramdhanu School</display-name>

    <welcome-file-list>
    <welcome-file>/pages/index.html</welcome-file>
    </welcome-file-list>

   <!-- Spring MVC -->

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

        <!-- Loads Spring Security config file -->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-security.xml,
            /WEB-INF/mvc-dispatcher-servlet.xml
        </param-value>
    </context-param>

    <!-- Spring Security -->

    <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>

</web-app>

MVC-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.ramdhanuschool.controller" />
    <!-- Define location and mapping of static content -->

   <mvc:default-servlet-handler />
   <mvc:annotation-driven/>
    <bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
        <value>/pages/</value>
      </property>
      <property name="suffix">
        <value>.html</value>
      </property>
    </bean>
<mvc:resources mapping = "/pages/**" location = "/pages/" />
</beans>

弹簧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.2.xsd">

    <http auto-config="true">
        <intercept-url pattern="/admin**" access="ROLE_USER" />
        <form-login
            login-page="/pages/login.html"
            default-target-url="/pages/hello.html"
            authentication-failure-url="/login?error"
            always-use-default-target="true"
            username-parameter="username"
            password-parameter="password" />
        <logout logout-success-url="/login?logout" />

        <!-- enable csrf protection >

        <csrf/>-->
    </http>
    <authentication-manager>
        <authentication-provider>
          <user-service>
            <user name="admin" password="admin" authorities="ROLE_USER" />
          </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

的login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>log in page</title>
</head>
<body>
<h1>Login page</h1>
<form name='loginForm'
          action="/j_spring_security_check" method='POST'>

          <table>
            <tr>
                <td>User:</td>
                <td><input type='text' name='j_username' value=''></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='j_password' /></td>
            </tr>
            <tr>
                <td colspan='2'><input name="submit" type="submit"
                    value="login" /></td>
            </tr>
          </table>
     <!-- 
          <input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />-->

        </form>
</body>
</html>

welcomeController.java

package com.ramdhanuschool.controller;

import java.security.Principal;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WelcomeController {

    @RequestMapping(value = { "/index" }, method = RequestMethod.GET)
    public ModelAndView homePage() {
        ModelAndView model = new ModelAndView();
        model.setViewName("index");
        return model;
    }



    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(
        @RequestParam(value = "error", required = false) String error,
        @RequestParam(value = "logout", required = false) String logout) {

        ModelAndView model = new ModelAndView();
        if (error != null) {
            model.addObject("error", "Invalid username and password!");
        }

        if (logout != null) {
            model.addObject("msg", "You've been logged out successfully.");
        }
        model.setViewName("login");

        return model;

    }
    @RequestMapping(value = { "/welcome**" }, method = RequestMethod.GET)
    public ModelAndView welcomePage() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Hello World");
        model.addObject("message", "This is welcome page!");
        model.setViewName("hello");
        return model;

    }

    @RequestMapping(value = "/admin**", method = RequestMethod.GET)
    public ModelAndView adminPage() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Hello World");
        model.addObject("message", "This is protected page!");
        model.setViewName("admin");

        return model;

    }
    }

1 个答案:

答案 0 :(得分:0)

<form name='loginForm' action="/j_spring_security_check" method='POST'>

这将把请求提交给基础目录,如果您的应用程序位于根路径中,那将是有效的,现在我认为您的应用程序位于子路径中,因此将操作更改为相对路径,j_spring_security_check <form name='loginForm' action="j_spring_security_check" method='POST'>