【问题标题】:Spring Security is not loading js and css. (giving 404 error)Spring Security 没有加载 js 和 css。 (给出 404 错误)
【发布时间】:2016-01-18 21:03:56
【问题描述】:

我最近在我的 MVC 系统中实现了 Spring Security。实现此之后,JS 和 CSS 没有被加载(给出 404 错误)。

弹簧安全 XML:

<global-method-security secured-annotations="enabled" />
<http pattern="/**/*.css" security="none" />
<http pattern="/**/*.js" security="none" />
<http pattern="/**/*.png" security="none" />
<http pattern="/**/*.jpg" security="none" />
<http pattern="/**/*.gif" security="none" />
<http pattern="/resources/**" security="none" />

<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true" entry-point-ref="authenticationEntryPoint">

    <!-- Dashboard & resources -->
    <intercept-url pattern="/" access="permitAll" />
    <intercept-url pattern="/loginRequest**" access="permitAll" />
    <intercept-url pattern="/logout**" access="permitAll" />
    <intercept-url pattern="/dashboard**" access="permitAll" />
    <intercept-url pattern="/resources**" access="permitAll" />

    <!-- Incoming Product -->
    <intercept-url pattern="/incomingProduct**" access="hasRole('Administrator') and hasRole('Local_Administrator') and hasRole('Supervisor') and hasRole('Manager')" />

    <!-- Maintanence pages -->
    <intercept-url pattern="/depotUser**" access="hasRole('Administrator') and hasRole('Local_Administrator')" />
    <intercept-url pattern="/product**" access="hasRole('Administrator') and hasRole('Local_Administrator') and hasRole('Supervisor') and hasRole('Manager')" />
    <intercept-url pattern="/productOwner**" access="hasRole('Administrator') and hasRole('Local_Administrator') and hasRole('Supervisor') and hasRole('Manager')" />
    <intercept-url pattern="/storageTank**" access="hasRole('Administrator') and hasRole('Local_Administrator') and hasRole('Supervisor') and hasRole('Manager')" />

    <intercept-url pattern="/admin**" access="hasRole('Administrator')" />

    <!-- access denied page -->
    <access-denied-handler error-page="/error/403" />
    <form-login 
        login-page="/"
        login-processing-url="/loginRequest"
        default-target-url="/dashboard/home"
        authentication-failure-url="/loginPage?invalidLogin=Yes" 
        username-parameter="username"
        password-parameter="password"  
        />
    <logout logout-success-url="/logout" />
    <!-- enable csrf protection -->
    <csrf />

    <custom-filter before="FORM_LOGIN_FILTER" ref="authenticationFilter"/>
</http>

我在调度的 servlet XML 中定义了我的资源路径:

<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

我的 JS 和 CSS 如下所示:

<script src="./resources/plugins/jQuery/jQuery-2.1.4.min.js"></script>
<!-- Bootstrap 3.3.2 JS -->
<script src="./resources/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<!-- Bootstrap 3.3.4 -->
<link href="./resources/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

我认为我在这里忽略了一些错误。

谁能帮帮我?

谢谢,提前。

【问题讨论】:

  • 删除. &lt;script src="/resources/plugins/jQuery/jQuery-2.1.4.min.js"&gt;&lt;/script&gt;

标签: java spring spring-mvc spring-security


【解决方案1】:

通过在您的页面上添加这行代码来尝试使用 jstl 核心库。

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

而不是使用这个。

<script src="./resources/plugins/jQuery/jQuery-2.1.4.min.js"></script>

尝试使用它。

<script src="<c:url value="/resources/plugins/jQuery/jQuery-2.1.4.min.js"/>"></script>

【讨论】:

    【解决方案2】:

    我认为您在 &lt;mvc:resources .../&gt; 中指向的位置不正确。

    如果您所有的 js/css 文件都在 src\main\webapp\resources 目录下,则应该是 &lt;mvc:resources location="/resources/" mapping="/resources/**"/&gt;

    看到这个answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      相关资源
      最近更新 更多