|
@@ -1,134 +0,0 @@
|
|
|
-package com.dragonsoft.dcuc.approvegateway.filter;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.dragonsoft.approve.common.ErrorCode;
|
|
|
-import com.dragonsoft.approve.model.TokenInfo;
|
|
|
-import com.dragonsoft.dcuc.approvegateway.pojo.OauthUserVo;
|
|
|
-import com.dragonsoft.duceap.base.entity.security.BaseSecurityUser;
|
|
|
-import com.dragonsoft.duceap.base.exception.ApplicationException;
|
|
|
-import com.dragonsoft.duceap.base.utils.UserContextUtils;
|
|
|
-import com.dragonsoft.duceap.core.entity.response.ResponseResult;
|
|
|
-import com.dragonsoft.duceap.security.jwt.JwtTokenUtils;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.netflix.zuul.ZuulFilter;
|
|
|
-import com.netflix.zuul.context.RequestContext;
|
|
|
-import com.netflix.zuul.exception.ZuulException;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.http.*;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.LinkedMultiValueMap;
|
|
|
-import org.springframework.util.MultiValueMap;
|
|
|
-import org.springframework.web.client.RestTemplate;
|
|
|
-
|
|
|
-import java.util.Optional;
|
|
|
-
|
|
|
-@Component
|
|
|
-public class DcucJwtTokenPreFilter extends ZuulFilter {
|
|
|
-
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(DcucJwtTokenPreFilter.class);
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取用户信息接口
|
|
|
- */
|
|
|
- private static final String AUTH_USER_PATH = "/dcuc/api/user-service/v2/users/oauth";
|
|
|
-
|
|
|
- /**
|
|
|
- * 认证服务url
|
|
|
- */
|
|
|
- @Value("${approve.filter.dcuc.host:}")
|
|
|
- private String oauthServiceHost;
|
|
|
-
|
|
|
- /**
|
|
|
- * 华为网关是否开启
|
|
|
- */
|
|
|
- @Value("${approve.filter.dcuc.enabled:true}")
|
|
|
- private Boolean dcucEnabled;
|
|
|
-
|
|
|
- @Override
|
|
|
- public String filterType() {
|
|
|
- return "pre";
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int filterOrder() {
|
|
|
- return -10;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean shouldFilter() {
|
|
|
- return dcucEnabled;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Object run() throws ZuulException {
|
|
|
- RequestContext ctx = RequestContext.getCurrentContext();
|
|
|
- BaseSecurityUser currentUser = UserContextUtils.getCurrentUser();
|
|
|
- logger.info("====登录用户信息:{}====", JSONObject.toJSONString(currentUser));
|
|
|
- if (currentUser != null) {
|
|
|
- String jwtToken = JwtTokenUtils.getAlgorithmGen(JwtTokenUtils.AlgorithmType.HS256).sign(currentUser);
|
|
|
- ctx.addZuulRequestHeader(JwtTokenUtils.AUTHORIZATION_HEADER, JwtTokenUtils.TOKEN_PREFIX + jwtToken);
|
|
|
- logger.info("登录jwtToken:{}", jwtToken);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- private BaseSecurityUser getSecurityUser() {
|
|
|
- RequestContext ctx = RequestContext.getCurrentContext();
|
|
|
-
|
|
|
- // logger.info();
|
|
|
- String userToken = ctx.getRequest().getHeader("token");
|
|
|
- String appToken = ctx.getRequest().getHeader("appToken");
|
|
|
-
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- //设置ContentType
|
|
|
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
- //应用token
|
|
|
- headers.add("dcucAppToken", appToken);
|
|
|
- //用户token
|
|
|
- headers.add("dcucUserToken", userToken);
|
|
|
- logger.info("userToken=【{}】,appToken=【{}】", userToken, appToken);
|
|
|
-
|
|
|
- //是否需要获取华为网关accessToken,公司内部无华为网关环境
|
|
|
- if (Optional.ofNullable(ctx.get(HwTokenPreFilter.HW_TOKEN)).isPresent()) {
|
|
|
- String accessToken = ((TokenInfo) ctx.get(HwTokenPreFilter.HW_TOKEN)).getAccessToken();
|
|
|
- String authorization = JwtTokenUtils.TOKEN_PREFIX + accessToken;
|
|
|
- headers.add("Authorization", authorization);
|
|
|
- logger.info("华为accessToken=【{}】", accessToken);
|
|
|
- }
|
|
|
-
|
|
|
- BaseSecurityUser baseSecurityUser = null;
|
|
|
- try {
|
|
|
- HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(new LinkedMultiValueMap(), headers);
|
|
|
- RestTemplate restTemplate = new RestTemplate();
|
|
|
- //远程调用用户中心接口
|
|
|
- ResponseEntity<ResponseResult> exchange = restTemplate.exchange(oauthServiceHost + AUTH_USER_PATH, HttpMethod.GET, requestEntity, ResponseResult.class);
|
|
|
- logger.info("远程调用返回结果resEntity=【{}】,请求url=【{}】", JSONObject.toJSONString(exchange), oauthServiceHost + AUTH_USER_PATH);
|
|
|
- //判断请求是否成功
|
|
|
- if (exchange.getStatusCode() == HttpStatus.OK) {
|
|
|
- ResponseResult responseResult = exchange.getBody();
|
|
|
- if (String.valueOf(HttpStatus.OK.value()).equals(responseResult.getStatusCode())) {
|
|
|
- OauthUserVo oauthUserVo = new ObjectMapper().convertValue(responseResult.getResult(), OauthUserVo.class);
|
|
|
- baseSecurityUser = new BaseSecurityUser();
|
|
|
- baseSecurityUser.setId(oauthUserVo.getId());
|
|
|
- baseSecurityUser.setName(oauthUserVo.getName());
|
|
|
- baseSecurityUser.setPoliceNo(oauthUserVo.getPoliceNumber());
|
|
|
- baseSecurityUser.setSecurityOrg(oauthUserVo.getOrgCode());
|
|
|
-// baseSecurityUser.setCode(oauthUserVo.getOrgCode());
|
|
|
-// baseSecurityUser.setUserName();
|
|
|
-// baseSecurityUser.setSecurityRoles();
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error("用户信息获取失败", e);
|
|
|
- throw new ApplicationException(ErrorCode.USER_INFO_ERROR.getCode(), ErrorCode.USER_INFO_ERROR.getMsg());
|
|
|
- }
|
|
|
- if (null == baseSecurityUser) {
|
|
|
- logger.error("用户信息获取失败,用户信息为空!");
|
|
|
- throw new ApplicationException(ErrorCode.USER_INFO_ERROR.getCode(), ErrorCode.USER_INFO_ERROR.getMsg());
|
|
|
- }
|
|
|
-
|
|
|
- return baseSecurityUser;
|
|
|
- }
|
|
|
-}
|