2016-06-01 12 views
0

Ich bin neu in der Federsicherung. Momentan versuche ich zu lernen, wie man sich mit Spring-Security einloggen kann. Bitte hilf mir. Ich habe meine Dateien web.xml, Dispatcher-servlet.xml, login.jsp und spring-security.xml bereitgestellt. Ich denke da ist ein Fehler in der spring-security.xml.cvc-complex-type.3.2.2: Attribut 'Benutzername-Parameter' darf nicht im Element 'Formular-Login' erscheinen

Freunde Ich bitte Sie, mir ein komplettes funktionierendes Login-Programm mit Feder-Sicherheit zur Verfügung zu stellen.

Fehler ist wie folgt:

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) 

Mein DispatcherServlet wie folgt:

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

Meine feder security.xml Datei wie folgt:

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

My Web. Die XML-Datei lautet wie folgt:

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

Mein login.jsp ist wie folgt:

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

Welche Spring-Sicherheitsversion verwenden Sie –

+0

Ich verwende Spring-Security-Core-3.1.2.RELEASE.jar und Spring-security-config-3.0.5.RELEASE.jar – Paawn12

+0

Etwas ist im Konflikt mit Xsd-Definition von Formularanmeldung können Sie überprüfen, ob kein anderes Sicherheitsglas (mit anderer Version) enthalten ist. Verwenden Sie irgendeinen Maven/Gradle für Abhängigkeitsmanagement –

Antwort

1

Die Antwort meines Problems folgt xml Schema:

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

Versuchen unter Konfiguration für feder 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> 

ich Ihre Konfiguration Version verwenden in allen XSD aktualisiert zu verwenden Schemas

+0

Nach dem Aktualisieren der Konfiguration tritt folgender Fehler auf, wie Sie bereits erwähnt haben: Zeile 10 im XML-Dokument von der ServletContext-Ressource [/WEB-INF/spring-security.xml] ist ungültig. verschachtelte Ausnahme ist org.xml.sax.SAXParseException; Zeilennummer: 10; Spaltennummer: 118; Das Präfix "bean" für das Element "bean: beans" ist nicht gebunden. \t org.springframework.beans.factory.parsing.FailFastProblemReporter.error (FailFastProblemReporter.java:70) – Paawn12

+0

oh gibt es einen Tippfehler in der ersten Zeile. Verwenden Sie '

+0

Ich tat, wie Sie sagten. Es gab auch ein anderes Problem. Das heißt, ich hatte xmlns: b statt xmlns: bohnen. Danke Freund für deine Hilfe. Dieses Problem ist gelöst, aber jetzt gibt es ein anderes Problem. Wenn ich den Benutzernamen und das Passwort auf der login.jsp-Seite einreiche, wird "Die angeforderte Ressource ist nicht verfügbar" angezeigt. Bitte hilf mir. – Paawn12

Verwandte Themen