当前位置: 首页 > 知识库问答 >
问题:

Spring安全不允许静态内容

史懿轩
2023-03-14

在过去的几天里,我一直在和Spring保安公司战斗,所以我希望有人能在这里帮助我。

  • 我正在使用Spring Boot 1.2.5
  • 我使用的是Spring Actuator和Spring Remote Shell,它们已经从类路径中删除,认为它们可能会引起问题
  • 我排除了SecurityAutoConfigsion,因为它可能会导致我的问题

这是我的主要课程

@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class})
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

这是我的安全配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private AuthFailureHandler authFailureHandler;

    @Autowired
    private AuthSuccessHandler authSuccessHandler;

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint(new Http403ForbiddenEntryPoint())
                    .accessDeniedPage("/403")
                    .and()
                .authorizeRequests()
                    .antMatchers("/").permitAll()
                    .antMatchers("/about").permitAll()
                    .antMatchers("/login").permitAll()
                    .anyRequest().fullyAuthenticated()
                    .and()
                .formLogin()
                    .usernameParameter("sec-user")
                    .passwordParameter("sec-password")
                    .loginPage("/login")
                    .failureHandler(authFailureHandler)
                    .successHandler(authSuccessHandler)
                    .permitAll()
                    .and()
                .logout()
                    .deleteCookies("JESSIONID")
                    .invalidateHttpSession(true)
                    .permitAll();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(userDetailsService)
                .passwordEncoder(new BCryptPasswordEncoder());
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}

我的问题/问题是

>

  • CSS, JavaScript, Images,基本上没有静态内容会加载,我似乎不知道为什么

    是什么让事情变得更有趣,而不是得到一个403错误,这是我所期望的,它重定向到登录页面?我不想那样,它应该返回403,因为他们没有访问权限。

    我是这样从Thymeleaf调用我的静态资源的

    <link rel="stylesheet" media="screen" th:href="@{/css/main.css}" />
    

    在添加静态资源之前,我的安全性很好。

    我的静态文件位于resources/public/中。

    这对Spring Boot文档来说很好

    默认情况下,Spring Boot将提供来自类路径中名为 /static(或 /public或 /resources或 /META-INF/resources)的文件夹或ServletContext根的静态内容。

  • 共有1个答案

    柳胜
    2023-03-14

    尝试添加资源处理程序

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
        registry.addResourceHandler("/css/**")
                .addResourceLocations("classpath:/css/**");
        registry.addResourceHandler("/img/**")
                .addResourceLocations("classpath:/img/**");
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }
    
     类似资料:
    • 问题内容: 该应用程序使用JDK 8,Spring Boot和Spring Boot Jersey启动程序,并打包为WAR(尽管它是通过Spring Boot Maven插件在本地运行的)。 我想做的是将我在运行中(在构建时)生成的文档作为欢迎页面。 我尝试了几种方法: 通过按照此处所述配置 适当的init参数,让Jersey提供静态内容 引入以便将生成的HTML文档作为欢迎文件列出。 这些都没有

    • 该应用程序使用JDK8、Spring Boot和Spring Boot Jersey starter,并被打包为WAR(尽管它是通过Spring Boot Maven插件在本地运行的)。 我想做的是将我生成的文档(在构建时)作为欢迎页面。 我尝试了几种方法: 通过在中配置正确的init参数,让Jersey提供静态内容,如下所述 引入以便将生成的HTML文档列为欢迎文件。 这些都没有成功。 我希望避

    • 我从这个来源构建了一个基本的spring身份验证服务:https://spring.io/guides/gs/securing-web/ 试图使用stackoverflow上几乎所有的解决方案来包含本地文件夹中的JS文件,但是我做不到。当html页面加载时,它显示:< br >“未捕获的引用错误:未定义我的函数” 这是我的home.html剧本: 这是我的js文件所在的位置,htmls被放置在模板

    • 我将SpringMVC与Thymleaf和spring安全一起使用。我想加载一个页面使用Thymleaf模板,我可以加载我的静态资源。 例如,我想从template.html加载位于:static/img/theme/logo.png中的图片 我得到的是:结果 template.html: mvcConfig.java WebSecurityConfig: 源代码树

    • 我有一个带有context-path/services的Spring Boot应用程序。属性文件中的上下文路径项: 当我使用URL http://localhost:8080/services/和GET方法时,它工作正常,并返回静态index.html文件作为响应。但是,当我尝试使用相同的URL http://localhost:8080/services/和POST方法时,我会得到以下错误:

    • 我会直接去MCVE: GCC 7.2和7.1拒绝编译,错误如下: 您可以在没有任何标志的情况下复制,也可以使用