Helius
2021-11-02 32cd9fe7569d4bf40bd3ee4b13d1e2e5a50203de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.xcong.excoin.configurations.security;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xcong.excoin.common.contants.AppContants;
import com.xcong.excoin.common.response.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
/**
 * @author wzy
 * @date 2020-05-12
 **/
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
 
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        String timeOut = response.getHeader("TimeOut");
        if (AppContants.TIME_OUT.equals(timeOut)) {
            Result result = Result.timeOut("Time Out");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            response.getWriter().write(new ObjectMapper().writeValueAsString(result));
            response.setStatus(HttpServletResponse.SC_REQUEST_TIMEOUT);
        } else {
            Result result = Result.loginFail("Unauthorized");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            response.getWriter().write(new ObjectMapper().writeValueAsString(result));
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
    }
}