代码之家  ›  专栏  ›  技术社区  ›  Vickie Jack

spring无法发送css和js资源

  •  0
  • Vickie Jack  · 技术社区  · 7 年前

    我将spring mvc和spring security用于web应用程序,并将tomcat用作web应用程序。我使用mvc:resources来处理资源请求。但chrome控制台显示以下错误:

    Failed to load resource: the server responded with a status of 404 ()
    Refused to execute script from 'http://localhost:8080/web-resources/jquery.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
    :8080/favicon.ico  Failed to load resource: the server responded with a status of 404 ()
    

    这张图片是我的项目结构:

    Project Structure

    我的网站。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"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
        <display-name>Archetype Created Web Application</display-name>
        <welcome-file-list>
            <welcome-file>login.jsp</welcome-file>
        </welcome-file-list>
        <!-- security config-->
        <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>
        <servlet>
            <servlet-name>spring-mvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring-mvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring-mvc-servlet.xml
                /WEB-INF/security.xml
            </param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <jsp-config>
            <jsp-property-group>
                <url-pattern>*.jsp</url-pattern>
                <page-encoding>UTF-8</page-encoding>
            </jsp-property-group>
        </jsp-config>
    </web-app> 
    

    还有我的spring mvc servlet。xml是:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <context:annotation-config/>
        <context:component-scan base-package="java"/>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        <mvc:annotation-driven/>
        <mvc:resources mapping="/web-resources/**" location="/web-resources" cache-period="31556926"/>
        <mvc:resources mapping="/favicon.ico" location="/web-resources" cache-period="31556926"/>
    </beans>
    

    和安全。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"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">
        <http auto-config="true">
            <intercept-url pattern="/user**" access="ROLE_USER"/>
            <intercept-url pattern="/" access="permitAll"/>
            <intercept-url pattern="/web-resources**" access="permitAll"/>
            <form-login
                    login-page="/login"
                    default-target-url="/user/index"
                    authentication-failure-url="/login?error"
                    username-parameter="username"
                    password-parameter="password"/>
            <logout logout-success-url="/login?logout"/>
            <!-- enable csrf protection -->
            <csrf/>
        </http>
        <authentication-manager>
            <authentication-provider>
                <user-service>
                    <user name="abcd" password="123456" authorities="ROLE_USER"/>
                </user-service>
            </authentication-provider>
        </authentication-manager>
    </beans:beans>
    

    以下是查看jsp文件:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
    
    <html>
    <body>
    <h1 id="banner">Login to Security Demo</h1>
    <form name="f" action="<c:url value='j_spring_security_check'/>"
          method="POST">
        <table>
            <tr>
                <td>Username:</td>
                <td><input type='text' name='j_username'/></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='j_password'></td>
            </tr>
            <tr>
                <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
                <td colspan='2'><input name="submit" type="submit">&nbsp;<input name="reset" type="reset"></td>
            </tr>
        </table>
    </form>
    <script type="text/javascript" src="web-resources/jquery.min.js"></script>
    </body>
    </html> 
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   flyingfox    7 年前

    mvc资源映射的设置似乎不正确。

    尝试更改

    <mvc:resources mapping="/web-resources/**" location="/web-resources" cache-period="31556926"/>
    

    <mvc:resources mapping="/web-resources/**" location="/web-resources/" cache-period="31556926"/>
    

    并将以下代码添加到spring security xml中:

    <http pattern="/web-resources/**" security="none"/>