2017-02-16 29 views
1

我正在使用spring安全性進行登錄操作,一切正常,但登錄頁面加載時沒有相應的樣式表文件。春季安全塊從jsp頁面加載css,js文件

這是我的spring-security.xml配置文件。

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security" 
     xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

     <security:http security="none" pattern="/resources/**"></security:http> 
     <security:http security="none" pattern="/resources/css/*"></security:http> 
     <security:http security="none" pattern="/resources/js/*"></security:http> 

     <security:http auto-config="true"> 
      <security:intercept-url pattern="/login" access="permitAll"/> 
      <security:intercept-url pattern="/entry.html" 
       access="hasRole('ROLE_USER')" /> 
      <security:form-login login-page="/login" 
       default-target-url="/entry.html" authentication-failure-url="/login?error" 
       username-parameter="username" password-parameter="password" /> 
      <security:logout logout-success-url="/login?logout"></security:logout> 
      <security:csrf/> 
     </security:http> 

     <security:authentication-manager> 
      <security:authentication-provider> 
       <security:user-service> 
        <security:user name="user" password="123456" 
         authorities="ROLE_USER"></security:user> 
       </security:user-service> 
      </security:authentication-provider> 
     </security:authentication-manager> 

    </beans> 

的login.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
    <html> 
    <head> 
    <title>Login Page</title> 
    <link rel="stylesheet" type="text/css" 
     href="<%=request.getContextPath()%>/resources/css/bootstrap.min.css"> 
    </head> 
    <body> 
     <div class="container"> 
      <div class="row"> 
       <div class="col-md-4"></div> 
       <div class="col-md-4"> 
        <div class="login-panel panel panel-default"> 
         <div class="panel-heading"> 
          <h3 class="panel-title">Login with Username and Password</h3> 
         </div> 
         <div class="panel-body"> 
          <c:if test="${not empty error}"> 
           <div class="error">${error}</div> 
          </c:if> 
          <c:if test="${not empty msg}"> 
           <div class="msg">${msg}</div> 
          </c:if> 
          <form name='loginForm' 
           action="<c:url value='j_spring_security_check' />" method="post"> 
           <fieldset> 
            <div class="form-group"> 
             <input class="form-control" placeholder="Username ..." 
              name="username" type="text" /> 
            </div> 
            <div class="form-group"> 
             <input class="form-control" placeholder="Password ..." 
              name="password" type="password" /> 
            </div> 
            <div> 
             <input type="submit" value="Login" class="btn btn-success" 
              name="submit" /> 
            </div> 
           </fieldset> 
           <input type="hidden" name="${_csrf.parameterName}" 
            value="${_csrf.token}" /> 
          </form> 
         </div> 
        </div> 
       </div> 
       <div class="col-md-4"></div> 
      </div> 
     </div> 
    </body> 
    </html> 

我不知道爲什麼它不加載CSS文件,我試圖提供所有可能的當局現有的資源文件夾中的文件,但沒有結果。

+1

嘗試直接瀏覽到CSS文件。 href是否有正確的路徑? –

+0

是的,我使用相同的方式訪問資源,它工作得很好,我遇到的問題是隻有** login.jsp ** –

回答

0

加入這一行的Spring Web配置

<mvc:resources mapping="/resources/**" location="/resources/" /> 

,並在你的JSP添加此行包括CSS

<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet"> 

添加此LIGNE春季安全 usign xml配置

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

<intercept-url pattern="/resources/**" filters="none"/> 

usign Java的配置

web.ignoring().antMatchers("/resources/**"); 
+1

沒有什麼改變,我一直得到相同的錯誤。 –

+0

它適用於我,請在url localhost:port/nameproject/resources/css/bootstrap.min.css中查看並顯示狀態碼http 403或404。 –