java - 能否將 MongoDB 作為 Shiro 的 realm 實(shí)現(xiàn)?
問(wèn)題描述
我的需求是從數(shù)據(jù)庫(kù)中讀取用戶及權(quán)限信息,以完成認(rèn)證和授權(quán)。Shiro 提供了 JdbcRealm 實(shí)現(xiàn),沒(méi)有 MongoDB 的 realm 實(shí)現(xiàn)。請(qǐng)問(wèn)能否:
將 MongoDB 作為 Shiro 的 realm 實(shí)現(xiàn)?
如果可以,具體的配置該怎么寫(xiě)?(Google 到一份具體實(shí)現(xiàn)代碼,但是缺少相關(guān)配置文件)
問(wè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中查出來(lái)的 return null; } // 授權(quán) @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // TODO 從數(shù)據(jù)庫(kù)中獲取授權(quán)信息, 從Mongo中查出來(lái)的 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. 怎么在phpstudy中用phpexcel上傳數(shù)據(jù)到MYSQL?2. javascript - 百度搜索網(wǎng)站,如何讓搜索結(jié)果顯示一張圖片加上一段描述,如圖;求教3. phpadmin的數(shù)據(jù)庫(kù),可以設(shè)置自動(dòng)變化時(shí)間的變量嗎?就是不需要接收時(shí)間數(shù)據(jù),自動(dòng)變化4. html5和Flash對(duì)抗是什么情況?5. 求救一下,用新版的phpstudy,數(shù)據(jù)庫(kù)過(guò)段時(shí)間會(huì)消失是什么情況?6. html - 爬蟲(chóng)時(shí)出現(xiàn)“DNS lookup failed”,打開(kāi)網(wǎng)頁(yè)卻沒(méi)問(wèn)題,這是什么情況?7. mac里的docker如何命令行開(kāi)啟呢?8. boot2docker無(wú)法啟動(dòng)9. 這是什么情況???10. gosts內(nèi)容是空的
