Spring Security 4自定义登录页面失败认证

时间:2016-02-06 12:12:48

标签: spring-mvc spring-security

请为我的项目需要帮助,当我使用自定义登录页面时,身份验证不会继续,它始终显示用户或密码无效。

的pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.football</groupId>
  <artifactId>reservation</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>reservation Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>reservation</finalName>
  </build>
</project>

Web.xml中

      <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID" version="3.1">

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:/spring/mvc-config.xml,
                classpath:/spring/security-config.xml,
            </param-value>
        </context-param>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                    <param-name>contextAttribute</param-name>
                    <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/login</url-pattern>
            <url-pattern>/</url-pattern>
        </servlet-mapping>


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

        <!-- Spring Security -->
        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
            <init-param>
                <param-name>contextAttribute</param-name>
                <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
            </init-param>
        </filter>

        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    </web-app>

的Login.jsp

     <!doctype html>
    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <spring:url value="/resources/css/bootstrap.min.css" var="bootstrapcss" />
    <link href="${bootstrapcss}" rel="stylesheet" />
    <spring:url value="/resources/css/formValidation.min.css"
        var="formValidation" />
    <link href="${formValidation}" rel="stylesheet" />
    <spring:url value="/resources/css/navbar.css" var="navbar" />
    <link href="${navbar}" rel="stylesheet" />
    <spring:url value="/resources/css/main.css" var="main" />
    <link href="${main}" rel="stylesheet" />
    <title>Login</title>
    </head>
    <body>
        <jsp:include page="Header.jsp" />
        <section class="container-fluid">
            <div class="row">
                <div class="jumbotron col-centered col-md-4 col-sm-6 col-xs-9">
                    <c:if test="${param.login_error != null}">
                        Invalid username / password
                    </c:if>
                    <c:url var="loginUrl" value="/login" />
                    <form class="form-horizontal" role="form" action="${loginUrl}"
                        method="post">
                        <div class="form-group">
                            <label for="username">UserName</label> <input type="text"
                                class="form-control" id="username" name="username"
                                placeholder="Enter username">
                        </div>
                        <div class="form-group">
                            <label for="password">Password</label> <input type="password"
                                class="form-control" id="password" name="password"
                                placeholder="Password">
                        </div>
                        <div class="checkbox">
                            <label> <input type="checkbox"> Remember Me
                            </label>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary">Login</button>
                            <button type="button" class="btn btn-default">Cancel</button>
                            <span class="glyphicon glyphicon-lock"></span> <a
                                href="register.html" class="left">Inscrivez-vous</a>
                        </div>
                        <input type="hidden" name="${_csrf.parameterName}"
                            value="${_csrf.token}" />
                    </form>
                </div>
            </div>
        </section>
        <jsp:include page="InfoSite.jsp" />
        <jsp:include page="MostPopularTickets.jsp" />
        <jsp:include page="Footer.jsp" />
        <jsp:include page="CopyRight.jsp" />

        <spring:url value="/resources/js/jquery-1.11.3.min.js" var="jquery_url" />
        <script src="${jquery_url }" type="text/javascript"></script>
        <spring:url value="/resources/js/bootstrap.min.js" var="bootstrap_url" />
        <script src="${bootstrap_url }" type="text/javascript"></script>
        <spring:url value="/resources/js/formValidation.min.js"
            var="formValidation_url" />
        <script src="${formValidation_url}" type="text/javascript"></script>
        <spring:url value="/resources/js/bootstrap.min-validation.js"
            var="bootstrap.min-validation_url" />
        <script src="${bootstrap.min-validation_url}" type="text/javascript"></script>
        <spring:url value="/resources/js/script.js" var="script_url" />
        <script src="${script_url}" type="text/javascript"></script>
    </body>
    </html>

安全-config.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:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <security:http use-expressions="true" disable-url-rewriting="true"
        auto-config="true">
        <security:intercept-url pattern="/resources/**"
            access="permitAll()" />
        <security:intercept-url pattern="/Ligue1Pro"
            access="hasRole('ROLE_ADMIN')" />
        <security:intercept-url pattern="/registration"
            access="permitAll()" />
        <security:intercept-url pattern="/login"
            access="permitAll()" />
        <security:intercept-url pattern="/" access="permitAll()" />
        <security:form-login default-target-url="/" login-page="/login"
            authentication-failure-url="/login?login_error"
            always-use-default-target="true" password-parameter="username"
            username-parameter="password" />
        <security:csrf />
    </security:http>
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="user" authorities="ROLE_ADMIN"
                    password="user" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

MVC-config.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"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <mvc:default-servlet-handler />
    <mvc:annotation-driven />
    <context:component-scan base-package="com.footballreservation.controller" />
    <mvc:resources location="/resources/" mapping="/resources/**" />
    <mvc:view-controller path="/" view-name="home" />
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    </bean>
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

MainController.java

    package com.footballreservation.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller("MainController")
public class MainController {

    @RequestMapping("/,home")
    public String showHome() {
        return "home";
    }

    @RequestMapping(value = "/Ligue1Pro", method = RequestMethod.GET)
    public String showLigue1ProPage() {
        return "Ligue1Pro";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String showLoginPage() {
        return "Login";
    }


    @RequestMapping(value = "/registration", method = RequestMethod.GET)
    public String showregistrationPage() {
        return "Register";
    }

}

1 个答案:

答案 0 :(得分:0)

根据您提供的代码,问题在于您的spring安全配置文件(security-config.xml)

password-parameter="username"
username-parameter="password" 

尝试将其更改为

password-parameter="password"
username-parameter="username"