Search code examples
apacheshiro

Configure shiro.ini for JDBC connection


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

Solution

  • 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