Spring安全性阻止css,js文件被加载到jsp页面上

时间:2017-02-16 14:17:33

标签: java spring spring-mvc spring-security

我正在使用spring安全性进行登录操作,一切正常但登录页面没有相应的样式表文件。

这是我的 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 个答案:

答案 0 :(得分:0)

将此行添加到spring web config

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

并在你的jsp中为include css添加这一行

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

添加此ligne以确保弹簧安全 usign xml config

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

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

usign java config

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