【问题标题】:Spring Security "Refused to execute script from ..."Spring Security“拒绝从...执行脚本”
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    同样的文件夹结构我也有同样的错误,它在 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()

    【讨论】:

    • 这对我有用,因为我遇到了 sam 问题,但适用于 CSS
    • 这对我有用。谢谢
    【解决方案3】:

    我用这个解决了我的问题,你可以试试这个

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

    希望对您有所帮助。

    【讨论】:

      【解决方案4】:

      我遇到了这个问题,并且挣扎了一段时间。我创建了一个应该匹配每个“不匹配”路由的路由,并发送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

      所以解决办法就是自定义路线。

      【讨论】:

        【解决方案5】:

        向控制器添加新方法后,我遇到了同样的错误。

        我忘记为@GetMapping 注释提供一个值,而@Controller 本身没有绑定到任何URL。奇怪的是,在我的方法中添加 @GetMapping("/foo") 解决了这个问题。我还没有弄清楚为什么会这样,但我打算找出答案。

        【讨论】:

          【解决方案6】:

          当我们使用 "permitall()" 或 anyrequest() 时,它会启用 Mime 检查,因此我指定所有用于访问的 url 列表。

          【讨论】:

            【解决方案7】:

            我从 css 文件的开头删除了所有 cmets,它可以工作。

            【讨论】:

              【解决方案8】:

              使用 Security Web Configuration 将静态资源文件导入 Spring MVC 的清单:

              1. 在从 WebSecurityConfigurerAdapter 扩展的配置方法中允许模式匹配器
                  @Override
                      protected void configure(HttpSecurity http) throws Exception {
                          
                          http
                              .authorizeRequests()
                              .antMatchers("/resources/**").permitAll()
                              .anyRequest().authenticated()
                              .and()
                              .formLogin().loginPage("/loginPage").loginProcessingUrl("/authenticateUser")
                              .permitAll();
                      }
              
              1. 通过实现 WebMvcConfigurer 在配置中映射资源位置
              @Override
                  public void addResourceHandlers(ResourceHandlerRegistry registry) {
              
                      registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
                  }
              
              1. 创建资源文件夹和子文件夹来存储静态文件。
              /src/main/webapp/
                  |-> resources
                    | -> css
                      | -> style.css
              
              1. 将资源添加到 JSP 文件中
               <link rel="stylesheet" type="text/css" href="css/style.css">
              
              1. 请记住,在控制器文件中始终指定路径,即使主页 @RequestMapping("/")

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2018-06-23
                • 2019-01-27
                • 2020-02-12
                • 2019-09-20
                • 1970-01-01
                • 2014-07-14
                • 1970-01-01
                相关资源
                最近更新 更多