由于MIME类型,第一次启动时未加载样式表

时间:2018-07-04 22:05:17

标签: java html css java-ee spring-security

..这是在最近启动项目时第一次发生。 首次登录并进行注销后,样式将根据需要继续工作。 为什么会这样呢? 附言对不起,我的英语不好

这是浏览器控制台显示的内容:

  

auth:1拒绝应用“ http://localhost:8080/auth”中的样式   因为它的MIME类型('text / html')不是受支持的样式表MIME   类型,并启用严格的MIME检查。

SecurityConfiguration:

package config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
    @Autowired
    DataSource dataSource;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().dataSource(dataSource).
            usersByUsernameQuery("select login,password,true from user where login=?").
             authoritiesByUsernameQuery("select login, role from user where login=?");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http    .authorizeRequests()
                .antMatchers("/auth", "/reg").permitAll()
                .antMatchers("/admin/**").hasAuthority("admin")
                .anyRequest().authenticated().and()
                .formLogin()
                .loginPage("/auth").usernameParameter("login")
                .permitAll()
                .and()
                .logout().logoutSuccessUrl("/auth");

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**");
    }
}

Authorization.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Authorization</title>
    <c:set var="root" value="${pageContext.request.contextPath}"/>
    <link type="text/css" rel="stylesheet" href="styles.css"/>
</head>
<body>
<div class="container">
    <div class="mainblock">
        <input type="hidden"
               name="${_csrf.parameterName}"
               value="${_csrf.token}"/>

        <form method="post" action="/auth">
            User Name : <input type="text" name="login">
            Password : <input type="password" name="password">
            <input type="hidden"
                   name="${_csrf.parameterName}"
                   value="${_csrf.token}"/>
<input name = "submit" value = "Authorize" type = "submit" />
        </form>

        <p>Did not create an account?

            <a href="/reg">Registration</a>
        </p>
    </div>
</div>
</body>
</html>

样式文件与Authorizatiom.jsp一起位于“网络”包中

1 个答案:

答案 0 :(得分:0)

所有这些都是由于我的粗心和在configure(WebSecurity web)中指定的指定错误目录所致。我需要添加“供应商/ **” 我也定义了内容路径:<link rel = "stylesheet" href = "$ {pageContext.request.contextPath} /vendor/css/styles.css">