无法在 Spring Security 中加载静态内容 [英] Unable to load static content in spring security

查看:52
本文介绍了无法在 Spring Security 中加载静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个来源构建了一个基本的 spring 身份验证服务:

这是我的 mvcConfig 代码:

包你好;导入 org.springframework.context.annotation.Configuration;导入 org.springframework.web.servlet.config.annotation.ViewControllerRegistry;导入 org.springframework.web.servlet.config.annotation.WebMvcConfigurer;导入 org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;@配置公共类 MvcConfig 实现了 WebMvcConfigurer {public void addViewControllers(ViewControllerRegistry 注册表) {registry.addViewController("/home").setViewName("home");registry.addViewController("/").setViewName("home");registry.addViewController("/hello").setViewName("重定向:http://localhost:3000/home.html");registry.addViewController("/login").setViewName("login");}@覆盖public void addResourceHandlers(ResourceHandlerRegistry registry) {if (!registry.hasMappingForPattern("/webjars/**")) {registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}if (!registry.hasMappingForPattern("/**")) {registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/");}registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");}}

WebSecurityConfig 代码:

包你好;导入 org.springframework.context.annotation.Bean;导入 org.springframework.context.annotation.Configuration;导入 org.springframework.security.config.annotation.web.builders.HttpSecurity;导入 org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;导入 org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;导入 org.springframework.security.core.userdetails.User;导入 org.springframework.security.core.userdetails.UserDetails;导入 org.springframework.security.core.userdetails.UserDetailsS​​ervice;导入 org.springframework.security.provisioning.InMemoryUserDetailsManager;@配置@启用网络安全公共类 WebSecurityConfig 扩展了 WebSecurityConfigurerAdapter {@覆盖protected void configure(HttpSecurity http) 抛出异常 {http.authorizeRequests().antMatchers("/", "/home","/resources/**").permitAll().anyRequest().authenticated().and().formLogin().loginPage("/登录").permitAll().and().注销().permitAll();}@豆@覆盖公共 UserDetailsS​​ervice userDetailsS​​ervice() {用户详细信息用户 =User.withDefaultPasswordEncoder().username("用户").password("密码").roles("用户").build();返回新的 InMemoryUserDetailsManager(user);}

}

解决方案

WebSecurityConfig 类中,您将 permitAll 设置为仅 '/', '/home''/resources/**'.匿名用户无需安全检查即可访问这三个端点.

对于 test.js 文件,src 指向当前 url 中的 test.js.因此,当您在 localhost 上运行它时,浏览器会尝试将 test.js 查找为 http://localhost:{port}/{current-page-url}/test.js

例如,如果页面位于 /home 下,则浏览器调用 http://localhost:8080/home/test.js,但正如您在WebSecurityConfig 除了 /home 本身之外的任何调用都将被 Spring Security 阻止.(/home/home/** 不一样)

所以你需要做的是将src url改为<script src="/resources/test.js"></script> 因为下的任何东西/resources/** 端点可以被任何人访问,并且它已经在 MvcConfig

的 resourceHandler 配置中注册

 registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/");

希望这有帮助!快乐编码:)

添加:

此外,在

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆