溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Security跳轉(zhuǎn)頁面失敗問題解決

發(fā)布時(shí)間:2020-10-10 15:27:33 來源:腳本之家 閱讀:222 作者:澤呀 欄目:編程語言

這篇文章主要介紹了Spring Security跳轉(zhuǎn)頁面失敗問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

今天新建SpringBoot項(xiàng)目練手,第一次添加了Spring Security.成功啟動(dòng)項(xiàng)目后發(fā)現(xiàn)與之前新建的項(xiàng)目有點(diǎn)不一樣,無論我怎么設(shè)置系統(tǒng)首頁,瀏覽器內(nèi)打開的都是登陸界面,如圖:

Spring Security跳轉(zhuǎn)頁面失敗問題解決

無論我怎么設(shè)置controller的跳轉(zhuǎn)路徑都不起作用,氣到撓頭!?。?/p>

經(jīng)過查閱各種資料發(fā)現(xiàn)可能是Spring Security權(quán)限控制的原因,于是著手配置控制權(quán)限。

新建SecurityConfig類進(jìn)行權(quán)限配置,代碼如下:

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: 配置一些資源或者鏈接的權(quán)限認(rèn)證
  • antMatchers:配置哪些資源或鏈接需要被認(rèn)證
  • permitAll:設(shè)置完全允許訪問的資源或者鏈接

添加上述權(quán)限設(shè)置后index頁面可以正常訪問

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI