2016-08-17 4 views
0

пытается сделать пользовательскую проверку входа в системе с помощью пружинной безопасности, как я стартер я не знаю, ошибка ниже мой код, структура проекта, файлы банка и ошибказаказ Войти проверка Spring Security

My Project Structure and Jars

Ошибка:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) 
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) 
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) 
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) 
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:605) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:509) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4760) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) 
    ... 21 more 

Aug 17, 2016 2:56:56 PM org.apache.catalina.core.StandardContext startInternal 
SEVERE: Error listenerStart 
Aug 17, 2016 2:56:56 PM org.apache.catalina.core.StandardContext startInternal 
SEVERE: Context [/CustomLoginValidation] startup failed due to previous errors 
Aug 17, 2016 2:56:56 PM org.apache.catalina.core.ApplicationContext log 
INFO: Closing Spring root WebApplicationContext 
Aug 17, 2016 2:56:56 PM org.apache.catalina.core.StandardContext listenerStop 
SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener 
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext 
    at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170) 
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1000) 
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:976) 
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:928) 
    at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:583) 
    at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116) 
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4801) 
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5401) 
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

web.xml

<?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>spring_mvc</display-name> 
    <welcome-file-list><welcome-file>/WEB-INF/jsp/index.jsp</welcome-file> </welcome-file-list> 
    <!-- Single Servlet --> 
    <servlet> 
     <servlet-name>mvc</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <!-- create ds , only app start.. --> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
      /WEB-INF/spring-config.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>mvc</servlet-name> 
     <!-- map all action to ds --> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Spring Security Configuration --> 
    <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> 

весна-config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
    xsi:schemaLocation=" 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.xsd"> 


    <context:component-scan base-package="org.pradeep.demo" /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/"></property> 
     <property name="suffix" value=".jsp"></property> 
    </bean> 
    <import resource="SpringSecurity.xml"/> 
</beans> 

SpringSecurity.xml

<?xml version="1.0" encoding="UTF-8"?> 
<bean:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:bean="http://www.springframework.org/schema/beans" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     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://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" /> 
     <!-- enable csrf protection --> 
     <csrf/> 
    </http> 

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

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<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 (Annotation)</h1> 

    <div id="login-box"> 

     <h2>Login with Username and Password</h2> 

     <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='user' value=''></td> 
      </tr> 
      <tr> 
       <td>Password:</td> 
       <td><input type='password' name='pass' /></td> 
      </tr> 
      <tr> 
        <td colspan='2'> 
           <input name="submit" type="submit" value="submit" /> 
           </td> 
      </tr> 
      </table> 

      <input type="hidden" 
        name="${_csrf.parameterName}" value="${_csrf.token}" /> 
     </form> 
    </div> 

</body> 
</html> 

класс контроллера:

package org.pradeep.demo; 

import org.springframework.stereotype.Controller; 
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 HelloController { 

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

     ModelAndView model = new ModelAndView(); 
     model.addObject("title", "Spring Security Custom Login Form"); 
     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 Custom Login Form"); 
     model.addObject("message", "This is protected page!"); 
     model.setViewName("admin"); 

     return model; 

    } 

    //Spring Security see this : 
    @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; 

    } 

} 

ответ

1

Ошибка сама по себе довольно понятна: FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml].

Вы настроили файл XML для DispatcherServlet, но не настроили корневой контекст, поэтому Spring ContextLoaderListener пытается загрузить исходный контекст из стандартного /WEB-INF/applicationContext.xml.

В любом случае, вы не должны объявлять конфигурацию безопасности Spring в контексте приложения DispatcherServlet, но в корневом. Безопасность Spring в реализованных фильтрах и фильтрах запускается до вызова DispatcherServlet. Вы должны удалить <import resource="SpringSecurity.xml"/> из файла Spring-config.xml и добавьте applicationContext.xml файл в WEB-INF для него, в котором вы позже добавить службу и сохраняемости бобы, которые делают не зависят от DispatcherServlet:

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
    xsi:schemaLocation=" 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.xsd"> 

     <!-- non web spring beans will go here --> 
     <import resource="SpringSecurity.xml"/> 
</beans> 

В качестве альтернативы вы можете сказать ContextLoaderListener для загрузки файла SpringSecurity.xml из файла web.xml:

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/SpringSecurity.xml</param-value> 
</context-param> 
Смежные вопросы