[英]Catching and rethrowing an exception from a boolean method does return false whereas not doing anything causes the method not to return
I have a general query regarding the java programming language and how it deals with exceptions and methods returning boolean. 我有一个关于Java编程语言以及它如何处理返回布尔值的异常和方法的常规查询。
Please not that although the example below deals with Spring/Ldap/ActiveDirectory, my question is only about java and exceptions . 请不要尽管尽管下面的示例涉及Spring / Ldap / ActiveDirectory,但我的问题仅是关于java和exceptions的问题 。
public boolean doAuthenticate(String userAndDomain, String password) {
UsernamePasswordAuthenticationToken userToken = new UsernamePasswordAuthenticationToken(replaceBackSlashWithAtSign(userAndDomain), password);
try {
Authentication authentication = adAuthenticationProvider.authenticate(userToken);
return authentication.isAuthenticated();
} catch (BadCredentialsException e) {
log.error("Authentication failed - wrong username\\password", e);
throw new BadCredentialsException("Authentication failed - wrong username\\password", e);
} catch (AuthenticationException e) {
log.error("Authentication failed - AuthenticationException", e);
throw new AuthenticationException("Authentication failed - AuthenticationException", e) { };
}
}
If any of BadCredentialsException
or AuthenticationException
is rethrown by the authenticate method, then the doAuthenticate method returns false . 如果authenticate方法重新抛出
BadCredentialsException
或AuthenticationException
任何一个,则doAuthenticate方法将返回false 。
However if for some reason another runtime exception is thrown by adAuthenticationProvider.authenticate()
, then the method does not return false and does not return at all ... 但是,如果由于某种原因
adAuthenticationProvider.authenticate()
引发了另一个运行时异常 ,则该方法不会返回false, 也不会返回 ...
I am just curious to know why that is... 我只是想知道为什么会这样...
edit : 编辑 :
LdapAuthentifier authentifier = new LdapAuthentifierImpl();
boolean didAuthenticate = authentifier.doAuthenticate(VALID_USER, INVALID_PASSWORD);
A System.out.println
of didAuthenticate
does show false if one of the two specified exceptions are thrown whereas another exception halts execution of the program and the System.out.println
is never reached... 如果抛出了两个指定的异常之一,则
didAuthenticate
的System.out.println
确实显示为false ,而另一个异常停止了程序的执行,并且从未达到System.out.println
。
edit 2 : 编辑2 :
public static void main(String[] args) {
LdapAuthentifier authentifier = new LdapAuthentifierImpl();
boolean didAuthenticate = authentifier.doAuthenticate(VALID_USER, INVALID_PASSWORD);
}
I understand what happened. 我了解发生了什么事。 Here is the explanation.
这是解释。
The exception I actually saw in the logs was BadCredentialsException
but this exception is never thrown by adAuthenticationProvider.authenticate
and therefore never rethrown by the below method . 其实,我在日志中看到的唯一的例外是
BadCredentialsException
但这种异常不会被抛出 adAuthenticationProvider.authenticate
, 因此永远不会被下面的方法重新抛出 。
What actually happened was that the authentication.isAuthenticated()
was just returning false and I was passing this boolean value to the client code. 实际发生的情况是
authentication.isAuthenticated()
返回的只是false,而我将此布尔值传递给客户端代码。
I am including the method again for clarity's sake: 为了清楚起见,我再次包含该方法:
@Override
public boolean doAuthenticate(String userAndDomain, String password) {
UsernamePasswordAuthenticationToken userToken = new UsernamePasswordAuthenticationToken(replaceBackSlashWithAtSign(userAndDomain), password);
try {
Authentication authentication = adAuthenticationProvider.authenticate(userToken);
return authentication.isAuthenticated();
} catch (BadCredentialsException e) {
log.error("Authentication failed - wrong username\\password", e);
throw new BadCredentialsException("Authentication failed - wrong username\\password", e);
} catch (AuthenticationException e) {
log.error("Authentication failed - AuthenticationException", e);
throw new AuthenticationException("Authentication failed - AuthenticationException", e) { };
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.