2015-09-06 102 views
1

我越来越疯狂基本spring-mvc应用程序与security module不起作用,我不知道它有什么问题。基本的Spring-Security MVC应用程序中的404错误

让我们从头开始,因为我认为存在问题。我创建了一个新的Maven基础项目,并在一些教程中做了应用程序。然后我尝试在服务器上运行它,但没有那种可能性,所以我通过eclipse给项目facets自然(或类似的东西),并在java facets中选择java web模块以使该项目可以在服务器上运行。我已经完成了这个教程,当我尝试在服务器上运行我的基本弹簧安全项目时,我得到了404 error。我得到这个错误甚至访问应用程序的基本路径和/welcome

目录结构时,看起来像这样:

src 
-main 
--webapp 
---WEB-INF 
----web.xml 
----spring-security.xml 
----mvc-dispatcher-servlet.xml 
----views 
-----hello.jsp 
-----admin.jsp 

HelloController.java:

@Controller 
public class HelloController { 

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

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

     return model; 

    } 

} 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 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_2_5.xsd"> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

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

    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

mvc-dispatcher-servlet.xml:

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

    <annotation-driven /> 
    <context:component-scan base-package="pl.tutorial.security" /> 

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



</beans:beans> 

弹簧security.xml文件:

<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="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/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-3.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <mvc:annotation-driven /> 
    <context:component-scan base-package="pl.tutorial.security" /> 

    <http auto-config="true"> 
     <intercept-url pattern="/admin**" access="ROLE_USER" /> 
    </http> 

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

</beans:beans> 

的hello.jsp:

<%@page session="false"%> 
<html> 
<body> 
    <h1>Title : ${title}</h1> 
    <h1>Message : ${message}</h1> 
</body> 
</html> 

和控制台输出,但没有任何错误堆栈跟踪:

wrz 06, 2015 6:11:36 PM org.apache.tomcat.util.digester.SetPropertiesRule begin 
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Application' did not find a matching property. 
wrz 06, 2015 6:11:36 PM org.apache.tomcat.util.digester.SetPropertiesRule begin 
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Test' did not find a matching property. 
wrz 06, 2015 6:11:36 PM org.apache.tomcat.util.digester.SetPropertiesRule begin 
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SecurityTutorial' did not find a matching property. 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Server version:  Apache Tomcat/8.0.23 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Server built:   May 19 2015 14:58:38 UTC 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Server number:   8.0.23.0 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: OS Name:    Windows 8.1 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: OS Version:   6.3 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Architecture:   amd64 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Java Home:    C:\Program Files\Java\jre1.8.0_60 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: JVM Version:   1.8.0_60-b27 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: JVM Vendor:   Oracle Corporation 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: CATALINA_BASE:   C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: CATALINA_HOME:   C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcatalina.base=C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcatalina.home=C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dwtp.deploy=C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\wtpwebapps 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\endorsed 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dfile.encoding=Cp1250 
wrz 06, 2015 6:11:36 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent 
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_60\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_60/bin/server;C:/Program Files/Java/jre1.8.0_60/bin;C:/Program Files/Java/jre1.8.0_60/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\xampp\php;C:\Program Files\MySQL\MySQL Server 5.1\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.1\bin;C:\Users\Adrian\Desktop\eclipse;;. 
wrz 06, 2015 6:11:36 PM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["http-nio-8081"] 
wrz 06, 2015 6:11:36 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 
INFO: Using a shared selector for servlet write/read 
wrz 06, 2015 6:11:36 PM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["ajp-nio-8010"] 
wrz 06, 2015 6:11:36 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 
INFO: Using a shared selector for servlet write/read 
wrz 06, 2015 6:11:36 PM org.apache.catalina.startup.Catalina load 
INFO: Initialization processed in 1452 ms 
wrz 06, 2015 6:11:36 PM org.apache.catalina.core.StandardService startInternal 
INFO: Starting service Catalina 
wrz 06, 2015 6:11:36 PM org.apache.catalina.core.StandardEngine startInternal 
INFO: Starting Servlet Engine: Apache Tomcat/8.0.23 
wrz 06, 2015 6:11:40 PM org.apache.jasper.servlet.TldScanner scanJars 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
wrz 06, 2015 6:11:40 PM org.apache.catalina.core.ApplicationContext log 
INFO: No Spring WebApplicationInitializer types detected on classpath 
wrz 06, 2015 6:11:40 PM org.apache.catalina.core.ApplicationContext log 
INFO: Initializing Spring root WebApplicationContext 
wrz 06, 2015 6:11:40 PM org.springframework.web.context.ContextLoader initWebApplicationContext 
INFO: Root WebApplicationContext: initialization started 
wrz 06, 2015 6:11:40 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing Root WebApplicationContext: startup date [Sun Sep 06 18:11:40 CEST 2015]; root of context hierarchy 
wrz 06, 2015 6:11:40 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/root-context.xml] 
wrz 06, 2015 6:11:41 PM org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFilters 
INFO: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning 
+0

我想你可能想尝试没有“安全”至上;然后一旦你有一切工作,就应用它。也可以看看[这个项目](https://bitbucket.org/vadimvera/spring-mvc)。 –

+0

是的,你是对的。我只是尝试过,没有安全的代码部分也无法工作。那么会出现什么问题呢? – Kavv

+0

如果您正在使用eclipse,请尝试清理您的项目。项目>清洁>清洁项目 –

回答

0

尝试修改上下文,如<url-pattern>/web/*</url-pattern>,然后插入/web的URL您使用,如:http://localhost:8080/your-app-context/web/home

此外,你可能想从那里添加index.jsp文件/webapp,然后重定向到你的“意见”中/WEB-INF/views/

文件:index.jsp(请确保您有JSTL库在你的classpath使用此)

<%@ page contentType="text/html;charset=UTF-8" %> 
<%@ page pageEncoding="UTF-8" language="java" %> 

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta charset="UTF-8"> 
    </head> 
    <body> 
    <c:redirect url="/web/hello" /> 
    </body> 
</html> 
+0

感谢您的建议,但第一个解决方案没有解决问题,我希望我的应用程序以标准方式工作 – Kavv