【问题标题】:Spring Security refuses to serve Static content (Javascript files)Spring Security 拒绝提供静态内容(Javascript 文件)
【发布时间】:2021-06-19 11:38:54
【问题描述】:

我目前正在做一个学校项目,我正在尝试在 Spring Boot 项目上实现 Spring Security 的登录功能。

不幸的是,我在加载我的 Javascript 文件方面遇到了困难,在网上搜索了所有可能的解决方案后,我仍然没有找到任何问题的答案。

我有很多关于资源的重复代码,但我只是想覆盖我的所有基础。

这是我的 Spring Security 配置类:

@Configuration
@EnableWebMvc
public class ApplicationSecurityConfiguration extends WebSecurityConfigurerAdapter implements WebMvcConfigurer
{
@Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/**/*.js").permitAll()
                .antMatchers("/**.js").permitAll()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/*.js").permitAll()
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage("/login").permitAll()
                .and()
                .logout().invalidateHttpSession(true)
                .clearAuthentication(true)
                .logoutRequestMatcher(new AntPathRequestMatcher("logout"))
                .logoutSuccessUrl("/logout-success").permitAll();
    }

@Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/h2-console/**")
                .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/img/**", "/icon/**");
    }

这是我的 HTML 代码,我的目标是我的通用 /static/js/script.js 文件。该文件是资源/模板/文件夹中的索引

<script type="text/javascript" th:src="@{/js/script.js}"></script>

这是我的文件夹结构,我在其中放置了一个箭头来显示 script.js 文件的放置位置。

│   ├───com
│   │   └───adventurealley
│   │       └───aafcro
│   │           ├───authorization
│   │           ├───bootstrap
│   │           ├───controller
│   │           ├───model
│   │           ├───repository
│   │           ├───restcontroller
│   │           └───service
│   ├───static
│   │   ├───css
│   │   └───js -> script.js
└───templates -> index.html

不幸的是,根本不可能以任何形式访问 script.js 文件。无法在项目中使用 Javascript 是一件大事。

有没有人可以帮我找出问题所在?

提前致谢, 符文

【问题讨论】:

  • 试试localhost:8080/js/script.js可以直接访问Javascript文件吗?
  • 嗨,Eleftheria,不,我不能。 “[nio-8080-exec-4] o.s.web.servlet.PageNotFound : 没有 GET /js/script.js 的映射”
  • 由于您收到未找到的响应,这与 Spring Security 无关。您的意思是用@EnableWebSecurity 而不是@EnableWebMvc 来注释您的ApplicationSecurityConfiguration 类吗? @EnableWebMvc 注释将阻止提供静态资源的默认行为。看看这个问题了解更多详情stackoverflow.com/questions/24661289/…
  • 做到了!非常感谢你,Eleftheria——我有一种烦人的感觉,觉得这很明显而且很烦人。当然,确实如此。 ;-)

标签: javascript spring spring-security thymeleaf staticresource


【解决方案1】:

感谢@Eleftheria Stein-Kousathana 解决了这个问题。

需要@EnableWebSecurity,而不是@EnableWebMvc。

由于您收到未找到的响应,这与 Spring Security 无关。您是否 > 打算使用 @EnableWebSecurity > 而不是 @EnableWebMvc 来注释您的 ApplicationSecurityConfiguration 类? @EnableWebMvc 注释将阻止默认行为 > 用于提供静态资源。查看此问题以获取更多详细信息 stackoverflow.com/questions/24661289/ – Eleftheria Stein-Kousathana

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2011-10-14
    • 2018-05-23
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多