首頁  >  文章  >  Spring 安全性阻止對樣式的訪問

Spring 安全性阻止對樣式的訪問

WBOY
WBOY轉載
2024-02-22 13:19:061178瀏覽

php小編香蕉為您帶來最新的Java問答內容:Spring框架中的安全性如何阻止對樣式的存取?這是一個涉及到Web應用程式安全性的重要問題。在使用Spring框架開發網路應用程式時,如何有效地保護樣式檔案不被未授權存取是一個需要重視的問題。本文將為您詳細解答這個問題,幫助您更好地提升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中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除