java - 能否將 MongoDB 作為 Shiro 的 realm 實現(xiàn)?
問題描述
我的需求是從數(shù)據(jù)庫中讀取用戶及權(quán)限信息,以完成認證和授權(quán)。Shiro 提供了 JdbcRealm 實現(xiàn),沒有 MongoDB 的 realm 實現(xiàn)。請問能否:
將 MongoDB 作為 Shiro 的 realm 實現(xiàn)?
如果可以,具體的配置該怎么寫?(Google 到一份具體實現(xiàn)代碼,但是缺少相關(guān)配置文件)
問題解答
回答1:謝邀, 你只需要實現(xiàn)自己的Realm就行, 比如:
public class MyRealm extends AuthorizingRealm { // 認證 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // TODO 從數(shù)據(jù)庫中獲取用戶信息, 從Mongo中查出來的 return null; } // 授權(quán) @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // TODO 從數(shù)據(jù)庫中獲取授權(quán)信息, 從Mongo中查出來的 return null; }}
然后把你自己的Realm設(shè)置到RealmSecurityManager中, 比如:
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();securityManager.setRealm(new MyRealm());
然后把這個SecurityManager設(shè)置到ShiroFilter中就行, 比如:
ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean();shiroFilterFactory.setSecurityManager(securityManager);
相關(guān)文章:
1. apache web server 怎么限制某一個網(wǎng)站對服務(wù)器資源的占用?2. docker網(wǎng)絡(luò)端口映射,沒有方便點的操作方法么?3. docker start -a dockername 老是卡住,什么情況?4. java中返回一個對象,和輸出對像的值,意義在哪兒5. css3 - 純css實現(xiàn)點擊特效6. mysql - 在不允許改動數(shù)據(jù)表的情況下,如何優(yōu)化以varchar格式存儲的時間的比較?7. 安全性測試 - nodejs中如何防m(xù)ySQL注入8. docker - 各位電腦上有多少個容器啊?容器一多,自己都搞混了,咋辦呢?9. javascript - 關(guān)于apply()與call()的問題10. html5 - 請問現(xiàn)在主流的前端自動化構(gòu)建工具是哪個?
