JwtAuthenticationEntryPoint.java 1.0 KB

12345678910111213141516171819202122232425
  1. package com.izouma.nineth.security;
  2. import org.springframework.security.core.AuthenticationException;
  3. import org.springframework.security.web.AuthenticationEntryPoint;
  4. import org.springframework.stereotype.Component;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.io.Serializable;
  9. @Component
  10. public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {
  11. private static final long serialVersionUID = -8970718410437077606L;
  12. @Override
  13. public void commence(HttpServletRequest request,
  14. HttpServletResponse response,
  15. AuthenticationException authException) throws IOException {
  16. // This is invoked when user tries to access a secured REST resource without supplying any credentials
  17. // We should just send a 401 Unauthorized response because there is no 'login page' to redirect to
  18. response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "授权失败");
  19. }
  20. }