如何在Spring中发送带有错误代码的自定义对象?

时间:2018-06-18 06:48:37

标签: java spring-security

我希望在会话在spring security中过期时发送带有错误代码和错误消息的自定义对象。到目前为止,这是我的代码:

@Component
public class AuthenticationEntryPoint extends BasicAuthenticationEntryPoint {

@Autowired
private UserJedisRepository userJedisRepository;

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)
        throws IOException, ServletException {
    String token = request.getSession().getId();
    userJedisRepository.delete(token);

    response.setContentType("application/json");
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    response.getOutputStream().println("error Messsage: "+authEx.getMessage());

}

@Override
public void afterPropertiesSet() throws Exception {
    setRealmName("Developer");
    super.afterPropertiesSet();
}

}

0 个答案:

没有答案