【发布时间】: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