parent
91b924bdc1
commit
21b03137d1
@ -1,23 +1,48 @@ |
||||
package com.gitee.sop.gateway.config; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.gitee.sop.gatewaycommon.bean.ApiConfig; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.lang.StringUtils; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.data.redis.core.StringRedisTemplate; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.annotation.PostConstruct; |
||||
|
||||
@Slf4j |
||||
@Component |
||||
public class MyConfig { |
||||
|
||||
@Autowired |
||||
private StringRedisTemplate redisTemplate; |
||||
private static String APP_AUTH_TOKEN_PREFIX = "com.gitee.sop.oauth2_access_token:"; |
||||
|
||||
public static String getAccessTokenKey(String accessToken) { |
||||
return APP_AUTH_TOKEN_PREFIX + accessToken; |
||||
} |
||||
|
||||
|
||||
@PostConstruct |
||||
public void after() { |
||||
ApiConfig.getInstance().setTokenValidator(apiParam -> { |
||||
// 获取客户端传递过来的token
|
||||
String token = apiParam.fetchAccessToken(); |
||||
return !StringUtils.isBlank(token); |
||||
// TODO: 校验token有效性,可以从redis中读取
|
||||
try { |
||||
String token = apiParam.fetchAccessToken(); |
||||
if (StringUtils.isBlank(token)) { |
||||
return Boolean.FALSE; |
||||
} |
||||
String json = redisTemplate.opsForValue().get(getAccessTokenKey(token)); |
||||
if(StringUtils.isEmpty(json)) { |
||||
return Boolean.FALSE; |
||||
} |
||||
|
||||
// 返回true表示这个token真实、有效
|
||||
JSONObject userInfo = JSONObject.parseObject(json); |
||||
return userInfo != null; |
||||
} catch (Exception e) { |
||||
log.error("redisError", e); |
||||
return Boolean.FALSE; |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -1,8 +1,14 @@ |
||||
package com.gitee.sop.openapi; |
||||
|
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
||||
|
||||
@EnableDiscoveryClient |
||||
@SpringBootApplication |
||||
public class SopOpenapiApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
|
||||
SpringApplication.run(SopOpenapiApplication.class, args); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,20 @@ |
||||
package com.gitee.sop.openapi.service; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/4 15:41 |
||||
*/ |
||||
public interface AuthService { |
||||
|
||||
int FORBIDDEN = 2; |
||||
|
||||
/** 0 插件应用未授权 **/ |
||||
int UNAUTHORIZED = 0; |
||||
/** 1 插件应用已授权 **/ |
||||
int AUTHORIZED = 1; |
||||
|
||||
Byte ENABLE_TOKEN = 1; |
||||
Byte DISABLE_TOKEN = 0; |
||||
|
||||
Long CODE_TIMEOUT = 3600L; |
||||
} |
@ -0,0 +1,83 @@ |
||||
package com.gitee.sop.openapi.service; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.gitee.sop.openapi.config.RedisOperator; |
||||
import com.gitee.sop.openapi.entity.IsvAuthTokenEntity; |
||||
import com.gitee.sop.openapi.mapper.IsvAuthTokenMapper; |
||||
import com.gitee.sop.openapi.po.AuthCodePo; |
||||
import com.gitee.sop.openapi.po.AuthTokePo; |
||||
import com.gitee.sop.openapi.util.GrantType; |
||||
import com.gitee.sop.openapi.util.MD5GeneratorUtil; |
||||
import com.gitee.sop.openapi.util.RedisKeyUtil; |
||||
import com.gitee.sop.openapi.vo.AuthTokenVo; |
||||
import com.gitee.sop.servercommon.exception.ServiceException; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/4 15:21 |
||||
*/ |
||||
@Service |
||||
public class AuthTokenService implements AuthService { |
||||
|
||||
private final RedisOperator redisOperator; |
||||
private final IsvAuthTokenMapper isvAuthTokenMapper; |
||||
|
||||
public AuthTokenService(final RedisOperator redisOperator, |
||||
final IsvAuthTokenMapper isvAuthTokenMapper) { |
||||
this.redisOperator = redisOperator; |
||||
this.isvAuthTokenMapper = isvAuthTokenMapper; |
||||
} |
||||
|
||||
|
||||
public AuthTokenVo getToken(AuthTokePo authTokePo) { |
||||
|
||||
AuthTokenVo tokenVo = new AuthTokenVo(); |
||||
String grantType = authTokePo.getGrantType(); |
||||
if (GrantType.AUTHORIZATION_CODE.name().equals(grantType)) { |
||||
String codeKey = RedisKeyUtil.getCodeKey(authTokePo.getCode()); |
||||
Object jsonObj = redisOperator.get(codeKey); |
||||
if (jsonObj == null) { |
||||
throw new ServiceException("Invalid request"); |
||||
} |
||||
|
||||
AuthCodePo codePo = JSONObject.parseObject(jsonObj.toString(), AuthCodePo.class); |
||||
if (codePo == null) { |
||||
throw new ServiceException("Can not found user by code"); |
||||
} |
||||
IsvAuthTokenEntity authTokenEntity = isvAuthTokenMapper.getByColumn("app_auth_code", authTokePo.getCode()); |
||||
if (authTokenEntity == null) { |
||||
throw new ServiceException("Can not found user by code"); |
||||
} |
||||
|
||||
|
||||
String authToken = MD5GeneratorUtil.generateValue(); |
||||
String refreshToken = MD5GeneratorUtil.generateValue(); |
||||
|
||||
|
||||
authTokenEntity.setStatus((byte) AUTHORIZED); |
||||
authTokenEntity.setAppAuthToken(authToken); |
||||
authTokenEntity.setAppRefreshToken(refreshToken); |
||||
isvAuthTokenMapper.update(authTokenEntity); |
||||
|
||||
String authTokenKey = RedisKeyUtil.getAuthTokenKey(authToken); |
||||
String refreshTokenKey = RedisKeyUtil.getRefreshTokenKey(refreshToken); |
||||
redisOperator.set(authTokenKey, codePo); |
||||
redisOperator.set(refreshTokenKey, codePo); |
||||
|
||||
tokenVo.setAppAuthToken(authToken); |
||||
tokenVo.setAppRefreshToken(refreshToken); |
||||
tokenVo.setUserId(codePo.getUserId()); |
||||
return tokenVo; |
||||
} else if (GrantType.REFRESH_TOKEN.name().equals(grantType)) { |
||||
|
||||
|
||||
return tokenVo; |
||||
} else { |
||||
throw new ServiceException("Invalid grant"); |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,23 @@ |
||||
package com.gitee.sop.openapi.util; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/4 15:25 |
||||
*/ |
||||
public enum GrantType { |
||||
|
||||
AUTHORIZATION_CODE("authorization_code"), |
||||
PASSWORD("password"), |
||||
REFRESH_TOKEN("refresh_token"), |
||||
CLIENT_CREDENTIALS("client_credentials"); |
||||
|
||||
private String grantType; |
||||
|
||||
private GrantType(String grantType) { |
||||
this.grantType = grantType; |
||||
} |
||||
|
||||
public String toString() { |
||||
return this.grantType; |
||||
} |
||||
} |
@ -0,0 +1,40 @@ |
||||
package com.gitee.sop.openapi.vo; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/4 15:22 |
||||
*/ |
||||
@Data |
||||
public class AuthTokenVo { |
||||
|
||||
/** |
||||
* 授权令牌 |
||||
*/ |
||||
private String appAuthToken; |
||||
|
||||
/** |
||||
* 用户id |
||||
*/ |
||||
private Long userId; |
||||
|
||||
|
||||
/** |
||||
* 令牌有效期. -1:永久有效 |
||||
*/ |
||||
private long expiresIn = -1; |
||||
|
||||
/** |
||||
* 刷新令牌有效期. -1:永久有效 |
||||
*/ |
||||
private long reExpiresIn = -1; |
||||
|
||||
|
||||
/** |
||||
* 刷新令牌时使用。 |
||||
* 刷新令牌后,我们会保证老的app_auth_token从刷新开始10分钟内可继续使用,请及时替换为最新token |
||||
*/ |
||||
private String appRefreshToken; |
||||
|
||||
} |
@ -1,6 +1,6 @@ |
||||
spring.profiles.active=dev |
||||
|
||||
server.port=8089 |
||||
spring.application.name=sop-openapi |
||||
spring.application.name=open-apis |
||||
# 注册中心 |
||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 |
Loading…
Reference in new issue