Shiro中多Realm下抛出异常不准确

Shiro中多Realm下抛出异常不准确

问题描述

如果你的项目中存在多个Realm,当你在Realm中判断账号异常抛出了一个UnknownAccountException异常时,到达FormAuthenticationFilter.onLoginFailure方法中的异常信息为:

Authentication token of type [class org.apache.shiro.authc.UsernamePasswordToken] could not be authenticated by any configured realms.  Please ensure that at least one realm can authenticate these tokens.

原因与解决

查看**ModularRealmAuthenticator.doMultiRealmAuthentication方法源码:

/**
     * Performs the multi-realm authentication attempt by calling back to a {@link AuthenticationStrategy} object
     * as each realm is consulted for {@code AuthenticationInfo} for the specified {@code token}.
     *
     * @param realms the multiple realms configured on this Authenticator instance.
     * @param token  the submitted AuthenticationToken representing the subject's (user's) log-in principals and credentials.
     * @return an aggregated AuthenticationInfo instance representing account data across all the successfully
     *         consulted realms.
     */
    protected AuthenticationInfo doMultiRealmAuthentication(Collection<Realm> realms, AuthenticationToken token) {
   

        AuthenticationStrategy strategy = getAuthenticationStrategy();

        AuthenticationInfo aggregate = strategy.beforeAllAttempts(realms, token);

        if (log.isTraceEnabled()) {
   
            log.trace("Iterating through {} realms for PAM authentication", realms.size());
        }

        for (Realm realm : realms) {
   

            aggregate = strategy.beforeAttempt(realm, token, aggregate);

            if (realm.supports(token)) {
   

                log.trace("Attempting to authenticate token [{}] using realm [{}]", token, realm);

                AuthenticationInfo info = null;
                Throwable t = null;
                try {
   
                    info = realm.getAuthenticationInfo(token);
                } catch 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shiro框架中,可以通过自定义一个Realm实现逆向密码与正确密码匹配的功能。 首先,您需要继承`org.apache.shiro.realm.AuthenticatingRealm`类,并实现其中的`doGetAuthenticationInfo`方法。在该方法中,您需要编写自己的认证逻辑,包括逆向密码与正确密码匹配的判断。 以下是一个简单的示例: ```java public class CustomRealm extends AuthenticatingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername(); String password = new String(upToken.getPassword()); // 获取用户信息 User user = getUserByUsername(username); if (user == null) { throw new UnknownAccountException("用户不存在"); } // 判断逆向密码与正确密码是否匹配 if (!password.equals(user.getPassword()) && !password.equals(user.getReversePassword())) { throw new IncorrectCredentialsException("密码不正确"); } // 返回认证信息 return new SimpleAuthenticationInfo(user, user.getPassword(), getName()); } private User getUserByUsername(String username) { // 通过用户名获取用户信息 // ... } } ``` 在上面的示例中,我们通过`getUserByUsername`方法获取了用户信息,然后判断逆向密码与正确密码是否匹配,如果不匹配则抛出`IncorrectCredentialsException`异常,否则返回认证信息。 最后,您需要将自定义的Realm注入到Shiro的配置中,以便Shiro能够使用它来进行身份认证。例如: ```java DefaultSecurityManager securityManager = new DefaultSecurityManager(); securityManager.setRealm(new CustomRealm()); // 将securityManager设置到SecurityUtils中 SecurityUtils.setSecurityManager(securityManager); ``` 这样,当用户进行身份认证时,Shiro就会调用我们自定义的Realm中的`doGetAuthenticationInfo`方法来进行认证,并判断逆向密码与正确密码是否匹配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值