java – 如何配置shiro Realm以连接到struts2应用程序中的oracle
我已经和shiro(ki,jsecurity)合作了几天,并且已经能够创建一个测试应用程序.我正在使用struts2,关闭JBoss.我已经能够创建一个shiro.ini文件与一些硬编码用户,我已经得到了这个工作.似乎JAVA中的API非常容易掌握,因此将shiro集成到我的应用程序并使其功能化并不是真正的问题.我真正的问题是,我无法找到任何形式的文档,介绍如何使用此设置创建/绑定REALM到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> 然后我将shiro.ini添加到我的WEB-INF: [main] [users] bruins = boston,admin,super,worker rangers = newyork,worker leafs = toronto,worker [urls] /authoring/logout = logout /authoring/** = authc 之后,它只是从我的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; } 总而言之,我让应用程序与硬编码用户一起工作:我的问题是,设置REALM连接到我的oracle数据库的最佳方法是什么?我已经通过几种不同的方式在网上看到了它,并且我无法将它们中的任何一种用于我的设置,我希望能够实现这一点,我可以继续熟悉API. 谢谢 – 约翰 解决方法
您可以使用Shiro的JdbcRealm,使用shiro.ini中配置的连接池DataSource,例如,与
BoneCP:
[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 我们在生产中使用BoneCP并且非常满意. 您可以查看JdbcRealm’s source code以了解它的工作原理.您可以自定义查询以匹配数据库架构. 此外,如果默认设置/查询不起作用,您当然可以将JdbcRealm子类化为自定义.自定义JdbcRealm的Here’s an example(如果你想这样做,它还使用JNDI来查找数据源,而不是在shiro.ini中配置数据源). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |