Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何配置shiro域以连接到struts2应用程序中的oracle数据库_Java_Oracle_Struts2_Jboss_Shiro - Fatal编程技术网

Java 如何配置shiro域以连接到struts2应用程序中的oracle数据库

Java 如何配置shiro域以连接到struts2应用程序中的oracle数据库,java,oracle,struts2,jboss,shiro,Java,Oracle,Struts2,Jboss,Shiro,我已经和shiro(ki,jsecurity)合作了几天,并且已经能够创建一个测试应用程序。我正在使用JBoss下的struts2。我已经能够用一些硬编码的用户创建一个shiro.ini文件,并且我已经让它工作了。JAVA中的API似乎非常容易掌握,因此将shiro集成到我的应用程序并使其功能化并不是真正的问题。我真正的问题是,我无法找到任何形式的文档,说明如何使用此设置创建/绑定领域到oracle数据库。我找到了几个例子,并漫无目的地试图使其适应我的情况。但我一直无法做到 我有一个带有USER

我已经和shiro(ki,jsecurity)合作了几天,并且已经能够创建一个测试应用程序。我正在使用JBoss下的struts2。我已经能够用一些硬编码的用户创建一个shiro.ini文件,并且我已经让它工作了。JAVA中的API似乎非常容易掌握,因此将shiro集成到我的应用程序并使其功能化并不是真正的问题。我真正的问题是,我无法找到任何形式的文档,说明如何使用此设置创建/绑定领域到oracle数据库。我找到了几个例子,并漫无目的地试图使其适应我的情况。但我一直无法做到

我有一个带有USERS表的oracle数据库,它只包含用户名、密码和角色。如果我能够建立连接,似乎它应该正好适合我当前的登录/注销应用程序

我在web.xml中添加了以下行

<filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
        <filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>INCLUDE</dispatcher> 
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
之后,它只是从我的loginAction.java中调用java代码:

Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();   
SecurityUtils.setSecurityManager(securityManager);   
Subject currentUser = SecurityUtils.getSubject();   
UsernamePasswordToken token = new UsernamePasswordToken(this.username, this.password);
try {   
    currentUser.login(token);   
    Session session = currentUser.getSession();   
    session.setAttribute("user", this.username);   
} catch (UnknownAccountException uae) {   
  ... 
} catch (IncorrectCredentialsException iae) {   
 ...
} catch (LockedAccountException lae) {   
 ... 
} catch (AuthenticationException ae) {   
  ...         
} catch (Exception e) {   
 ...
}   
ActionMessages messages = new ActionMessages();
if (currentUser.isAuthenticated()) {   
    System.out.println("user is authenticated!!!!!");

    if (currentUser.hasRole("admin")){
        System.out.println(this.username + " can access admin functions");
    }
    if(currentUser.hasRole("super")){
        System.out.println(this.username + " can access supervisor functions");
    }
    if(currentUser.hasRole("worker")){
        System.out.println(this.username + " can only perform worker functions");
    }
    addActionMessage(getText("success.valid.user", this.username));
    return SUCCESS;
} else {   
    System.out.println("user was not authenticated!!!!!!");
    addActionError(getText("error.login"));
    return ERROR;
}
Factory-Factory=new-InSecurityManagerFactory(“类路径:shiro.ini”);
SecurityManager SecurityManager=factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser=SecurityUtils.getSubject();
UsernamePasswordToken=新的UsernamePasswordToken(this.username,this.password);
试试{
currentUser.login(令牌);
会话会话=currentUser.getSession();
session.setAttribute(“user”,this.username);
}捕获量(未知量)
... 
}捕获(不正确的证书例外){
...
}捕获(锁定帐户例外lae){
... 
}捕获(AuthenticationException ae){
...         
}捕获(例外e){
...
}   
ActionMessages=新的ActionMessages();
如果(currentUser.isAuthenticated()){
System.out.println(“用户已验证!!!”;
if(currentUser.hasRole(“admin”)){
System.out.println(this.username+“可以访问管理功能”);
}
if(currentUser.hasRole(“超级”)){
System.out.println(this.username+“可以访问主管功能”);
}
if(currentUser.hasRole(“worker”)){
System.out.println(this.username+“只能执行辅助函数”);
}
addActionMessage(getText(“success.valid.user”,this.username));
回归成功;
}否则{
System.out.println(“用户未通过身份验证!!!!!!!”;
addActionError(getText(“error.login”);
返回误差;
}
总之,我让应用程序与硬编码用户一起工作:我的问题是,对我来说,设置连接到oracle数据库的领域的最佳方式是什么?我在网上看到过它以几种不同的方式完成,但我无法将它们中的任何一种应用到我的设置中,我很想让它实现,这样我就可以继续熟悉API了


谢谢-John

您可以使用Shiro.ini中配置的连接池数据源使用Shiro的JdbcRealm,例如:

我们在生产中使用BoneCP,对此非常满意

您可以查看以了解其工作原理。您可以自定义查询以匹配数据库架构


此外,如果默认设置/查询不起作用,您当然可以将JdbcRealm子类化以根据需要进行自定义。自定义JdbcRealm(如果您想查找数据源,它还使用JNDI来查找数据源,而不是在shiro.ini中配置数据源)。

Les,这非常有帮助,非常感谢。然而,我仍然有一个关于数据源的问题。到目前为止,我的应用程序中已经有了一个数据源。。因此,从我的角度来看,我有两个选择(如果我错了,请纠正我,可能还有更多,或者其中一个可能不起作用…)A-在.ini文件中定义我的DS。或B-在.ini文件中定义我的领域文件,然后使用java从我的领域文件调用我当前的DS。我也不反对,我只是好奇有没有一种方法更安全/更有效-谢谢,Johnaproach B可能比A好,因为你的应用程序的其余部分不会依赖Shiro配置。但是,还有一种更干净的方法:实现接口以获取应用程序DS并从getInstance()方法返回它。然后您可以配置它:ds=com.my.DatasourceFactory jdbcream.dataSource=$ds@LesHazlewood工厂是返回BoneCP连接池还是返回实际连接?谢谢您迄今为止的精彩回答。顺便说一句@frostymaverlous连接池数据源。
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();   
SecurityUtils.setSecurityManager(securityManager);   
Subject currentUser = SecurityUtils.getSubject();   
UsernamePasswordToken token = new UsernamePasswordToken(this.username, this.password);
try {   
    currentUser.login(token);   
    Session session = currentUser.getSession();   
    session.setAttribute("user", this.username);   
} catch (UnknownAccountException uae) {   
  ... 
} catch (IncorrectCredentialsException iae) {   
 ...
} catch (LockedAccountException lae) {   
 ... 
} catch (AuthenticationException ae) {   
  ...         
} catch (Exception e) {   
 ...
}   
ActionMessages messages = new ActionMessages();
if (currentUser.isAuthenticated()) {   
    System.out.println("user is authenticated!!!!!");

    if (currentUser.hasRole("admin")){
        System.out.println(this.username + " can access admin functions");
    }
    if(currentUser.hasRole("super")){
        System.out.println(this.username + " can access supervisor functions");
    }
    if(currentUser.hasRole("worker")){
        System.out.println(this.username + " can only perform worker functions");
    }
    addActionMessage(getText("success.valid.user", this.username));
    return SUCCESS;
} else {   
    System.out.println("user was not authenticated!!!!!!");
    addActionError(getText("error.login"));
    return ERROR;
}
[main]
ds = com.jolbox.bonecp.BoneCPDataSource
ds.driverClass = oracle.jdbc.driver.OracleDriver
ds.jdbcUrl = dbc:oracle:thin:@localhost:1521:myschema
ds.username = scott
ds.password = tiger
# other BoneCP connection pool settings as desired

jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.dataSource = $ds
# you can customize the authenticationQuery, userRolesQuery and permissionsQuery
# if needed.

securityManager.realms = $jdbcRealm