使用Spring Security進行使用者許可權控制

2021-08-08 04:56:30 字數 2541 閱讀 4797

系統比較簡單,所以只有使用者表, 角色表和域表, 沒有許可權表. 各個域的使用者是隔離的, 因為前後端分離, 控制起來格外不方便, 不過好歹是用filter實現了,bug還很多改天再說.

現在的問題就是登入 ,前端獲取登入使用者以及許可權攔截 .其中比較花時間的是使用者登入.

登入可以使用authenticationprovider 或者userdetailsservice實現,我傾向於使用前者,看起來diy程度比較高,下面是**:

@component

public class cloudinsauthprovider implements authenticationprovider

if(users.size()==0)

cloudinsuserdetail dbuser = new cloudinsuserdetail(users.get(0));

//加密過程在這裡體現

if (!password.equals(dbuser.getpassword()))

collection<? extends grantedauthority> authorities = dbuser.getauthorities(roledao);

return new usernamepasswordauthenticationtoken(dbuser, password, authorities);

}@override

public boolean

supports(class<?> aclass)

}

其中用到的user需要實現userdetails介面,其中的屬性資訊我寫在父類中:

public class cloudinsuserdetail extends cloudinsuser implements userdetails 

public

cloudinsuserdetail(cloudinsuser user)

@deprecated

@override

public collection<? extends grantedauthority> getauthorities()

public collection<? extends grantedauthority> getauthorities(yarnjooqdao,long> roledao)

@override

public string getpassword()

@override

public string getusername()

@override

public boolean

isaccountnonexpired()

@override

public boolean

isaccountnonlocked()

@override

public boolean

iscredentialsnonexpired()

@override

public boolean

isenabled()

}

之後就是核心的websecurityconfigureradapter ,在這裡選擇使用者登入時的驗證方式,以及攔截配置:

@configuration

//@enablewebsecurity: 禁用boot的預設security配置,配合@configuration啟用自定義配置

// (需要擴充套件websecurityconfigureradapter)

@enablewebsecurity

//@enableglobalmethodsecurity(prepostenabled = true): 啟用security註解,

// 例如最常用的@preauthorize

以上定義了使用authenticationprovider 進行使用者驗證,對很多靜態資源不做攔截,其他url需要登入,某些url需要特定角色才能開啟,登入頁面是/user_login,登入表單提交url是/login_process. 於是我們就完成了簡單的使用者分級許可權控制,關於具體的mvc方法,還能用註解單獨控制:

, method = requestmethod.delete

)public responseentity, object>> removeuser(@requestparam long id)

許可權管理(一)初識spring security

pom檔案 security元件 org.springframework.boot spring boot starter security org.springframework.boot spring boot starter web 熱部署使用 org.springframework.boot...

使用GraphEdit使用

1 註冊元件。其實乙個filter就是乙個com元件,所以使用之前需要註冊,可以有兩種方法對元件進行註冊。1.直接使用命令。命令列下輸入 regsvr32 hqtlystd.ax 編譯之後你會在工程目錄下的debug中找到hqtlystd.ax,這個就是要用的filter 即可註冊成功。2.vc6....

MySQL使用學習使用 mysql學習使用

1 mysql學習 1 安裝 ubuntu下直接安裝 apt get install mysql server 2 檢查伺服器是否啟動 sudo netstat tap grep mysql,如果啟動成功,出現以下資訊 tcp00localhost.localdomain mysql listen ...