提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.sys.core.auth.entrypoint; |
懒 |
2 |
|
|
3 |
import cn.stylefeng.guns.base.auth.exception.enums.AuthExceptionEnum; |
|
4 |
import cn.stylefeng.roses.kernel.model.response.ErrorResponseData; |
|
5 |
import com.alibaba.fastjson.JSON; |
|
6 |
import org.springframework.security.core.AuthenticationException; |
|
7 |
import org.springframework.security.web.AuthenticationEntryPoint; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
|
|
10 |
import javax.servlet.http.HttpServletRequest; |
|
11 |
import javax.servlet.http.HttpServletResponse; |
|
12 |
import java.io.IOException; |
|
13 |
import java.io.Serializable; |
|
14 |
|
|
15 |
/** |
|
16 |
* 这个端点用在用户访问受保护资源但是不提供任何token的情况下 |
|
17 |
* <p> |
|
18 |
* 当前用户没有登录(没有token),访问了系统中的一些需要权限的接口,就会进入这个处理器 |
|
19 |
* |
|
20 |
* @author fengshuonan |
|
21 |
* @Date 2019/7/20 17:57 |
|
22 |
*/ |
|
23 |
@Component |
|
24 |
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable { |
|
25 |
|
|
26 |
private static final long serialVersionUID = -1L; |
|
27 |
|
|
28 |
@Override |
|
29 |
public void commence(HttpServletRequest request, |
|
30 |
HttpServletResponse response, |
|
31 |
AuthenticationException authException) throws IOException { |
|
32 |
|
|
33 |
// GET请求跳转到主页 |
|
34 |
if ("get".equalsIgnoreCase(request.getMethod()) |
|
35 |
&& !request.getHeader("Accept").contains("application/json")) { |
|
36 |
|
|
37 |
response.sendRedirect(request.getContextPath() + "/global/sessionError"); |
|
38 |
|
|
39 |
} else { |
|
40 |
|
|
41 |
// POST请求返回json |
|
42 |
response.setCharacterEncoding("utf-8"); |
|
43 |
response.setContentType("application/json"); |
|
44 |
|
|
45 |
ErrorResponseData errorResponseData = new ErrorResponseData( |
|
46 |
AuthExceptionEnum.NO_PAGE_ERROR.getCode(), AuthExceptionEnum.NO_PAGE_ERROR.getMessage()); |
|
47 |
|
|
48 |
response.getWriter().write(JSON.toJSONString(errorResponseData)); |
|
49 |
} |
|
50 |
} |
|
51 |
} |