commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.core.filter.security.entrypoint; |
|
26 |
|
|
27 |
import cn.hutool.core.util.ObjectUtil; |
|
28 |
import cn.hutool.log.Log; |
|
29 |
import org.springframework.security.core.AuthenticationException; |
|
30 |
import org.springframework.security.web.AuthenticationEntryPoint; |
|
31 |
import org.springframework.stereotype.Component; |
|
32 |
import vip.xiaonuo.core.exception.ServiceException; |
|
33 |
import vip.xiaonuo.core.exception.enums.AuthExceptionEnum; |
|
34 |
import vip.xiaonuo.core.exception.enums.PermissionExceptionEnum; |
|
35 |
import vip.xiaonuo.core.exception.enums.ServerExceptionEnum; |
|
36 |
import vip.xiaonuo.core.util.ResponseUtil; |
|
37 |
import vip.xiaonuo.sys.core.cache.ResourceCache; |
|
38 |
|
|
39 |
import javax.annotation.Resource; |
|
40 |
import javax.servlet.http.HttpServletRequest; |
|
41 |
import javax.servlet.http.HttpServletResponse; |
|
42 |
import java.io.IOException; |
|
43 |
import java.io.Serializable; |
|
44 |
import java.util.Collection; |
|
45 |
|
|
46 |
/** |
|
47 |
* 未认证用户访问须授权资源端点 |
|
48 |
* |
|
49 |
* @author xuyuxiang |
|
50 |
* @date 2020/3/18 11:52 |
|
51 |
*/ |
|
52 |
@Component |
|
53 |
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable { |
|
54 |
|
|
55 |
private static final Log log = Log.get(); |
|
56 |
|
|
57 |
@Resource |
|
58 |
private ResourceCache resourceCache; |
|
59 |
|
|
60 |
/** |
|
61 |
* 访问未经授权的接口时执行此方法,未经授权的接口包含系统中存在和不存在的接口,分别处理 |
|
62 |
* |
|
63 |
* @author xuyuxiang |
|
64 |
* @date 2020/3/18 19:15 |
|
65 |
*/ |
|
66 |
@Override |
|
67 |
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException { |
|
68 |
|
|
69 |
String requestUri = request.getRequestURI(); |
|
70 |
|
|
71 |
//1.检查redis中RESOURCE缓存是否为空,如果为空,直接抛出系统异常,缓存url作用详见ResourceCollectListener |
|
72 |
Collection<String> urlCollections = resourceCache.getAllResources(); |
|
73 |
if (ObjectUtil.isEmpty(urlCollections)) { |
|
74 |
log.error(">>> 获取缓存的Resource Url为空,请检查缓存中是否被误删,requestUri={}", requestUri); |
|
75 |
ResponseUtil.responseExceptionError(response, |
|
76 |
ServerExceptionEnum.SERVER_ERROR.getCode(), |
|
77 |
ServerExceptionEnum.SERVER_ERROR.getMessage(), |
|
78 |
new ServiceException(ServerExceptionEnum.SERVER_ERROR).getStackTrace()[0].toString()); |
|
79 |
return; |
|
80 |
} |
|
81 |
|
|
82 |
//2.判断缓存的url中是否有当前请求的uri,没有的话响应给前端404 |
|
83 |
if (!urlCollections.contains(requestUri)) { |
|
84 |
log.error(">>> 当前请求的uri不存在,请检查请求地址是否正确或缓存中是否被误删,requestUri={}", requestUri); |
|
85 |
ResponseUtil.responseExceptionError(response, |
|
86 |
PermissionExceptionEnum.URL_NOT_EXIST.getCode(), |
|
87 |
PermissionExceptionEnum.URL_NOT_EXIST.getMessage(), |
|
88 |
new ServiceException(PermissionExceptionEnum.URL_NOT_EXIST).getStackTrace()[0].toString()); |
|
89 |
return; |
|
90 |
} |
|
91 |
|
|
92 |
//3.响应给前端无权限访问本接口(没有携带token) |
|
93 |
log.error(">>> 没有权限访问该资源,requestUri={}", requestUri); |
|
94 |
ResponseUtil.responseExceptionError(response, |
|
95 |
AuthExceptionEnum.REQUEST_TOKEN_EMPTY.getCode(), |
|
96 |
AuthExceptionEnum.REQUEST_TOKEN_EMPTY.getMessage(), |
|
97 |
new ServiceException(AuthExceptionEnum.REQUEST_TOKEN_EMPTY).getStackTrace()[0].toString()); |
|
98 |
} |
|
99 |
} |