【发布时间】:2014-09-03 18:15:20
【问题描述】:
我正在我的 HTML 文件(thymeleaf 模板)中使用 Spring Security 和 Bootstrap 构建一个 Spring MVC 应用程序。 Spring Security 部分基于 Spring Guide for Spring Security 并结合了 Spring Boot 应用服务器。
启用 Spring Security 后,引导 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开发者控制台。
我的尝试:
- 禁用 Spring 安全性 => bootstrap css 再次工作,但我需要安全性
- 在spring论坛上搜索,但是有一个循环链接没有解决办法
- 添加资源处理程序,但我看不到处理程序被调用,错误也消失了
- 将资源路径添加到
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");
}
}
示例.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 资源目录加载 css/js 文件?
【问题讨论】:
-
我也遇到了同样的问题,你找到解决办法了吗?
-
这是我的答案:[如何设置 Spring Security 以允许 webjars][1] [1]:stackoverflow.com/a/31893880/912829
标签: java spring twitter-bootstrap spring-mvc spring-security