CustomizeAuthenticationEntryPoint.java 1.1 KB

123456789101112131415161718192021222324252627
  1. package com.welampiot.security;
  2. import com.alibaba.fastjson.JSON;
  3. import com.welampiot.common.BaseResult;
  4. import com.welampiot.common.ResultEnum;
  5. import org.springframework.security.core.AuthenticationException;
  6. import org.springframework.security.web.AuthenticationEntryPoint;
  7. import org.springframework.stereotype.Component;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.IOException;
  12. /**
  13. * @Description: 匿名用户访问无权限资源时的异常
  14. */
  15. @Component
  16. public class CustomizeAuthenticationEntryPoint implements AuthenticationEntryPoint {
  17. @Override
  18. public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e)
  19. throws IOException, ServletException {
  20. BaseResult result = BaseResult.fail(ResultEnum.USER_NOT_LOGIN);
  21. httpServletResponse.setContentType("text/json;charset=utf-8");
  22. httpServletResponse.getWriter().write(JSON.toJSONString(result));
  23. }
  24. }