我有如下所示的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发布于 2014-01-29 16:54:54
请澄清你的问题。标题询问如何从数据库表中获取值。但是,post使用SercurityUtils对象。数据库表不等于SecurityUtils。
如果要从数据库表中选择值,请执行以下操作。您需要指定所使用的数据库类型、架构等,然后实现语句从数据库中选择值。
如果您试图将Shiro配置为使用数据库,请将此配置发布给您。
请张贴您的衬衫配置。这可能会给人们提供足够的信息来帮助你:)
必须将Shiro配置为使用数据库。
更新:
在Shiro配置中,tou需要将您的领域添加到安全管理器中:
securityManager.realm = $jdbcRealm您还需要验证是否正确配置了jndi查找。
此外,请张贴“但后面的代码行.”的上下文。您是否提交了有效的登录。如果是这样的话,是怎么做的?“代码行”在哪里?它是如何被调用的?
https://stackoverflow.com/questions/21437176
复制相似问题