-1

I've been working on multiple Spring MVC projects in the past, with all of them having Spring Security without a problem, but I've ran into a peculiar problem this time. When implementing Spring Security, it stopped reading all resources files. I have seen the other questions on this topic on StackOverflow, and none of them answer my question, and most of them are also a few years old. Here's the relevant information:

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>

dispatcher-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>
  • I can login and logout just fine, and Spring Security works well, however, when I launch the application, none of the resource files are available. When I try to access them in any way, I am redirected to the URL of the files, however, I am then prompted to input credentials, which leads me to the conclusion that Spring Security doesn't let me access the files.

I am also greeted with the message:

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.

I've tried changing the type from 'text/html' to 'text/css' but it doesn't change anything. Why is this happening? Here are the dependencies:

 <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>
David Misic
  • 81
  • 3
  • 11

1 Answers1

2

I've had a problem with the CSS and JS files being refused with the error message:

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

I managed to fix this by noticing a typo in my Controller. Instead of

@RequestMapping("/login")

I wrote:

@RequestMapping

Adding the String parameter to the @RequestMapping fixed the problem for me.

David Landup
  • 169
  • 1
  • 16