CVC-复杂type.3.2.2:属性“用户名参数”是不允许出现在元素“的形式登陆”

问题描述:

我是新来的春天的安全性。目前我正在学习如何使用spring-security进行登录。请帮帮我。我已经提供了我的web.xml,Dispatcher-servlet.xml,login.jsp,spring-security.xml文件。我在spring-security.xml中有一些错误。CVC-复杂type.3.2.2:属性“用户名参数”是不允许出现在元素“的形式登陆”

朋友我请你向我提供使用弹簧安全一个完整的工作login程序。

错误如下:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [spring-security.xml] 
Offending resource: ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 21 in XML document from ServletContext resource [/WEB-INF/spring-security.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 36; cvc-complex-type.3.2.2: Attribute 'username-parameter' is not allowed to appear in element 'form-login'. 
org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70) 

我的DispatcherServlet如下:

<?xml version='1.0' encoding='UTF-8' ?> 
    <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/security 
http://www.springframework.org/schema/security/spring-security-3.2.xsd"> 
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 
<context:annotation-config /> 
<context:component-scan base-package="com" /> 
<mvc:annotation-driven /> 

<!-- 
Most controllers will use the ControllerClassNameHandlerMapping above, but 
for the index controller we are using ParameterizableViewController, so we must 
define an explicit mapping for it. 
--> 
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <props> 
      <prop key="index.htm">indexController</prop> 
     </props> 
    </property> 
</bean> 

<bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" 
     p:suffix=".jsp" /> 

<!-- 
The index controller. 
--> 
<bean name="indexController" 
      class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
     p:viewName="index" /> 
<import resource="spring-security.xml"/> 
</beans> 

我的弹簧security.xml文件如下:

<b:beans xmlns:b="http://www.springframework.org/schema/beans" 
xmlns="http://www.springframework.org/schema/security" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
<http auto-config='true'> 
    <intercept-url pattern="/admin**" access="ROLE_USER" /> 
    <form-login 
     login-page="/login" 
     default-target-url="/welcome" 
     authentication-failure-url="/login?error" 
     username-parameter="username" 
     password-parameter="password" /> 
    <logout logout-success-url="/login?logout" /> 
     </http> 

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
      <user name="mkyong" password="123456" authorities="ROLE_USER" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 
</beans:beans> 

我的网站。 xml文件如下:

<?xml version="1.0" encoding="UTF-8"?> 
<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>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 
</web-app> 

我的login.jsp如下:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@page session="true"%> 
<html> 
<head> 
<title>Login Page</title> 
<style> 
.error { 
padding: 15px; 
margin-bottom: 20px; 
border: 1px solid transparent; 
border-radius: 4px; 
color: #a94442; 
background-color: #f2dede; 
border-color: #ebccd1; 
} 

.msg { 
padding: 15px; 
margin-bottom: 20px; 
border: 1px solid transparent; 
border-radius: 4px; 
color: #31708f; 
background-color: #d9edf7; 
border-color: #bce8f1; 
} 

#login-box { 
width: 300px; 
padding: 20px; 
margin: 100px auto; 
background: #fff; 
-webkit-border-radius: 2px; 
-moz-border-radius: 2px; 
border: 1px solid #000; 
} 
</style> 
</head> 
<body onload='document.loginForm.username.focus();'> 
<h1>Spring Security Custom Login Form (XML)</h1> 
<div id="login-box"> 
    <h3>Login with Username and Password</h3> 
    <c:if test="${not empty error}"> 
     <div class="error">${error}</div> 
    </c:if> 
    <c:if test="${not empty msg}"> 
     <div class="msg">${msg}</div> 
    </c:if> 
    <form name='loginForm' 
     action="<c:url value='/j_spring_security_check' />" method='POST'> 
     <table> 
      <tr> 
       <td>User:</td> 
       <td><input type='text' name='username'></td> 
      </tr> 
      <tr> 
       <td>Password:</td> 
       <td><input type='password' name='password' /></td> 
      </tr> 
      <tr> 
       <td colspan='2'><input name="submit" type="submit" 
        value="submit" /></td> 
      </tr> 
     </table>   
     </form> 
    </div> 
</body> 
</html> 
+0

您正在使用哪个Spring安全版本 –

+0

我正在使用spring-security-core-3.1.2.RELEASE.jar和spring-security-config-3.0.5.RELEASE.jar – Paawn12

+0

某些内容与xsd定义冲突表单登录。你能检查是否没有其他安全jar(不同版本)被包含在内。你是否在使用任何maven/gradle进行依赖管理 –

我的问题的答案是下面的XML架构:

<beans:beans xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns="http://www.springframework.org/schema/security" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/security 
http://www.springframework.org/schema/security/spring-security-3.2.xsd 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/util  
http://www.springframework.org/schema/util/spring-util-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

尝试下面的配置使用弹簧security.xml文件

<bean:beans xmlns:b="http://www.springframework.org/schema/beans" 
xmlns="http://www.springframework.org/schema/security" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 
<http auto-config='true'> 
    <intercept-url pattern="/admin**" access="ROLE_USER" /> 
    <form-login 
     login-page="/login" 
     default-target-url="/welcome" 
     authentication-failure-url="/login?error" 
     username-parameter="username" 
     password-parameter="password" /> 
    <logout logout-success-url="/login?logout" /> 
     </http> 

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
      <user name="mkyong" password="123456" authorities="ROLE_USER" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 
</beans:beans> 

我已经更新你的配置在所有XSD使用版本模式

+0

正如您所说更新配置后发生以下错误:来自ServletContext资源的XML文档中的第10行[/WEB-INF/spring-security.xml]无效;嵌套异常是org.xml.sax.SAXParseException; lineNumber:10; columnNumber:118;元素“bean:beans”的前缀“bean”未绑定。 \t org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70) – Paawn12

+0

哦有在第一线拼写错误。使用'

+0

我照你说的去做。还有另一个问题。那是我做了xmlns:b而不是xmlns:beans。感谢朋友的帮助。这个问题已经解决,但现在还有另一个问题。当我在login.jsp页面提交用户名和密码时,它说“请求的资源不可用”。请帮帮我。 – Paawn12