12345678910111213141516171819202122232425262728293031 |
- package edu.travel.config;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.security.core.AuthenticationException;
- import org.springframework.security.web.AuthenticationEntryPoint;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
- @Override
- public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
- Map map = new HashMap();
- map.put("code", "451");
- map.put("message", "无权限访问");
- map.put("path", httpServletRequest.getServletPath());
- map.put("timestamp", String.valueOf(System.currentTimeMillis()));
- httpServletResponse.setContentType("application/json");
- httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
- try {
- ObjectMapper mapper = new ObjectMapper();
- mapper.writeValue(httpServletResponse.getOutputStream(), map);
- } catch (Exception ex) {
- throw new ServletException();
- }
- }
- }
|