【问题标题】:Spring Security then deploy -> 404 errorSpring Security 然后部署-> 404 错误
【发布时间】:2016-07-23 11:11:18
【问题描述】:

我正在开始 Spring MVC 项目。
我使用 Spring 安全性。

我添加了:(web.xml)

<i>


    <display-name>Archetype Created Web Application</display-name>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
            /WEB-INF/security-context.xml
        </param-value>

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

    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>



    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

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



我添加了:pom.xml

<i>


    <modelVersion>4.0.0</modelVersion>
    <groupId>test4</groupId>
    <artifactId>test4</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>test4 Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!--
              SpringFramework-context
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>


        <!--
              SpringFramework-webmvc
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>


        <!--
            SpringFramework-webmvc portlet
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc-portlet</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>


        <!--
             SpringSecurity-web
        -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>


        <!--
             SpringSecurity-core
        -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>


        <!--
            SpringSecurity-config
        -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>



    </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>


    <properties>
        <javaVersion>1.8</javaVersion>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
</i>


最后添加(security-context.xml)

<br>

<i>

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

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="guest" password="guest" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>
<./


<br>
and my folder structure is <br>
[enter image description here][1]


  [1]: http://i.stack.imgur.com/Vlzsx.png

构建成功..
但是 404 错误。/..
我哪里做错了?

【问题讨论】:

  • 解释构建后会发生什么。你是怎么得到这个错误的?

标签: java spring spring-mvc spring-security settings


【解决方案1】:

web.xml 中更改这一行:

 <url-pattern>*.do</url-pattern>

到:

 <url-pattern>/</url-pattern>

并在 security-context.xml 中进行更改:

<http auto-config='true'>
    <intercept-url pattern="/**" access="isAuthenticated()" />
</http>

希望对您有所帮助。

【讨论】:

  • 没有。仍然会出现 404 错误页面。还是不行。
  • 您是否有一个@Controller 可以响应您系统中的地址/
  • 是的。 @Controller public class TestController { @RequestMapping("/") public ModelAndView indexPage(){ ModelAndView mav = new ModelAndView(); mav.setViewName("index"); mav.addObject("调用", "gi");返回mav; } @RequestMapping("/hello") public ModelAndView hello() { ModelAndView mav = new ModelAndView(); mav.setViewName("你好"); mav.addObject("调用", "gi");返回mav; }
  • 我不能完全帮助您,因为您需要在您的网络中追踪确切的问题。如果您发现任何可以显示的错误,请说明它可以帮助社区解决问题。查看我编辑的更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 2014-04-06
  • 2015-06-13
  • 1970-01-01
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多