java捕获自定义异常_shiro自定义异常无法被捕获总是抛出AuthenticationException解决方案...

这个问题我也是出的莫名其妙,刚开始好好的,然后配置多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 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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值