Spring Security跳轉頁面失敗問題解決
阿新 • • 發佈:2020-01-07
這篇文章主要介紹了Spring Security跳轉頁面失敗問題解決,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
今天新建SpringBoot專案練手,第一次添加了Spring Security.成功啟動專案後發現與之前新建的專案有點不一樣,無論我怎麼設定系統首頁,瀏覽器內開啟的都是登陸介面,如圖:
無論我怎麼設定controller的跳轉路徑都不起作用,氣到撓頭!!!
經過查閱各種資料發現可能是Spring Security許可權控制的原因,於是著手配置控制權限。
新建SecurityConfig類進行許可權配置,程式碼如下:
import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.password.NoOpPasswordEncoder; @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //配置資原始檔,其中/css/**、index可以任意訪問 http .authorizeRequests() .antMatchers("/css/**","/index").permitAll(); } }
一些解釋:
- authorizeRequests: 配置一些資源或者連結的許可權認證
- antMatchers:配置哪些資源或連結需要被認證
- permitAll:設定完全允許訪問的資源或者連結
新增上述許可權設定後index頁面可以正常訪問
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。