如何检查数据库表中的多个shiro权限?

时间:2014-01-29 16:40:56

标签: java shiro

我有roles_perms表格如下

id || username || role_name || perms_name
1      root         admin      page:show:*;folder:show:*
2      user1        editor     page:show:editorpage,page1;folder:show:folder45,folder55

但是下面的代码行不会返回预期的结果。

// for user with role "admin"

System.out.println("Test 1 :"+SecurityUtils.getSubject().isPermitted("page:show:*")); // false
System.out.println("Test 2 :"+SecurityUtils.getSubject().isPermitted("page:show:*;folder:show:*")); // true

output:

Test1 :false
Test2 :true


// for user with role "editor"

System.out.println("Test 3 :"+SecurityUtils.getSubject().isPermitted("page:show:page1")); // false
System.out.println("Test 4 :"+SecurityUtils.getSubject().isPermitted("page:show:page1;folder:show:folder55")); // false 

 output:
 Test3 :false
 Test4 :false

更新

shiro.ini文件

[main]
ds = org.apache.shiro.jndi.JndiObjectFactory
ds.requiredType   = javax.sql.DataSource
ds.resourceName = jdbc/imgDB
ds.resourceRef = true
jdbcRealm = com.java.realm.MyRealm 

# 1000 ms = 1 sec
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
securityManager.sessionManager = $sessionManager
securityManager.sessionManager.globalSessionTimeout = 1800000 

# password hashing specification
sha256Matcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
sha256Matcher.hashAlgorithmName = SHA-256
jdbcRealm.credentialsMatcher = $sha256Matcher

jdbcRealm.permissionsLookupEnabled = true 
jdbcRealm.authenticationQuery = SELECT password FROM shiro_users WHERE username = ? 
jdbcRealm.userRolesQuery = SELECT role_name FROM shiro_user_roles WHERE username = ? 
jdbcRealm.permissionsQuery = SELECT perms_name FROM shiro_roles_perms WHERE role_name = ? AND username = ? 


jdbcRealm.dataSource = $ds
jdbcRealm.authorizationCachingEnabled = false

# specify login page 
authc.loginUrl = /login.jsp 

# redirect after successful login
authc.successUrl = /home.jsp

# roles filter: redirect to error page if user does not have access rights
# perms filter: redirect to error page if user does not have permissions
roles.unauthorizedUrl = /accessdenied.jsp
perms.unauthorizedUrl = /accessdenied.jsp

# request parameter with login error information; if not present filter assumes 'shiroLoginFailure'
# authc.failureKeyAttribute = simpleShiroApplicationLoginFailure

[urls] 

/login.jsp = authc
/admin/** = authc,roles[admin]
/editor/** = authc

# enable authc filter for all application pages
/ApacheShiroLogin/** = authc

1 个答案:

答案 0 :(得分:0)

请澄清你的问题。标题询问从数据库表中获取值。但是,该帖子使用SercurityUtils对象。数据库表不等于SecurityUtils。

如果要从数据库表中选择值。您需要指定正在使用的数据库类型,架构等。然后实现语句以从数据库中选择值。

如果您尝试将Shiro配置为使用数据库,请发送shiro配置。

请发布您的shiro配置。这可能会给人们足够的信息来帮助你:)

必须将Shiro配置为使用数据库。

更新

在你的Shiro配置中,tou需要将你的领域添加到安全管理器:

securityManager.realm = $jdbcRealm

您还需要验证是否正确配置了jndi查找。

另外,请发布“但是遵循以下代码行......”的上下文。您是否提交了有效登录信息?如果是这样,怎么样? “代码行”在哪里?它是如何被调用的?