404基本Spring-Security MVC应用程序出错

时间:2015-09-06 16:25:09

标签: java maven spring-mvc spring-security

我变得疯狂导致基本的spring-mvc申请security module无法运作,我不知道它有什么问题。

让我们从头开始,因为我认为存在问题。我创建了一个新的maven基础项目,并在一些教程中做了应用程序。然后我试着在服务器上运行它,但是没有这种可能性所以我通过eclipse给了项目facets nature(或类似的东西)并在java facets中选择java web模块以使这个项目可以在服务器上运行。我已经完成了那个教程,当我尝试在服务器上运行我的基本Spring安全项目时,我得到了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-调度-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

1 个答案:

答案 0 :(得分:0)

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

此外,您可能希望在index.jsp中添加/webapp文件,然后从那里重定向到您的&#34;视图&#34;在/WEB-INF/views/内。

文件:index.jsp(确保您的类路径中有JSTL库才能使用它)

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