php小编香蕉为您带来最新的java问答内容:spring框架中的安全性如何阻止对样式的访问?这是一个涉及到web应用程序安全性的重要问题。在使用spring框架开发web应用时,如何有效地保护样式文件不被未授权访问是一个需要重视的问题。本文将为您详细解答这个问题,帮助您更好地提高web应用程序的安全性。
我遇到了一个问题,在某些时候我的 spring 项目中的某些样式停止工作。我在css文件中使用了bootstrap样式和我亲自编写的样式。 由于某种原因启动程序后,仅显示其中的一部分。当在google开发者工具中查看“网络”时,我发现样式文件由于某种原因被重定向到http://localhost:8080/login,尽管我可以访问样式文件。 (https://i.stack.imgur.com/ggort.png)
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((requests) -> requests
.requestMatchers("/", "/reg", "/registration/**", "/static/**", "/static/images/**", "/static/styles/**").permitAll()
.anyRequest().authenticated()
)
.formLogin((form) -> form
.loginPage("/login")
.failureUrl("/login_error")
.defaultSuccessUrl("/pc")
.permitAll()
)
.logout((logout) -> logout.permitAll().logoutSuccessUrl("/"));
return http.build();
}发现这一点后,我决定注释掉 pom.xml 中的 spring security 依赖项,并注释掉所有使用它的类。 之后,样式开始工作,并且当 spring security 未注释时一切都继续工作。然而,当我重新启动计算机并开始项目时,我又遇到了同样的问题。 这可能与什么有关以及如何解决? 我非常需要你的帮助!
您需要告诉 Spring Security 忽略 src/main/resources/static 下的资源。您可以通过将 http.authorizeRequests((requests) -> requests..requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()) 添加到安全配置中并删除所有 "/static/ 来实现此目的。 .. 来自现有配置的引用。
以上就是Spring 安全性阻止对样式的访问的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号