【问题标题】:Using Shiro Guice with jdbcRealm将 Shiro Guice 与 jdbcRealm 一起使用
【发布时间】:2015-01-01 19:29:51
【问题描述】:

我是 Guice 和 Shiro 的新手,我正在尝试将它与我的 DB (h2) 一起使用。 我读过这个:click

但正如他们所说,它只适用于用户和角色部分,这对我来说没用。

我的 shiro.ini 正在工作,我设法在没有 Guice 部分的情况下创建用户、登录和注销。

MyShiroModule

public class MyShiroModule extends ShiroModule{

protected void configureShiro() {
    try {
        bindRealm().toConstructor(IniRealm.class.getConstructor(Ini.class));
    } catch (NoSuchMethodException e) {
        addError(e);
    }
}

@Provides
Ini loadShiroIni() {
    return Ini.fromResourcePath("classpath:shiro.ini");
}
}

和我的模块:

公共类模块扩展 AbstractModule {

@Singleton

protected void configure() {
    Injector injector = Guice.createInjector(new MyShiroModule());
    SecurityManager securityManager = injector.getInstance(SecurityManager.class);
    SecurityUtils.setSecurityManager(securityManager);        
}
}

正如他们在教程中所说的那样。 我必须添加什么才能使用我的 shiro.ini 的 [main] 部分?

【问题讨论】:

    标签: java jdbc guice shiro


    【解决方案1】:

    我从来没有让 JDBC 领域与 Guice 一起工作,因为正如您所指出的,它只读取用户和组部分,无论出于何种原因。我最终没有使用 Shiro.ini,只是像这样自己创建了JdbcRealm

    public class ShiroAuthModule extends ShiroModule {
    
      @Override
      public void configure() {
        super.configure();
        // Bind your data source however you need to - I use JNDI 
        // but it would be easy to switch to a properties file.
        bind(Context.class).to(InitialContext.class);
        bind(DataSource.class).toProvider(JndiIntegration.fromJndi(DataSource.class, "java:/comp/env/jdbc/security"));
      }
    
      @Provides
      @Singleton
      JdbcRealm loadJdbcRealm(Ini ini, DataSource ds, 
          @Named("shiro.authenticationQuery") String authenticationQuery,
          @Named("shiro.userRolesQuery") String roleQuery,
          @Named("shiro.permissionsQuery") String permissionQuery) {
        JdbcRealm realm = new JdbcRealm();
        realm.setAuthenticationQuery(authenticationQuery);
        realm.setUserRolesQuery(roleQuery);
        realm.setPermissionsQuery(permissionQuery);
        realm.setPermissionsLookupEnabled(true);
        realm.setDataSource(ds);
        return realm;
      }
    
      @Override
      protected void configureShiro() {
        // shiro.properties should be on your classpath and 
        // contain the named properties in loadJdbcRealm
        Properties properties = Module.loadProperties(this, "shiro.properties");
        Names.bindProperties(binder(), properties);
        try {
          bindRealm().to(JdbcRealm.class);
        } catch (SecurityException e) {
          addError(e);
        }
      }
    
    } 
    

    【讨论】:

      猜你喜欢
      • 2014-03-03
      • 2018-04-18
      • 2018-06-17
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      相关资源
      最近更新 更多