【发布时间】:2018-06-04 20:43:00
【问题描述】:
我创建了一个自定义异常类
public class DataException extends Exception {
private final String errorCode;
private final String errorMessage;
private final HttpStatus httpStatus;
/**
*/
public DataException( String errorCode, String errorMessage,
HttpStatus httpStatus )
{
this.errorCode = errorCode;
this.errorMessage = errorMessage;
this.httpStatus = httpStatus;
}
当抛出异常时,我会将此异常类发送到客户端。我也将Spring-Security and JWT 用于Authentication and Authorization 进程。
当用户通过点击端点/rest/login 登录到应用程序时,如果凭据错误,spring 会抛出它的异常,该异常将发送到客户端。
如何捕获安全过滤器抛出的异常并创建我们的用户定义异常并将其发送到客户端?
我的过滤方法,
@Override
public Authentication attemptAuthentication( HttpServletRequest request, HttpServletResponse response )
{
try
{
UserModel credentials = new ObjectMapper().readValue(request.getInputStream(), UserModel.class);
return authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(credentials.getEmailId(),
credentials.getPassword(), new ArrayList<>()));
}
catch( IOException | AuthenticationException e )
{
LOGGER.error("Exception", "Invalid EmailId/Password");
throw new BadCredentialsException("Invalid EmailId/password");
}
}
【问题讨论】:
标签: java spring-boot spring-security jwt