【问题标题】:Shiro custom JDBC realmShiro 自定义 JDBC 领域
【发布时间】:2016-01-13 16:58:16
【问题描述】:

我正在搞乱 Shiro 安全框架并实现自定义 JDBC 领域。

以下值当前在我的 shiro.ini 文件中设置

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

我的问题是,如果我扩展 JdbcRealm 并覆盖它的 doGetAuthenticationInfo(AuthenticationToken token) 方法,那么 jdbcRealm.authenticationQuery 设置在我的 shiro.ini 文件仍然被调用?还是方法覆盖优先于 shiro.ini 文件中的设置?

public class CustomJdbcRealm extends JdbcRealm 
{
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException 
    {
        // Connect to DB to get password and salt values
    }
}

【问题讨论】:

    标签: java apache shiro


    【解决方案1】:

    doGetAuthenticationInfo 是从数据库完成身份验证的主要方法。因此,如果您通常覆盖它,那么您将覆盖身份验证过程。所以最好先调用超类方法,然后获取它的 ino 然后使用它,这样您就不必更改任何内容。 shiro.ini 中的 sql 也会自动映射。并且在您覆盖之前不会更改它们
    setAuthenticationQuery、setUserRolesQuery 等

    您可以轻松调用以下方法来模拟实际过程,然后自定义它。

    AuthenticationInfo  info = super.doGetAuthenticationInfo(token); 
    

    注意,super 是对父级的引用,但 super() 是它的构造函数。

    喜欢:

    public class CustomJdbcRealm extends JdbcRealm 
    {
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException 
        {
    
           AuthenticationInfo  info = super.doGetAuthenticationInfo(token);
          // Your own code here 
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-12
      • 2015-01-19
      • 2012-09-17
      • 2015-06-06
      • 2015-05-31
      • 2012-03-01
      • 2018-04-03
      • 2012-01-12
      • 2011-12-12
      相关资源
      最近更新 更多