首页 文章

Spring安全性不允许静态内容

提问于
浏览
2

过去几天我一直在和Spring Security打架,所以我希望有人可以帮助我 .

  • 我正在使用Spring Boot 1.2.5

  • 我使用的是Spring Actuator和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,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 回答

  • 0

    尝试添加资源处理程序 .

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

相关问题