Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Security不允许静态内容_Spring_Spring Mvc_Spring Security_Spring Boot - Fatal编程技术网

Spring Security不允许静态内容

Spring Security不允许静态内容,spring,spring-mvc,spring-security,spring-boot,Spring,Spring Mvc,Spring Security,Spring Boot,在过去的几天里,我一直在与Spring Security进行斗争,所以我希望有人能在这里帮助我 我使用的是Spring Boot 1.2.5 我使用的是Spring执行器和Spring Remote Shell,它们已经从类路径中删除,认为它们可能会导致问题 我排除了SecurityAutoConfiguration,因为它可能导致我的问题 这是我的主课 @SpringBootApplication(exclude = {org.springframework.boot.autoconfig

在过去的几天里,我一直在与Spring Security进行斗争,所以我希望有人能在这里帮助我

  • 我使用的是Spring Boot 1.2.5
  • 我使用的是Spring执行器和Spring Remote Shell,它们已经从类路径中删除,认为它们可能会导致问题
  • 我排除了SecurityAutoConfiguration,因为它可能导致我的问题
这是我的主课

@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、图像,基本上不会加载静态内容,我似乎不明白为什么

  • 是什么让事情变得更有趣,它没有得到我所期望的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的根目录提供静态内容


    尝试添加资源处理程序

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
        registry.addResourceHandler("/css/**")
                .addResourceLocations("classpath:/css/**");
        registry.addResourceHandler("/img/**")
                .addResourceLocations("classpath:/img/**");
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }
    

    为什么要将css/js/图像映射为授权请求下的URL?你不应该在那里列出它们,你应该将它们添加到你的资源处理程序中,让spring来完成这项工作。我使用的是spring Boot,我的理解是它已经通过它的自动配置完成了这项工作,因为在我添加安全性之前,它运行良好,除非我不理解你。它们位于/resources/public中,spring boot会自动映射“.authorizeRequests().antMatchers(“/css/**”、“js/**”、“images/**”)。permitAll()是多余的代码,不应在其中列出。您基本上是告诉系统希望/css是一个有效的url来访问。您仍然可以在启动时使用资源处理程序。但是,摆脱这一行,给它一个旋转我更新了我的配置,但它仍然不工作。当请求/css/main.css时,它会返回作为登录页面的HTML。您究竟是如何加载资源的?你能显示视图代码吗