提交 | 用户 | 时间
|
ed4f78
|
1 |
package com.iailab.module.data.common.utils; |
a6de49
|
2 |
|
H |
3 |
|
|
4 |
import com.iailab.framework.common.constant.Constant; |
d41f14
|
5 |
import com.iailab.framework.tenant.core.context.TenantContextHolder; |
a6de49
|
6 |
import com.iailab.module.system.api.user.AdminUserApi; |
H |
7 |
import org.apache.commons.lang3.StringUtils; |
d41f14
|
8 |
|
a6de49
|
9 |
import javax.annotation.Resource; |
d41f14
|
10 |
|
a6de49
|
11 |
import org.springframework.stereotype.Component; |
H |
12 |
|
|
13 |
import javax.servlet.http.HttpServletRequest; |
|
14 |
import java.util.regex.Pattern; |
|
15 |
|
|
16 |
/** |
|
17 |
* @author PanZhibao |
|
18 |
* @Description |
|
19 |
* @createTime 2023年12月06日 15:55:00 |
|
20 |
*/ |
|
21 |
@Component |
|
22 |
public class ApiSecurityUtils { |
|
23 |
|
0866d8
|
24 |
/*@Resource |
潘 |
25 |
private ApiAppService apiAppService;*/ |
a6de49
|
26 |
|
H |
27 |
@Resource |
|
28 |
private AdminUserApi adminUserApi; |
|
29 |
|
|
30 |
private Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); |
|
31 |
|
|
32 |
private String getRequestToken(HttpServletRequest httpRequest) { |
|
33 |
//从header中获取token |
|
34 |
String token = httpRequest.getHeader(Constant.TOKEN_HEADER); |
|
35 |
|
|
36 |
//如果header中不存在token,则从参数中获取token |
|
37 |
if (StringUtils.isBlank(token)) { |
|
38 |
token = httpRequest.getParameter(Constant.TOKEN_HEADER); |
|
39 |
} |
|
40 |
|
|
41 |
return token; |
|
42 |
} |
|
43 |
|
d41f14
|
44 |
private void setTenantId(HttpServletRequest httpRequest) { |
潘 |
45 |
String tenantId = httpRequest.getHeader(Constant.HEAD_TENANT_ID); |
|
46 |
|
047527
|
47 |
if (StringUtils.isNotBlank(tenantId)) { |
d41f14
|
48 |
TenantContextHolder.setTenantId(Long.parseLong(tenantId)); |
潘 |
49 |
} |
|
50 |
} |
|
51 |
|
a6de49
|
52 |
|
H |
53 |
public void validate(HttpServletRequest httpRequest) throws Exception { |
d41f14
|
54 |
setTenantId(httpRequest); |
潘 |
55 |
/*String token = getRequestToken(httpRequest); |
a6de49
|
56 |
if (StringUtils.isBlank(token)) { |
H |
57 |
throw new Exception("token 不能为空!"); |
|
58 |
} |
|
59 |
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); |
|
60 |
if (ObjectUtils.isEmpty(loginUser)) { |
|
61 |
throw new RuntimeException("用户不能为空"); |
|
62 |
} |
|
63 |
CommonResult<AdminUserRespDTO> user = adminUserApi.getUser(loginUser.getId()); |
|
64 |
if(ObjectUtils.isEmpty(user)) { |
|
65 |
throw new RuntimeException("用户不存在"); |
|
66 |
} |
|
67 |
AdminUserRespDTO userData = user.getData(); |
d41f14
|
68 |
String username = userData.getUsername();*/ |
0866d8
|
69 |
/*ApiAppEntity appInfo = apiAppService.getInfoByAppKey(username); |
a6de49
|
70 |
if (appInfo == null) { |
H |
71 |
throw new RuntimeException("应用未授权"); |
0866d8
|
72 |
}*/ |
a6de49
|
73 |
//TODO 验证签名 |
H |
74 |
// if(!com.iailab.common.utils.JwtUtils.verify(token, appInfo.getAppSecret())){ |
|
75 |
// throw new RuntimeException("签名错误"); |
|
76 |
// } |
|
77 |
} |
|
78 |
|
|
79 |
private boolean isInteger(String str) { |
|
80 |
return pattern.matcher(str).matches(); |
|
81 |
} |
|
82 |
|
|
83 |
|
|
84 |
} |