java - 能否將 MongoDB 作為 Shiro 的 realm 實(shí)現(xiàn)?
問題描述
我的需求是從數(shù)據(jù)庫(kù)中讀取用戶及權(quán)限信息,以完成認(rèn)證和授權(quán)。Shiro 提供了 JdbcRealm 實(shí)現(xiàn),沒有 MongoDB 的 realm 實(shí)現(xiàn)。請(qǐng)問能否:
將 MongoDB 作為 Shiro 的 realm 實(shí)現(xiàn)?
如果可以,具體的配置該怎么寫?(Google 到一份具體實(shí)現(xiàn)代碼,但是缺少相關(guān)配置文件)
問題解答
回答1:謝邀, 你只需要實(shí)現(xiàn)自己的Realm就行, 比如:
public class MyRealm extends AuthorizingRealm { // 認(rèn)證 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // TODO 從數(shù)據(jù)庫(kù)中獲取用戶信息, 從Mongo中查出來的 return null; } // 授權(quán) @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // TODO 從數(shù)據(jù)庫(kù)中獲取授權(quán)信息, 從Mongo中查出來的 return null; }}
然后把你自己的Realm設(shè)置到RealmSecurityManager中, 比如:
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();securityManager.setRealm(new MyRealm());
然后把這個(gè)SecurityManager設(shè)置到ShiroFilter中就行, 比如:
ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean();shiroFilterFactory.setSecurityManager(securityManager);
相關(guān)文章:
1. docker gitlab 如何git clone?2. html5 - css not選擇器3. javascript - 關(guān)于this的一個(gè)疑問4. python - (2006, ’MySQL server has gone away’)5. javascript - ios下獲取焦點(diǎn)失敗6. html - 為什么我給div設(shè)置display:inline然后設(shè)置height還是有效呢7. 這種數(shù)據(jù)怎么合并啊?8. service mysql.server start 啟動(dòng)失敗9. 怎么用css截取字符?10. php - 淘寶訂單拆單表設(shè)計(jì)
