Spring Security“拒绝执行来自...的脚本”

2024-04-29

我正在 HTML 文件(thymeleaf 模板)中使用 Spring Security 和 Bootstrap 构建 Spring MVC 应用程序。 Spring Security部分基于Spring Guide对于春季安全 http://spring.io/guides/gs/securing-web/并与 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 Security => bootstrap css 再次工作,但我需要安全性
  • 在 spring 论坛上搜索,但有一个循环链接,没有解决方案
  • 添加资源处理程序,但我看不到处理程序被调用,错误也消失
  • 添加资源路径permit call

我的目录结构:

/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);
    }
}

安全配置.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 https://stackoverflow.com/a/24920752/966698其他有类似问题的人。

如果你把js文件放在/js/dir,不应该有那种MIME错误。
并且,对于 javascript 文件,最好禁用它们的安全性:

@Override
public void configure(WebSecurity web) throws Exception {
  web.ignoring().antMatchers("/the_js_path/**");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Security“拒绝执行来自...的脚本” 的相关文章

随机推荐

  • 将静态链接的 elf 二进制文件转换为动态链接的

    我有一个 elf 二进制文件 它已静态链接到 libc 我无权访问其 C 代码 我想使用 OpenOnload 库 它在用户空间中实现了套接字 因此与标准 libc 版本相比提供了更低的延迟 OpenOnload 实现标准套接字 api 并
  • CSS 网格行垂直溢出其容器

    我想在页面上有一个网格布局 其中网格延伸到整个视口 并且行具有最小高度 最简单的示例是具有单个单元格的网格 请参见下面的代码片段 我遇到的问题是 当视口的高度小于定义的最小行高时 该行垂直溢出其容器 通过在下面的示例中添加红色和绿色边框 可
  • iPhone 相机访问权限?

    我想知道如何访问 iPhone 相机并实时使用它 例如 仅在相机视图上绘图 另一个相关问题 可以显示吗同时 4 个摄像机视图就像 Mac 上的 Photo Booth 一样 您可以使用 AVFoundation 来做到这一点 void in
  • 从布伦特里汇款

    我使用 Braintree 作为我的网站的付款方式 收款时没问题 但现在我想将钱转入特定客户帐户 不退款 请帮我 谢谢你 Full disclosure I work at Braintree If you have any further
  • 如何让wildfly localhost连接自动变成https?

    我需要在本地主机上使用 https 协议测试我的 Web 应用程序 我怎样才能在wildfly上配置设置以使https localhost 8443 myapp html works New 我将其添加到我的安全领域
  • 使用 Google 地图对 geoJson 文件中的点进行自定义标记

    我使用 GeoJSON 作为 Google 地图的数据源 我使用 API v3 创建数据层 如下所示
  • Node.js 中的缓冲区是什么?

    正如您可以在有关 Buffer 类的 Node js 文档 http nodejs org api buffer html 一个缓冲区 类似于整数数组 但对应于 V8 堆外部的原始内存分配 到目前为止 一切都很好 现在让我困惑的是 从技术上
  • 查找其他列表项中列表项的列表索引

    我有一个长字符串列表 我想获取与另一个列表中的字符串子字符串匹配的列表元素的索引 使用列表理解可以轻松检查列表项是否包含列表中的单个字符串 例如这个问题 https stackoverflow com questions 4843158 c
  • AJAX 加载 WordPress 内容

    我一直在遵循 AJAX 教程来尝试将我的 WordPress 帖子内容加载到我网站的主页上 而无需重新加载页面 我不知道为什么 但是当单击链接时 它仍然导航到页面 而不是将内容加载到我指定的 div 中 不管怎样 这对我来说有点太多了 我希
  • [UIScreen mainScreen].bounds 与 [UIApplcation sharedApplication].keyWindow.bounds?

    我认为我想覆盖整个屏幕 我想将其框架设置为覆盖整个屏幕 浏览堆栈溢出 我发现设置视图框架覆盖屏幕的两种不同方法 UIScreen mainScreen bounds UIApplcation sharedApplication keyWin
  • 为什么 XDebug 忽略 NetBeans 6.8 中的断点?

    我在 Ubuntu 10 04 笔记本电脑上运行 PHP 5 3 2 Apache 2 2 14 和 xdebug 2 2 0rc1 并且尝试在 Netbeans 6 8 中的本地主机上设置调试 我的问题是我在 Netbeans 中设置的断
  • 尝试从 assetForURL:resultBlock 内部分配 __block ALAsset 时出错:

    我正在尝试创建一个方法 该方法将返回给定资产 url 的 ALAsset 我需要稍后上传资产 并希望在结果块之外执行此操作 ALAsset assetForPhoto Photo photo ALAssetsLibrary library
  • 如何在 NSUserDefaults 中存储 Swift 枚举值

    我有一个这样的枚举 enum Environment case Production case Staging case Dev 我想在 NSUserDefaults 中保存一个实例 如下所示 func saveEnvironment en
  • 如何使用 forEach 删除列表中的元素?

    var people alex jason matt people forEach function p if p length gt 4 REMOVE THIS PERSON or pop it out of the list or wh
  • 从恐慌中恢复的程序未按预期退出

    根据我的理解 当恐慌恢复时 我期望程序退出并表现出正常行为 但事实并非如此 我期望最后一行打印 程序结束 是正确的吗 如果出现运行时错误 它不会被打印 是吗 package main import fmt func main defer f
  • 最快的 SQL Server 协议?

    最快的 SQL Server 连接协议是什么 相关 哪些协议可以远程使用 哪些可以本地使用 这会影响最快协议的选择吗 VIA 这是最快的 SQL 协议 它在专用硬件上运行 并用于执行 SQL Server 基准测试记录 请注意 VIA 协议
  • 从 Spark-Shell (pyspark) 查询 Spark 流应用程序

    我正在关注这个example http cdn2 hubspot net hubfs 438089 notebooks spark2 0 Structured 20Streaming 20using 20Python 20DataFrame
  • 将 CVS 升级到 git/hg 的技巧?

    我们仍然使用 CVS 我使用 git 和 hg 供我个人使用 尽管我对这两个工具仍然是新手 但我意识到它们更现代 更好 更快 分布式等 只是每个人都如此习惯 CVS 以至于我觉得如果我是推荐并实际将我们当前的 CVS 服务器升级 移植 转换
  • 如何根据 XML 参数更新表

    我有一个表 我想根据 XML 参数中的值更新其 varchar 字段之一 我有下表 ID Constraint Value 1 OldVal 1 OldVal 2 2 OldVal 2 OldVal 1 我想使用以下 XML 来更新Cons
  • Spring Security“拒绝执行来自...的脚本”

    我正在 HTML 文件 thymeleaf 模板 中使用 Spring Security 和 Bootstrap 构建 Spring MVC 应用程序 Spring Security部分基于Spring Guide对于春季安全 http s