简体   繁体   English

Spring-Security MVC基本应用程序中的404错误

[英]404 Error in basic Spring-Security MVC Application

I'm getting crazy cause basic spring-mvc application with security module doesn't work and I have no idea what's wrong with it. 我发疯了,因为带有security module基本spring-mvc应用程序无法正常工作,我也不知道它有什么问题。

Let's start at the beginning, because I think there's the problem. 让我们从头开始,因为我认为存在问题。 I created a new maven basic project and did application like in some tutorial. 我创建了一个新的maven基本项目,并像一些教程中一样进行了应用。 Then I tried to run it on server, but there wasn't that possibility so I gave the project facets nature (or something like that) by eclipse and choose java web module in java facets to make this project possible to run on server. 然后我尝试在服务器上运行它,但是没有这种可能性,所以我通过eclipse赋予了项目方面(或类似的东西)性质,并在java facets中选择了Java Web模块,以使该项目可以在服务器上运行。 I have done that tutorial and when I try to run my basic spring security project on server I'm getting 404 error . 我已经完成了该教程,当我尝试在服务器上运行我的基本Spring Security项目时,出现404 error I'm getting that error even when accessing to basic path of application and /welcome 即使访问应用程序和/welcome基本路径,我也会收到该错误

Directory structure looks like that: 目录结构如下所示:

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

HelloController.java: 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: 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: 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>

spring-security.xml: 弹簧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: 为hello.jsp:

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

And console output, but there isn't any error stacktrace: 和控制台输出,但没有任何错误stacktrace:

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

Try to modify the context like <url-pattern>/web/*</url-pattern> , then insert /web to the URL you are using, like: http://localhost:8080/your-app-context/web/home 尝试修改诸如<url-pattern>/web/*</url-pattern>类的上下文,然后将/web插入您正在使用的URL中,例如: http:// localhost:8080 / your-app-context / web /家

Additionally, you might want to add an index.jsp file in /webapp , then redirect from there to your "views" inside /WEB-INF/views/ . 另外,您可能想要在/webapp添加index.jsp文件,然后从那里重定向到/WEB-INF/views/ “视图”。

File: index.jsp (make sure you have the JSTL library in your classpath to use this) 文件: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM