【问题标题】:can i redirect user to other jsp page in shiro jdbcrealm doGetAuthenticationInfo() method?我可以在 shiro jdbcrealm doGetAuthenticationInfo() 方法中将用户重定向到其他 jsp 页面吗?
【发布时间】:2013-11-11 09:48:50
【问题描述】:

您好,我想知道的我可以将用户重定向到 shiro 自定义 jdbcrealm 中的 accessdeniedpage.jsp 这是我的代码....

  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws HostUnauthorizedException,AuthenticationException {

    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername();
    String clientIP = upToken.getHost();

    // Null username is invalid
    if (username == null) {
        throw new AccountException("Null usernames are not allowed by this realm.");
    }

    Connection conn = null;
    AuthenticationInfo info = null;
    try {
        conn = dataSource.getConnection();

        String password = getPasswordForUser(conn, username); // get userpassword
        boolean ipFlag = getIPFlag(conn,username); // check whether users ip needs to be check i.e. get ipflag from users tbl, if true check user's ip else not
        boolean ipMatched = checkIP(conn,username,clientIP,ipFlag); // returns if user's ip matched with ip stored in database..

        if (password == null) {
            throw new UnknownAccountException("No account found for user [" + username + "]");
        }

        if(ipMatched == false){
             // how to redirect user to accessdeniedpage.jsp ?
        }

        info = buildAuthenticationInfo(username, password.toCharArray());

    } catch (SQLException e) {
        final String message = "There was a SQL error while authenticating user [" + username + "]";
        if (log.isErrorEnabled()) {
            log.error(message, e);
        }

        // Rethrow any SQL errors as an authentication exception
        throw new AuthenticationException(message, e);
    } finally {
        JdbcUtils.closeConnection(conn);
    }

    return info;
}

我正在检查用户的 ip,如果在数据库中找不到 ip,我想将用户重定向到拒绝访问的页面

更新shiro.ini

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

# 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 users WHERE username = ? 
jdbcRealm.userRolesQuery = SELECT role_name FROM user_roles WHERE username = ? 
jdbcRealm.permissionsQuery = SELECT roleper FROM roles_permissions WHERE role_name = ? 
jdbcRealm.permissionsQueryIP = SELECT ip FROM user_ip_permissions WHERE username = ? 
jdbcRealm.permissionsQueryCountry = SELECT countryname FROM country_permissions WHERE username = ? 
jdbcRealm.defaultPageQuery = SELECT default_page FROM users WHERE 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

# only users with some roles are allowed to use role-specific pages 
/admin/** = authc,perms[page:*]
/java/** = authc,perms[page:javadeveloperpage]
/php/** = authc,perms[page:phpdeveloperpage]
/ruby/** = authc,perms[page:rubydeveloperpage]
/deo/** = authc,perms[page:deopage]

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

感谢和问候

【问题讨论】:

    标签: java jsp shiro


    【解决方案1】:

    既然你想拒绝访问,逻辑上你需要抛出AuthorizationException并将其映射到web.xml中的自定义页面

    if(ipMatched == false){
        throw new AuthorizationException();
    }
    

    在你的 web.xml

    <error-page>
        <exception-type>org.apache.shiro.authz.AuthorizationException</exception-type>
        <location>/path/to/accessdeniedpage.jsp</location>
    </error-page>
    

    附带说明,仅在 身份验证失败的情况下,抛出 AuthenticationException 合乎逻辑

    【讨论】:

    • 感谢您的帮助,但它再次将我重定向到 login.jsp 页面
    猜你喜欢
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2017-07-21
    相关资源
    最近更新 更多