gpt4 book ai didi

java - Spring 安全 "Refused to execute script from ..."

转载 作者:IT老高 更新时间:2023-10-28 13:50:09 24 4
gpt4 key购买 nike

我正在我的 HTML 文件(thymeleaf 模板)中使用 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开发者控制台。

我的尝试:

  • 禁用 Spring Security=> 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 文件?

最佳答案

请查看 answer其他有类似问题的人。

如果你把js文件放在/js/目录下,应该不会出现那种MIME错误。
而且,对于 javascript 文件,最好禁用它们的安全性:

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

关于java - Spring 安全 "Refused to execute script from ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24726218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com