我想从我的类中抛出AuthenticationException
到我的覆盖attemptAuthentication
中,这个类扩展了UsernamePasswordAuthenticationFilter
方法,但是我不知道怎么做。
我的代码:
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
String username = request.getParameter("username");
String password = request.getParameter("password");
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
if(portService.getOpenPort()==null){
//'AuthenticationException' is abstract; cannot be instantiated
throw new AuthenticationException("No free port found");
}
return authenticationManager.authenticate(authenticationToken);
}
它是否仅用于try catch子句,如果是,是否有其他方法使过滤器继续unsuccessfulAuthentication
?
1条答案
按热度按时间stszievb1#
您应该像
BadCredentialsException
一样创建自己的AuthenticationException
子类。您可以创建:
并在代码中执行以下操作:
throw new NoOpenPortAuthenticationException("No free port found");