2014-03-01 37 views
-1

我的應用程序使用Spring Security,在啓動過程中崩潰。跟蹤應用程序的執行,我可以確認誤差在方法onStartup從類MainWebAppInitializer發生:基於Spring Security的Web應用程序在啓動過程中崩潰(NullPointerException)

public class MainWebAppInitializer implements WebApplicationInitializer { 

    /** 
    * Register and configure all Servlet container components necessary to power the web application. 
    */ 
    @Override 
    public void onStartup(final ServletContext sc) throws ServletException { 
     // Create the 'root' Spring application context 
     final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); 
     root.scan("com.spring.web.config"); 

     // Manages the lifecycle of the root application context 
     sc.addListener(new ContextLoaderListener(root)); 

     // Handles requests into the application 
     final ServletRegistration.Dynamic appServlet = sc.addServlet("horariolivreapp", new DispatcherServlet(new GenericWebApplicationContext())); 
     appServlet.setLoadOnStartup(1); 
     final Set<String> mappingConflicts = appServlet.addMapping("/"); 
     if (!mappingConflicts.isEmpty()) { 
      throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); 
     } 
    } 

} 

更多specificly,在一個NullPointerException觸發線

appServlet.setLoadOnStartup(1) 

發生錯誤。遵循它是我的配置文件,以供參考:

的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_3_0.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>HorarioLivre</display-name> 

    <!-- Spring MVC --> 
    <servlet> 
     <servlet-name>horariolivreapp</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>horariolivreapp</servlet-name> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
    </context-param> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>com.spring.web.config</param-value> 
    </context-param> 
    <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> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

</web-app> 

horariolivreap-servlet.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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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"> 

    <context:component-scan base-package="com.horariolivreapp.controller" /> 
    <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> 

webSecurityConfig.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xsi:schemaLocation=" 

http://www.springframework.org/schema/security 


http://www.springframework.org/schema/security/spring-security-3.1.xsd 


http://www.springframework.org/schema/beans 

     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 

    <http use-expressions="true"> 
     <intercept-url pattern="/login*" access="isAnonymous()" /> 
     <intercept-url pattern="/**" access="isAuthenticated()"/> 

     <form-login 
     login-page='/form_login.html' 
     login-processing-url="/usuario_login.html" 
     default-target-url="/usuario_start.html" 
     authentication-failure-url="/form_login" 
     always-use-default-target="true"/> 

     <logout logout-success-url="/login.html" /> 

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

查看此文件,有人可以找到此問題的原因?

UPDATE 1 這是我的控制器(DispatcherServlet的)類:

package com.horariolivreapp.controller; 

import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.ImportResource; 
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; 

import com.horariolivreapp.core.Sessao; 
import com.horariolivreapp.data.UsuarioDAO; 
@Controller 
public class HorarioLivreController { 

    private Sessao sessao; 

    @RequestMapping("/cadastra_evento") 
    public ModelAndView cadastra_evento() { 
     return null; 
    } 
    @RequestMapping(value="/listagem_evento", method=RequestMethod.GET) 
    public ModelAndView listagem_evento() { 
     return null; 
    } 
    @RequestMapping("/cadastra_horario") 
    public ModelAndView cadastra_horario() { 
     return null; 
    } 
    @RequestMapping("/listagem_horario") 
    public ModelAndView listagem_horario() { 
     return null; 
    } 
    @RequestMapping("/cadastra_usuario") 
    public ModelAndView cadastra_usuario() { 
     return null; 
    } 
    @RequestMapping("/listagem_usuario") 
    public ModelAndView listagem_usuario() { 
     return null; 
    } 
    @RequestMapping("/cadastra_tipo") 
    public ModelAndView cadastra_tipo() { 
     return null; 
    } 
    @RequestMapping("/cadastra_campo") 
    public ModelAndView cadastra_campo() { 
     return null; 
    } 
    @RequestMapping("/cadastra_autorizacao") 
    public ModelAndView cadastra_autorizacao() { 
     return null; 
    } 
    @RequestMapping("/usuario_perfil") 
    public ModelAndView usuario_perfil() { 
     return null; 
    } 
    @RequestMapping("/usuario_config") 
    public ModelAndView usuario_config() { 
     return null; 
    } 
    @RequestMapping(value="/usuario_login", method=RequestMethod.POST) 
    public ModelAndView usuario_login(@RequestParam("j_username") String username, @RequestParam("j_password") String password) { 
     UsuarioDAO usuario = new UsuarioDAO(username, password); 
     if(usuario.getUsuario() != null) { 
      this.sessao = new Sessao(usuario.getUsuario()); 
     } 
     return new ModelAndView("usuario_start","usuario",usuario.getUsuario()); 
    } 
    @Configuration 
    @ImportResource({ "classpath:webSecurityConfig.xml" }) 
    public class SecSecurityConfig { 
     public SecSecurityConfig() { 
      super(); 
     } 
    } 
} 
+0

爲什麼在初始化程序和'web.xml'中複製初始化(servlet + listener)。任何具體的原因或者是一個錯誤?順便說一句。這是空的理由 - http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#addServlet%28java.lang.String,%20java.lang.Class%29 –

+0

我只是請按照本文中的說明操作:http://www.javacodegeeks.com/2013/05/spring-security-login.html。我嘗試從Web中刪除初始化(servlet + listener)。xml,但現在應用程序被加載,但我收到一個錯誤** HTTP狀態404 -/HorarioLivre/**,與此詳細信息:**類型**狀態報告, **消息**/HorarioLivre /,**說明**請求的資源不可用。 –

+0

你如何定義你的Spring上下文有點不清楚。不知道根配置(你的一些'@ Configuration')是否會加載'webSecurityConfig.xml'。但最重要的是,您正在使用空上下文初始化調度程序servlet,因此它將永遠不會正確加載'horariolivreap-servlet.xml'。 –

回答

1

的NPE在這一點意味着appServlet爲空,這反過來又意味着,sc.addServlet(...)返回null

addServlet的Javadoc這樣說:

返回:可用於進一步配置這個servlet一個ServletRegistration對象,或者null如果ServletContext已經包含了與一個servlet的完整ServletRegistration給予servletName或者如果同一個servlet實例已經在同一個容器中註冊與本或其他ServletContext

現在您正在實例化Servlet對象,因此它以前不能註冊。但有可能是另一個Servlet具有相同的名稱...這是問題的可能的直接原因。

而事實上,它看起來像你有已經通過聲明它在web.xml文件中註冊一個名爲「horariolivreapp」的servlet。

+0

好吧,我如何對帕維爾說,我嘗試從初始化程序和web.xml中刪除初始化(servlet + listener)。從兩種方式進行後,應用程序已加載,但我收到錯誤404(也在上面的答案中進行了描述)。 –