Spring Security拒绝申请css

时间:2018-03-12 02:41:24

标签: java spring security model-view-controller spring-security

我过去一直在研究多个Spring MVC项目,所有这些项目都有Spring Security没有问题,但这次我遇到了一个奇怪的问题。实现Spring Security时,它停止读取所有资源文件。我在StackOverflow上看到了关于这个主题的其他问题,但没有一个回答我的问题,而且大多数问题也都是几年之久。 以下是相关信息:

的web.xml:

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/dispatcher-servlet.xml,
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</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>

调度员的servlet:

    <context:component-scan base-package="com.hellospring"/>
    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
    <tx:annotation-driven/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

的applicationContext:

<?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/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 ">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/TetraNoodle"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.hellospring</value>
            </list>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <security:http auto-config="true">
        <security:intercept-url pattern="/admin/**" access="ROLE_USER" />
        <security:intercept-url pattern="/WEB-INF/resources/**" access="permitAll"/>
        <security:form-login
                login-page="/login"
                default-target-url="/"
                authentication-failure-url="/login?error"
                username-parameter="username"
                password-parameter="password"
        />
        <security:logout logout-success-url="/login?logout"/>

    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:jdbc-user-service
                    data-source-ref="dataSource"
                    authorities-by-username-query="SELECT username, authority FROM Authorities WHERE username = ?"
                    users-by-username-query="SELECT username, password, enabled FROM Users WHERE username=?"/>
        </security:authentication-provider>
    </security:authentication-manager>

</beans>
  • 我可以正常登录和注销,Spring Security运行良好,但是,当我启动应用程序时,没有任何资源文件可用。当我尝试以任何方式访问它们时,我被重定向到文件的URL,但是,然后我被提示输入凭据,这使我得出Spring Security不会的结论我访问文件。

我也受到了以下信息的欢迎:

Refused to apply style from 'http://localhost:8080/resources/css/bootstrap.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

我尝试将类型从'text / html'更改为'text / css',但它不会改变任何内容。 为什么会这样?以下是依赖项:

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>
      <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

1 个答案:

答案 0 :(得分:2)

我在使用错误消息拒绝CSS和JS文件时遇到了问题:

Refused to apply style because its MIME type ('text/html') is not a supported stylesheet MIME type

我设法通过注意Controller中的拼写错误来解决这个问题。而不是

@RequestMapping("/login")

我写道:

@RequestMapping

String参数添加到@RequestMapping为我解决了问题。