shiro自定义异常无法被捕获总是抛出Authenticati…

2019-06-14 08:04:19来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  这个问题我也是出的莫名其妙,刚开始好好的,然后配置多realm之后出的。

  现在直入主题

  在继承了 org.apache.shiro.authc.pam.ModularRealmAuthenticator的类中重写doMultiRealmAuthentication方法

  以下是重写的代码,判断是否存在异常。如果存在异常,则抛出。

public class MyModularRealmAuthenticator extends ModularRealmAuthenticator {

private static final Logger log = LoggerFactory.getLogger(ModularRealmAuthenticator.class);

@Override
protected AuthenticationInfo doMultiRealmAuthentication(Collection<Realm> realms, AuthenticationToken token) throws AuthenticationException {
AuthenticationStrategy strategy = getAuthenticationStrategy();

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

if (log.isTraceEnabled()) {
log.trace("Iterating through {} realms for PAM authentication", realms.size());
}
AuthenticationException authenticationException = null;
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;
try {
info = realm.getAuthenticationInfo(token);
} catch (AuthenticationException e) {
authenticationException = e;
if (log.isDebugEnabled()) {
String msg = "Realm [" + realm + "] threw an exception during a multi-realm authentication attempt:";
log.debug(msg, e);
}
}

aggregate = strategy.afterAttempt(realm, token, info, aggregate, authenticationException);

} else {
log.debug("Realm [{}] does not support token {}. Skipping realm.", realm, token);
}
}
if(authenticationException != null){
throw authenticationException;
}
aggregate = strategy.afterAllAttempts(token, aggregate);

return aggregate;
}
}

 

  关键点在于

    if(authenticationException != null){
throw authenticationException;
}
  

  


原文链接:https://www.cnblogs.com/hunmeng/p/11023443.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:别再问什么是Java内存模型了,看这里!

下一篇:synchronized到底锁住的是谁?