|
@@ -0,0 +1,155 @@
|
|
|
+package com.dragonsoft.dcuc.approvegateway.filter;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.dragonsoft.approve.component.TokenOperate;
|
|
|
+import com.dragonsoft.approve.model.TokenInfo;
|
|
|
+import com.dragonsoft.dcuc.approvegateway.Constants;
|
|
|
+import com.dragonsoft.dcuc.approvegateway.business.BimBusiness;
|
|
|
+import com.dragonsoft.dcuc.approvegateway.pojo.BimUserInfoItemRespVO;
|
|
|
+import com.dragonsoft.dcuc.approvegateway.properties.DcucApproveProperties;
|
|
|
+import com.dragonsoft.duceap.base.api.security.ISecurityAccessTokenResolver;
|
|
|
+import com.dragonsoft.duceap.base.entity.security.BaseSecurityUser;
|
|
|
+import com.dragonsoft.duceap.base.entity.security.SecurityUser;
|
|
|
+import com.dragonsoft.duceap.commons.util.UrlMatcher;
|
|
|
+import com.dragonsoft.duceap.commons.util.json.JsonUtils;
|
|
|
+import com.dragonsoft.duceap.security.jwt.securityaccess.SecurityAccessTokenProperties;
|
|
|
+import com.dragonsoft.duceap.security.jwt.securityaccess.SecurityAccessUserCacheResolver;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.time.DateUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.autoconfigure.web.ServerProperties;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author huangzqa
|
|
|
+ * @date 2021/4/15
|
|
|
+ **/
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class SecurityAccessTokenResolver implements ISecurityAccessTokenResolver {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SecurityAccessTokenProperties secAccessProp;
|
|
|
+
|
|
|
+ @Autowired(required = false)
|
|
|
+ private SecurityAccessUserCacheResolver cacheResolver;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ServerProperties serverProperties;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BimBusiness bimBusiness;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DcucApproveProperties dcucApproveProperties;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TokenOperate tokenComponent;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BaseSecurityUser resolve(HttpServletRequest request) {
|
|
|
+ String contextPath = serverProperties.getServlet().getContextPath();
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ log.debug("Security filter origin uri:{}", requestUri);
|
|
|
+
|
|
|
+ // 去除上下文
|
|
|
+ requestUri = requestUri.substring(contextPath.length());
|
|
|
+
|
|
|
+ log.debug("Security filter not context uri:{}", requestUri);
|
|
|
+ String huaweiLoginFilterUrl = dcucApproveProperties.getHuaweiLoginFilterUrl();
|
|
|
+ String[] splitUrls = huaweiLoginFilterUrl.split(StrUtil.COMMA);
|
|
|
+
|
|
|
+ log.debug("splitUrls:{} .", JsonUtils.toJSONString(splitUrls));
|
|
|
+
|
|
|
+ if (!requestUri.equalsIgnoreCase(Constants.API_USER_INFO)) {
|
|
|
+ if (UrlMatcher.matches(requestUri, splitUrls)) {
|
|
|
+ log.debug("URI:{} Not need get user info.", requestUri);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ String userToken = request.getHeader(secAccessProp.getUserTokenHeaderName());
|
|
|
+ String appToken = request.getHeader(Constants.APP_TOKEN);
|
|
|
+
|
|
|
+ log.info("userToken:{},appToken:{}", userToken, appToken);
|
|
|
+
|
|
|
+ if (cacheResolver != null) {
|
|
|
+ //从缓存中取
|
|
|
+ SecurityUser securityUserCache = (SecurityUser) cacheResolver.getIfPresent(cacheResolver.cacheKey(userToken));
|
|
|
+
|
|
|
+ if (securityUserCache != null) {
|
|
|
+ log.info("Cache securityUserCache:{}", JsonUtils.toJSONString(securityUserCache));
|
|
|
+
|
|
|
+ return securityUserCache;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isBlank(userToken)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ TokenInfo tokenOauth = getTokenOauth();
|
|
|
+ String token = tokenOauth.getAccessToken();
|
|
|
+ BimUserInfoItemRespVO userInfoItemRespVO = bimBusiness.getUserInfoByUserToken(userToken, token);
|
|
|
+ String sfzh = userInfoItemRespVO.getSfzh();
|
|
|
+
|
|
|
+ log.info("idcard:{}, userInfo :{}", sfzh, JsonUtils.toJSONString(userInfoItemRespVO));
|
|
|
+
|
|
|
+ SecurityUser securityUser = new SecurityUser();
|
|
|
+ // 这里使用的是华为认证的id不是用户中心的id
|
|
|
+ securityUser.setId(userInfoItemRespVO.getYhId());
|
|
|
+ securityUser.setName(userInfoItemRespVO.getXm());
|
|
|
+ securityUser.setUserName(userInfoItemRespVO.getSfzh());
|
|
|
+ securityUser.setPoliceNo(userInfoItemRespVO.getJh());
|
|
|
+ securityUser.setIdcard(userInfoItemRespVO.getSfzh());
|
|
|
+ securityUser.setSecurityOrg(userInfoItemRespVO.getDwdm());
|
|
|
+ securityUser.setSecurityOrgName(userInfoItemRespVO.getDwmc());
|
|
|
+
|
|
|
+ //放入缓存
|
|
|
+ if (cacheResolver != null) {
|
|
|
+ cacheResolver.put(cacheResolver.cacheKey(userToken), securityUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("return idcard:{}, securityUser :{}", sfzh, JsonUtils.toJSONString(securityUser));
|
|
|
+
|
|
|
+ return securityUser;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取token信息
|
|
|
+ *
|
|
|
+ * @return token信息
|
|
|
+ */
|
|
|
+ public TokenInfo getTokenOauth() {
|
|
|
+ TokenInfo tokenInfo = tokenComponent.fetchTokenInfo();
|
|
|
+
|
|
|
+ Date currentDate = new Date();
|
|
|
+ //token正常直接返回
|
|
|
+ if (null != tokenInfo && currentDate.before(tokenInfo.getOverdueTime())) {
|
|
|
+ log.debug("HwTokenInfo=【{}】", JSON.toJSONString(tokenInfo));
|
|
|
+ return tokenInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (tokenInfo != null) {
|
|
|
+ String accessToken = tokenInfo.getAccessToken();
|
|
|
+ bimBusiness.logoutToken(accessToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ //调用竹云认证获取 token
|
|
|
+ String token = bimBusiness.getToken();
|
|
|
+ tokenInfo = new TokenInfo();
|
|
|
+ tokenInfo.setAccessToken(token);
|
|
|
+
|
|
|
+ //设置缓存
|
|
|
+ Integer bimTokenExpireSecond = dcucApproveProperties.getBimTokenExpireSecond();
|
|
|
+ tokenInfo.setOverdueTime(DateUtils.addSeconds(currentDate, bimTokenExpireSecond));
|
|
|
+ tokenComponent.pushHwTokenInfo(tokenInfo);
|
|
|
+ return tokenInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|