黎星凯
2024-05-17 3520e86e2b00b9c1ee3f4fffd4ab49fe3d6c259e
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
package com.iplatform.security;
 
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.web.ResponseCode;
import com.walker.web.ResponseValue;
import com.walker.web.util.ServletUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
 
import javax.servlet.ServletException;
import java.io.IOException;
 
/**
 * 匿名用户访问无权限资源时的异常。
 * @author 时克英
 * @date 2022-10-31
 */
public class FailedAuthenticationEntryPoint implements AuthenticationEntryPoint {
 
    protected final transient Logger logger = LoggerFactory.getLogger(getClass());
 
    @Override
    public void commence(javax.servlet.http.HttpServletRequest request
            , javax.servlet.http.HttpServletResponse response
            , AuthenticationException authException) throws IOException, ServletException {
 
        String msg = "认证失败,无权限访问系统资源" + request.getRequestURI();
        try {
            ServletUtils.renderString(response, JsonUtils.objectToJsonString(ResponseValue.error(ResponseCode.EXCEPTION.getCode(), msg)));
        } catch (Exception e) {
            logger.error("无权限访问系统资源" + request.getRequestURI());
        }
    }
}