gpt4 book ai didi

java - 将 Shiro Guice 与 jdbcRealm 结合使用

转载 作者:行者123 更新时间:2023-11-30 11:12:40 24 4
gpt4 key购买 nike

我是 Guice 和 Shiro 的新手,我正在尝试将它与我的数据库 (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");
}
}

和我的模块:

公共(public)类模块扩展了 AbstractModule {

@Singleton

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

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

最佳答案

我从来没有让 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);
}
}

}

关于java - 将 Shiro Guice 与 jdbcRealm 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26760036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com