我在我的HTML文件(百万美元模板)中使用Spring Security和Bootstrap构建Spring MVC应用程序。 Spring Security部分基于Spring Guide for Spring Security并与Spring Boot应用程序服务器结合使用。
启用Spring Security后,bootstrap css文件无法加载错误消息:
Refused to execute script from 'http://localhost:8080/js/bootstrap.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
上面的错误消息来自chrome开发者控制台。
我尝试过的内容:
permit
调用我的目录结构:
/main
|-> /java
| BootStart.java
|-> /security
|SecurityConfiguration.java
|-> /resources
|-> /static
|-> /css /** bootstrap location */
|-> /js
|-> /fonts
|-> /templates
| /user
| sample.html
BootStart.java是Spring Boot获取的java文件。
BootStart.java:
@EnableAutoConfiguration
@ComponentScan
public class BootStart {
public static void main(String[] args) {
SpringApplication.run(BootStart.class, args);
}
}
SecurityConfiguration.java:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
http
.authorizeRequests()
.antMatchers("/", "/resources/static/**").permitAll()
.anyRequest().authenticated();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
Sample.html:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}" href="../../css/bootstrap.min.css"/>
<link rel="stylesheet" th:href="@{/css/bootstrap-theme.css}" href="../../css/bootstrap-theme.min.css"/>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div class="alert alert-danger" role="alert">!Basic template!</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
目前我在我的pom中使用以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
如何配置Spring Security,我可以从我的/ static resources目录加载css / js文件?
答案 0 :(得分:17)
请用其他类似问题查看此answer。
如果将js文件放在/js/
目录中,则不应该出现那种MIME错误
而且,对于javascript文件,最好禁用它们的安全性:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/the_js_path/**");
}
答案 1 :(得分:6)
我使用相同的文件夹结构也有同样的错误,它在css文件中。
我所做的只是在antMatcher()中添加/css/**
,如下所示:
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
.antMatchers("/css/**").permitAll() //Adding this line solved it
.anyRequest().fullyAuthenticated()
.and()
.formLogin()
.loginPage("/login").permitAll()
.loginProcessingUrl("/sign-in")
.defaultSuccessUrl("/home")
.failureUrl("/error")
.and()
.logout()
.logoutSuccessUrl("/logout")
.permitAll()
.and()
.csrf().disable();
}
在你的情况下,只需在antMatcher()中添加/js/**
,然后使用permitAll()
答案 2 :(得分:0)
将新方法添加到控制器后,我遇到了同样的错误。
我忘了为@GetMapping
注释提供值,而@Controller
本身并未绑定到任何网址。很好地将@GetMapping("/foo")
添加到我的方法中解决了这个问题。我还没弄清楚为什么会这样,但我打算找出来。
答案 3 :(得分:0)
我用它来解决问题,您可以尝试
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**");
}
希望有帮助。
答案 4 :(得分:0)
当我们使用“ permitall()”或 anyrequest()时,它将启用Mime检查,因此我指定了所有以前属于accecs的URL列表。
答案 5 :(得分:0)
我从css文件的开头删除了所有注释,并且可以正常工作。
答案 6 :(得分:0)
我遇到了这个问题,挣扎了一段时间。我已经创建了一条路线,该路线应与每条“不匹配”的路线相匹配,并发送index.html
。
@Controller
public class DefaultPageController {
@Autowired
private LogService logService;
@GetMapping("/*")
public String defaultPage(Model model){
logService.saveLog(Log.LogType.INFO, null, "Default route");
return "index";
}
}
问题在于,它覆盖了路由/file.js
,并且没有发送静态文件,但始终发送index.html
。
因此解决方案仅是自定义路线。