首頁  >  問答  >  主體

java - 能否將 MongoDB 作為 Shiro 的 realm 實作?

我的需求是從資料庫讀取使用者及權限信息,以完成認證和授權。 Shiro 提供了 JdbcRealm 實現,沒有 MongoDB 的 realm 實作。
請問能否:

  1. 將 MongoDB 作為 Shiro 的 realm 實作?

  2. 如果可以,具體的配置該怎麼寫? (Google 到一份具體實現程式碼,但是缺少相關設定檔)

大家讲道理大家讲道理2688 天前527

全部回覆(1)我來回復

  • 怪我咯

    怪我咯2017-05-17 10:00:54

    謝邀, 你只需要實現自己的Realm就行, 例如:

    public class MyRealm extends AuthorizingRealm {
    
      // 认证
      @Override
      protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    
        // TODO 从数据库中获取用户信息, 从Mongo中查出来的
        return null;
      }
    
      // 授权
      @Override
      protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    
        // TODO 从数据库中获取授权信息, 从Mongo中查出来的
        return null;
      }
    }

    然後把自己的Realm设置到RealmSecurityManager中, 例如:

    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    securityManager.setRealm(new MyRealm());

    然後把這個SecurityManager设置到ShiroFilter中就行, 例如:

    ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean();
    shiroFilterFactory.setSecurityManager(securityManager);

    回覆
    0
  • 取消回覆