Configure shiro.ini for JDBC connection

半城伤御伤魂 提交于 2019-12-12 07:24:13

问题


As part of my new years learning new technologies initiative I have started messing around with the Apache Shiro Security Framework.

I managed to get the basic example working which stores usernames, passwords and roles in the shiro.ini file, but when I modified my shiro.ini file to use JDBC it just stopped working. I now keep getting prompted for my username and password when trying to access my application. I've kept it as simple as possible (the passwords aren't even hashed).

Below is my shiro.ini file, does anyone have any idea what I'm doing wrong?

[main]
authc.usernameParam = j_username
authc.passwordParam = j_password
authc.failureKeyAttribute = shiroLoginFailure

jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled = true

jdbcRealm.authenticationQuery = "SELECT password FROM user WHERE username = ?"
jdbcRealm.userRolesQuery = "SELECT role FROM user WHERE username = ?"

ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
ds.serverName = localhost
ds.user = root
ds.password = password
ds.databaseName = database
jdbcRealm.dataSource = $ds

# Use Built-in Chache Manager
builtInCacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $builtInCacheManager

securityManager.realms = $jdbcRealm

[users]
[roles]
[urls]
/* = authcBasic

回答1:


If you are not giving permission query then better disable permission lookup. Also if you want to use basic Authentication why use authc attributes.

Try Following

    [main]
    #authc.usernameParam = j_username
    #authc.passwordParam = j_password
    #authc.failureKeyAttribute = shiroLoginFailure

    jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
    jdbcRealm.permissionsLookupEnabled = false

    jdbcRealm.authenticationQuery = SELECT password FROM user WHERE username = ?
    jdbcRealm.userRolesQuery = SELECT role FROM user WHERE username = ?

    ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
    ds.serverName = localhost
    ds.user = root
    ds.password = password
    ds.databaseName = database
    jdbcRealm.dataSource = $ds

    # Use Built-in Chache Manager
    builtInCacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
    securityManager.cacheManager = $builtInCacheManager

    securityManager.realms = $jdbcRealm

    [users]
    [roles]
    [urls]
    /* = authcBasic


来源:https://stackoverflow.com/questions/34637907/configure-shiro-ini-for-jdbc-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!