13 changed files with 220 additions and 27 deletions
@ -0,0 +1,84 @@ |
|||
package com.epmet.auth; |
|||
|
|||
import com.epmet.commons.security.jwt.JwtUtils; |
|||
import com.epmet.commons.security.sign.openapi.OpenApiSignUtils; |
|||
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|||
import com.epmet.commons.tools.exception.RenException; |
|||
import com.epmet.commons.tools.redis.RedisKeys; |
|||
import com.epmet.commons.tools.redis.RedisUtils; |
|||
import com.epmet.commons.tools.utils.Result; |
|||
import com.epmet.commons.tools.utils.SpringContextUtils; |
|||
import com.epmet.feign.EpmetCommonServiceOpenFeignClient; |
|||
import io.jsonwebtoken.Claims; |
|||
import io.jsonwebtoken.Jwts; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.http.server.reactive.ServerHttpRequest; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.web.server.ServerWebExchange; |
|||
|
|||
/** |
|||
* 外部应用认证处理器:来平台token的方式 |
|||
*/ |
|||
@Component |
|||
public class ExtAppFetchTokenAuthProcessor extends ExtAppAuthProcessor { |
|||
|
|||
@Autowired |
|||
private JwtUtils jwtTokenUtils; |
|||
|
|||
@Autowired |
|||
private RedisUtils redisUtils; |
|||
|
|||
@Override |
|||
public void auth(String appId, String token, Long ts, ServerWebExchange exchange) { |
|||
// 这种方式不需要其他平台传appId,因此我们自己从redis中取
|
|||
appId = (String) redisUtils.get(RedisKeys.getOpenApiAccessTokenKey(token)); |
|||
|
|||
// 1.token过期校验
|
|||
if (StringUtils.isBlank(appId)) { |
|||
throw new RenException(EpmetErrorCode.OPEN_API_SIGN_TOKEN_EXPIRED.getCode(), |
|||
EpmetErrorCode.OPEN_API_SIGN_TOKEN_EXPIRED.getMsg()); |
|||
} |
|||
|
|||
String secret = getSecret(appId); |
|||
|
|||
if (jwtTokenUtils.isTokenExpired(jwtTokenUtils.getExpiration(token, secret))) { |
|||
throw new RenException(EpmetErrorCode.OPEN_API_SIGN_TOKEN_EXPIRED.getCode(), |
|||
EpmetErrorCode.OPEN_API_SIGN_TOKEN_EXPIRED.getMsg()); |
|||
} |
|||
|
|||
// 2.验签
|
|||
// 验签暂时放到具体接口中
|
|||
//openApiSignUtils.checkSign();
|
|||
|
|||
// 2. 获取claims
|
|||
Claims claims = jwtTokenUtils.getClaimByToken(token, secret); |
|||
appId = claims.get("appId", String.class); |
|||
|
|||
if (!StringUtils.isBlank(appId)) { |
|||
ServerHttpRequest.Builder mutate = exchange.getRequest().mutate(); |
|||
mutate.header("appId", appId); |
|||
exchange.mutate().request(mutate.build()).build(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* @Description 获取秘钥 |
|||
* @return |
|||
* @author wxz |
|||
* @date 2021.03.23 14:12 |
|||
*/ |
|||
private String getSecret(String appId) { |
|||
EpmetCommonServiceOpenFeignClient commonService = SpringContextUtils.getBean(EpmetCommonServiceOpenFeignClient.class); |
|||
Result<String> result = commonService.getSecret(appId); |
|||
if (result == null || !result.success()) { |
|||
throw new RenException("fetchToken方式的外部应用认证,获取secret失败"); |
|||
} |
|||
String secret = result.getData(); |
|||
if (StringUtils.isBlank(secret)) { |
|||
throw new RenException("fetchToken方式的外部应用认证,获取secret失败"); |
|||
} |
|||
|
|||
return secret; |
|||
} |
|||
} |
@ -0,0 +1,15 @@ |
|||
package com.epmet.dto.form.openapi; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
|
|||
@Data |
|||
public class GetOrgDetailFormDTO extends OpenApiBaseFormDTO { |
|||
|
|||
@NotBlank(message = "orgId不能为空") |
|||
private String orgId; |
|||
|
|||
private String test; |
|||
|
|||
} |
@ -0,0 +1,13 @@ |
|||
package com.epmet.dto.form.openapi; |
|||
|
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* open api基础类 |
|||
*/ |
|||
@Data |
|||
public class OpenApiBaseFormDTO { |
|||
|
|||
private String sign; |
|||
|
|||
} |
@ -0,0 +1,63 @@ |
|||
package com.epmet.controller; |
|||
|
|||
import com.epmet.commons.security.sign.openapi.OpenApiSignUtils; |
|||
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|||
import com.epmet.commons.tools.exception.RenException; |
|||
import com.epmet.commons.tools.redis.RedisKeys; |
|||
import com.epmet.commons.tools.redis.RedisUtils; |
|||
import com.epmet.commons.tools.utils.ConvertUtils; |
|||
import com.epmet.commons.tools.utils.Result; |
|||
import com.epmet.dto.form.openapi.GetOrgDetailFormDTO; |
|||
import com.epmet.feign.EpmetCommonServiceOpenFeignClient; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import java.util.Map; |
|||
|
|||
@RestController |
|||
@RequestMapping("open-api") |
|||
public class OpenApiOrgController { |
|||
|
|||
@Autowired |
|||
private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; |
|||
|
|||
@Autowired |
|||
private RedisUtils redisUtils; |
|||
|
|||
@PostMapping("/get-org-detail") |
|||
public Result getOrgDetail(@RequestBody GetOrgDetailFormDTO input, |
|||
@RequestHeader("appId") String appId) { |
|||
// 验签
|
|||
Map<String, String> params = null; |
|||
try { |
|||
params = ConvertUtils.entityToMap(input); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
if (!OpenApiSignUtils.checkSign(params, getSecret(appId))) { |
|||
// 验签失败,抛出异常提示
|
|||
throw new RenException(EpmetErrorCode.OPEN_API_SIGN_ERROR.getCode()); |
|||
} |
|||
|
|||
return new Result().ok("测试org"); |
|||
} |
|||
|
|||
private String getSecret(String appId) { |
|||
String secret = (String)redisUtils.get(RedisKeys.getExternalAppSecretKey(appId)); |
|||
if (StringUtils.isBlank(secret)) { |
|||
Result<String> result = commonServiceOpenFeignClient.getSecret(appId); |
|||
if (!result.success()) { |
|||
throw new RenException("调用common service查询secret失败"); |
|||
} |
|||
secret = result.getData(); |
|||
if (StringUtils.isBlank(secret)) { |
|||
throw new RenException(String.format("根据appId%s没有找到对应的secret", appId)); |
|||
} |
|||
redisUtils.set(RedisKeys.getExternalAppSecretKey(appId), secret); |
|||
} |
|||
return secret; |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue