parent
1370883af9
commit
91b924bdc1
@ -0,0 +1,110 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<groupId>com.gitee.sop</groupId> |
||||
<artifactId>sop-parent</artifactId> |
||||
<version>4.0.3-SNAPSHOT</version> |
||||
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository --> |
||||
</parent> |
||||
<groupId>com.gitee.sop.openapi</groupId> |
||||
<artifactId>sop-openapi</artifactId> |
||||
<name>sop-openapi</name> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>com.gitee.sop</groupId> |
||||
<artifactId>sop-service-common</artifactId> |
||||
<version>4.0.3-SNAPSHOT</version> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-web</artifactId> |
||||
</dependency> |
||||
|
||||
<!-- 使用nacos注册中心 --> |
||||
<dependency> |
||||
<groupId>com.alibaba.cloud</groupId> |
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.springframework.cloud</groupId> |
||||
<artifactId>spring-cloud-starter-openfeign</artifactId> |
||||
</dependency> |
||||
|
||||
<!-- swagger2 --> |
||||
<dependency> |
||||
<groupId>io.springfox</groupId> |
||||
<artifactId>springfox-swagger2</artifactId> |
||||
<version>2.9.2</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>com.github.xiaoymin</groupId> |
||||
<artifactId>swagger-bootstrap-ui</artifactId> |
||||
<version>1.9.5</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>io.springfox</groupId> |
||||
<artifactId>springfox-swagger-ui</artifactId> |
||||
<version>2.9.2</version> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-test</artifactId> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
|
||||
|
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-data-redis</artifactId> |
||||
</dependency> |
||||
<!-- redis 依赖commons-pool --> |
||||
<dependency> |
||||
<groupId>org.apache.commons</groupId> |
||||
<artifactId>commons-lang3</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>mysql</groupId> |
||||
<artifactId>mysql-connector-java</artifactId> |
||||
<scope>runtime</scope> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>net.oschina.durcframework</groupId> |
||||
<artifactId>fastmybatis-spring-boot-starter</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
|
||||
<build> |
||||
<plugins> |
||||
<!-- 打包时跳过测试 --> |
||||
<plugin> |
||||
<groupId>org.apache.maven.plugins</groupId> |
||||
<artifactId>maven-surefire-plugin</artifactId> |
||||
<version>2.12.4</version> |
||||
<configuration> |
||||
<skipTests>true</skipTests> |
||||
</configuration> |
||||
</plugin> |
||||
<plugin> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
|
||||
<repositories> |
||||
<repository> |
||||
<id>spring-milestones</id> |
||||
<name>Spring Milestones</name> |
||||
<url>https://repo.spring.io/milestone</url> |
||||
</repository> |
||||
</repositories> |
||||
</project> |
@ -0,0 +1,8 @@ |
||||
package com.gitee.sop.openapi; |
||||
|
||||
public class SopOpenapiApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
package com.gitee.sop.openapi.config; |
||||
|
||||
import com.gitee.sop.servercommon.bean.ServiceConfig; |
||||
import com.gitee.sop.servercommon.swagger.SwaggerSupport; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2; |
||||
|
||||
@Configuration |
||||
public class OpenapiConfig { |
||||
|
||||
static { |
||||
ServiceConfig.getInstance().getI18nModules().add("i18n/isp/auth_error"); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 开启文档,本地微服务文档地址:http://localhost:2222/doc.html
|
||||
* http://ip:port/v2/api-docs
|
||||
*/ |
||||
@Configuration |
||||
@EnableSwagger2 |
||||
public static class Swagger2 extends SwaggerSupport { |
||||
@Override |
||||
protected String getDocTitle() { |
||||
return "开放平台API"; |
||||
} |
||||
|
||||
@Override |
||||
protected boolean swaggerAccessProtected() { |
||||
return false; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,43 @@ |
||||
package com.gitee.sop.openapi.config; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect; |
||||
import com.fasterxml.jackson.annotation.PropertyAccessor; |
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.data.redis.connection.RedisConnectionFactory; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; |
||||
import org.springframework.data.redis.serializer.StringRedisSerializer; |
||||
|
||||
@Configuration |
||||
public class RedisConfig { |
||||
|
||||
|
||||
@Bean |
||||
public RedisTemplate<String, Object> redisTemplate(@Autowired RedisConnectionFactory redisConnectionFactory) { |
||||
|
||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); |
||||
redisTemplate.setConnectionFactory(redisConnectionFactory); |
||||
|
||||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); |
||||
ObjectMapper objectMapper = new ObjectMapper(); |
||||
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
||||
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
||||
jackson2JsonRedisSerializer.setObjectMapper(objectMapper); |
||||
|
||||
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); |
||||
|
||||
// key采用String的序列化方式
|
||||
redisTemplate.setKeySerializer(stringRedisSerializer); |
||||
// hash的key也采用String的序列化方式
|
||||
redisTemplate.setHashKeySerializer(stringRedisSerializer); |
||||
// value采用jackson序列化方式
|
||||
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); |
||||
// hash的value采用jackson序列化方式
|
||||
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); |
||||
redisTemplate.afterPropertiesSet(); |
||||
return redisTemplate; |
||||
} |
||||
} |
@ -0,0 +1,80 @@ |
||||
package com.gitee.sop.openapi.config; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 17:22 |
||||
*/ |
||||
@Slf4j |
||||
@Component |
||||
public class RedisOperator { |
||||
|
||||
private final RedisTemplate<String, Object> redisTemplate; |
||||
|
||||
public RedisOperator(final RedisTemplate<String, Object> redisTemplate) { |
||||
this.redisTemplate = redisTemplate; |
||||
} |
||||
|
||||
public boolean expire(String key, long time) { |
||||
try { |
||||
if (time >= 0) { |
||||
redisTemplate.expire(key, time, TimeUnit.SECONDS); |
||||
} |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisTemplate.expire.Error", e); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
|
||||
public long getExpire(String key) { |
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS); |
||||
} |
||||
|
||||
public Object get(String key) { |
||||
return key == null ? null : redisTemplate.opsForValue().get(key); |
||||
} |
||||
|
||||
public boolean set(String key, Object value) { |
||||
try { |
||||
redisTemplate.opsForValue().set(key, value); |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisTemplate.set.Error", e); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 普通缓存放入并设置时间 |
||||
* |
||||
* @param key 键 |
||||
* @param value 值 |
||||
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 |
||||
* @return true成功 false 失败 |
||||
*/ |
||||
public boolean set(String key, Object value, long time) { |
||||
try { |
||||
if (time > 0) { |
||||
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); |
||||
} else { |
||||
set(key, value); |
||||
} |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisTemplate.setAndExpire.Error", e); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
|
||||
public boolean del(String key) { |
||||
return redisTemplate.delete(key); |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
package com.gitee.sop.openapi.controller; |
||||
|
||||
import com.gitee.sop.openapi.po.AuthCodePo; |
||||
import com.gitee.sop.openapi.vo.AuthCodeVo; |
||||
import com.gitee.sop.openapi.service.AuthCodeService; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.validation.constraints.NotBlank; |
||||
|
||||
@Slf4j |
||||
@RestController |
||||
@Api(tags = "超浏授权接口") |
||||
public class AuthCodeController { |
||||
|
||||
|
||||
private final AuthCodeService authCodeService; |
||||
|
||||
public AuthCodeController(final AuthCodeService authCodeService) { |
||||
this.authCodeService = authCodeService; |
||||
} |
||||
|
||||
|
||||
@ApiOperation(value = "获取code") |
||||
@PostMapping("/auth/code") |
||||
public AuthCodeVo getCode(@RequestBody AuthCodePo authCodePo) { |
||||
return authCodeService.getCode(authCodePo); |
||||
} |
||||
|
||||
|
||||
@ApiOperation(value = "解除授权") |
||||
@PostMapping("/auth/cancel") |
||||
public void cancelToken(@NotBlank(message = "{isp.auth.user_id_empty_error}") Long userId) { |
||||
authCodeService.cancelToken(userId); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,27 @@ |
||||
package com.gitee.sop.openapi.controller; |
||||
|
||||
import com.gitee.sop.openapi.po.AuthTokePo; |
||||
import com.gitee.sop.servercommon.annotation.Open; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
@Slf4j |
||||
@RestController |
||||
@Api(tags = "插件应用获取token接口") |
||||
public class AuthTokenController { |
||||
|
||||
|
||||
|
||||
@Open(value = "auth.get.token", ignoreValidate = true, mergeResult = false, needToken = false) |
||||
@PostMapping(value = "/token") |
||||
@ApiOperation(value = "code换token", notes = "插件服务器获取token") |
||||
public String getToken(@RequestBody AuthTokePo tokePo) { |
||||
|
||||
|
||||
return ""; |
||||
} |
||||
} |
@ -0,0 +1,37 @@ |
||||
package com.gitee.sop.openapi.entity; |
||||
|
||||
import lombok.Data; |
||||
import org.springframework.data.annotation.Id; |
||||
|
||||
import javax.persistence.Column; |
||||
import javax.persistence.GeneratedValue; |
||||
import javax.persistence.GenerationType; |
||||
import javax.persistence.Table; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 16:57 |
||||
*/ |
||||
|
||||
@Data |
||||
@Table(name = "isv_auth_token") |
||||
public class IsvAuthTokenEntity { |
||||
|
||||
@Id |
||||
@Column(name = "id") |
||||
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
/** 数据库字段:id */ |
||||
private Long id; |
||||
|
||||
/** appKey, 数据库字段:app_key */ |
||||
private String appKey; |
||||
|
||||
/** userId, 数据库字段:user_id */ |
||||
private Long userId; |
||||
|
||||
/** 1启用,2禁用, 数据库字段:status */ |
||||
private Byte status; |
||||
|
||||
/** appAuthToken 数据库字段:app_auth_token */ |
||||
private String appAuthToken; |
||||
} |
@ -0,0 +1,30 @@ |
||||
package com.gitee.sop.openapi.entity; |
||||
|
||||
import lombok.Data; |
||||
import org.springframework.data.annotation.Id; |
||||
|
||||
import javax.persistence.Column; |
||||
import javax.persistence.GeneratedValue; |
||||
import javax.persistence.GenerationType; |
||||
import javax.persistence.Table; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 16:22 |
||||
*/ |
||||
@Data |
||||
@Table(name = "isv_info") |
||||
public class IsvInfoEntity { |
||||
|
||||
@Id |
||||
@Column(name = "id") |
||||
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
/** 数据库字段:id */ |
||||
private Long id; |
||||
|
||||
/** appKey, 数据库字段:app_key */ |
||||
private String appKey; |
||||
|
||||
/** 1启用,2禁用, 数据库字段:status */ |
||||
private Byte status; |
||||
} |
@ -0,0 +1,11 @@ |
||||
package com.gitee.sop.openapi.mapper; |
||||
|
||||
import com.gitee.fastmybatis.core.mapper.CrudMapper; |
||||
import com.gitee.sop.openapi.entity.IsvAuthTokenEntity; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 16:59 |
||||
*/ |
||||
public interface IsvAuthTokenMapper extends CrudMapper<IsvAuthTokenEntity, Long> { |
||||
} |
@ -0,0 +1,11 @@ |
||||
package com.gitee.sop.openapi.mapper; |
||||
|
||||
import com.gitee.fastmybatis.core.mapper.CrudMapper; |
||||
import com.gitee.sop.openapi.entity.IsvInfoEntity; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 15:52 |
||||
*/ |
||||
public interface IsvInfoMapper extends CrudMapper<IsvInfoEntity, Long> { |
||||
} |
@ -0,0 +1,26 @@ |
||||
package com.gitee.sop.openapi.po; |
||||
|
||||
import com.gitee.sop.servercommon.param.validation.Group1; |
||||
import com.gitee.sop.servercommon.param.validation.Group2; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
import org.hibernate.validator.constraints.Length; |
||||
|
||||
import javax.validation.constraints.NotBlank; |
||||
import javax.validation.constraints.NotNull; |
||||
|
||||
|
||||
@Data |
||||
public class AuthCodePo { |
||||
|
||||
|
||||
@ApiModelProperty(value = "应用ID", required = true, example = "test1", position = 1) |
||||
@NotBlank(message = "{isp.auth.app_id_empty_error}", groups = Group1.class) |
||||
@Length(min = 1, max = 64, message = "{isp.auth.app_id_length_error}=1,64", groups = Group2.class) |
||||
private String appId; |
||||
|
||||
@ApiModelProperty(value = "用户ID", required = true, example = "1", position = 2) |
||||
@NotNull(message = "{isp.auth.user_id_empty_error}") |
||||
private Long userId; |
||||
|
||||
} |
@ -0,0 +1,46 @@ |
||||
package com.gitee.sop.openapi.po; |
||||
|
||||
import com.gitee.sop.servercommon.param.validation.Group1; |
||||
import com.gitee.sop.servercommon.param.validation.Group2; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import org.hibernate.validator.constraints.Length; |
||||
|
||||
import javax.validation.constraints.NotBlank; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 18:31 |
||||
*/ |
||||
public class AuthTokePo { |
||||
|
||||
|
||||
|
||||
/** |
||||
* 授权码.与refresh_token二选一,用户对应用授权后得到,即第一步中开发者获取到的app_auth_code值 |
||||
*/ |
||||
@ApiModelProperty(value = "应用ID", example = "test1", position = 1) |
||||
@NotBlank(message = "{isp.auth.app_id_empty_error}", groups = Group1.class) |
||||
@Length(min = 1, max = 64, message = "{isp.auth.app_id_length_error}=1,64", groups = Group2.class) |
||||
private String code; |
||||
|
||||
@NotBlank(message = "{isp.auth.user_token_empty_error}", groups = Group1.class) |
||||
@Length(min = 1, max = 64, message = "{isp.auth.user_token_length_error}=1,64", groups = Group2.class) |
||||
@ApiModelProperty(value = "用户登录凭证", required = true, example = "1111", position = 2) |
||||
private String grant_type; |
||||
|
||||
|
||||
/** |
||||
* 授权类型。<br> |
||||
* 如果使用app_auth_code换取token,则为authorization_code, |
||||
* 如果使用refresh_token换取新的token,则为refresh_token |
||||
*/ |
||||
@NotBlank(message = "授权类型不能为空") |
||||
private String grantType; |
||||
|
||||
|
||||
/** |
||||
* 刷新令牌.与code二选一,可为空,刷新令牌时使用 |
||||
*/ |
||||
private String refreshToken; |
||||
|
||||
} |
@ -0,0 +1,106 @@ |
||||
package com.gitee.sop.openapi.service; |
||||
|
||||
import com.gitee.fastmybatis.core.query.Query; |
||||
import com.gitee.sop.openapi.config.RedisOperator; |
||||
import com.gitee.sop.openapi.entity.IsvAuthTokenEntity; |
||||
import com.gitee.sop.openapi.entity.IsvInfoEntity; |
||||
import com.gitee.sop.openapi.mapper.IsvAuthTokenMapper; |
||||
import com.gitee.sop.openapi.mapper.IsvInfoMapper; |
||||
import com.gitee.sop.openapi.po.AuthCodePo; |
||||
import com.gitee.sop.openapi.vo.AuthCodeVo; |
||||
import com.gitee.sop.openapi.util.MD5GeneratorUtil; |
||||
import com.gitee.sop.openapi.util.RedisKeyUtil; |
||||
import com.gitee.sop.servercommon.exception.ServiceException; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 16:28 |
||||
*/ |
||||
@Slf4j |
||||
@Service |
||||
public class AuthCodeService { |
||||
public static final int FORBIDDEN = 2; |
||||
|
||||
/** 0 插件应用未授权 **/ |
||||
public static final int UNAUTHORIZED = 0; |
||||
/** 1 插件应用已授权 **/ |
||||
public static final int AUTHORIZED = 1; |
||||
|
||||
public static final Byte ENABLE_TOKEN = 1; |
||||
public static final Byte DISABLE_TOKEN = 0; |
||||
|
||||
private final IsvInfoMapper isvInfoMapper; |
||||
private final IsvAuthTokenMapper isvAuthTokenMapper; |
||||
private final RedisOperator redisOperator; |
||||
|
||||
public AuthCodeService(final IsvInfoMapper isvInfoMapper, |
||||
final IsvAuthTokenMapper isvAuthTokenMapper, |
||||
final RedisOperator redisOperator) { |
||||
this.isvInfoMapper = isvInfoMapper; |
||||
this.isvAuthTokenMapper = isvAuthTokenMapper; |
||||
this.redisOperator = redisOperator; |
||||
} |
||||
|
||||
public AuthCodeVo getCode(AuthCodePo authCodePo) { |
||||
|
||||
IsvInfoEntity isvInfo = isvInfoMapper.getByColumn("app_key", authCodePo.getAppId()); |
||||
if (isvInfo == null || isvInfo.getStatus().intValue() == FORBIDDEN) { |
||||
log.error("appId已禁用:{}", authCodePo.getAppId()); |
||||
throw new ServiceException("应用ID未申请或已被禁用"); |
||||
} |
||||
|
||||
// todo check userToken and get userId
|
||||
|
||||
String authCode = MD5GeneratorUtil.generateValue(); |
||||
|
||||
Query query = new Query() |
||||
.eq("app_key", authCodePo.getAppId()) |
||||
.eq("user_id", authCodePo.getUserId()) |
||||
.eq("status", ENABLE_TOKEN); |
||||
IsvAuthTokenEntity authTokenEntity = isvAuthTokenMapper.getByQuery(query); |
||||
AuthCodeVo authCodeVo = new AuthCodeVo(); |
||||
authCodeVo.setCode(authCode); |
||||
if (authTokenEntity == null) { |
||||
authCodeVo.setState(UNAUTHORIZED); |
||||
authTokenEntity = new IsvAuthTokenEntity(); |
||||
authTokenEntity.setAppAuthToken(authCode); |
||||
authTokenEntity.setAppKey(authCodePo.getAppId()); |
||||
authTokenEntity.setUserId(authCodePo.getUserId()); |
||||
isvAuthTokenMapper.save(authTokenEntity); |
||||
} else { |
||||
authCodeVo.setState(AUTHORIZED); |
||||
isvAuthTokenMapper.update(authTokenEntity); |
||||
} |
||||
|
||||
// 写入redis缓存
|
||||
String codeKey = RedisKeyUtil.getCodeKey(authCodePo.getAppId(), authCodePo.getUserId()); |
||||
redisOperator.set(codeKey, authCodePo.getUserId()); |
||||
|
||||
return authCodeVo; |
||||
} |
||||
|
||||
|
||||
public void cancelToken(Long userId) { |
||||
// todo check userToken and get userId
|
||||
|
||||
Query query = new Query() |
||||
.eq("user_id", userId) |
||||
.eq("status", ENABLE_TOKEN); |
||||
|
||||
IsvAuthTokenEntity authTokenEntity = isvAuthTokenMapper.getByQuery(query); |
||||
if (authTokenEntity == null) { |
||||
throw new ServiceException("用户未授权插件应用"); |
||||
} |
||||
|
||||
authTokenEntity.setStatus(ENABLE_TOKEN); |
||||
isvAuthTokenMapper.update(authTokenEntity); |
||||
|
||||
String tokenKey = RedisKeyUtil.getAuthTokenKey(authTokenEntity.getAppKey(), authTokenEntity.getUserId()); |
||||
String refreshKey = RedisKeyUtil.getRefreshTokenKey(authTokenEntity.getAppKey(), authTokenEntity.getUserId()); |
||||
redisOperator.del(tokenKey); |
||||
redisOperator.del(refreshKey); |
||||
|
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
package com.gitee.sop.openapi.util; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 16:42 |
||||
*/ |
||||
public enum AuthErrorEnum { |
||||
|
||||
|
||||
} |
@ -0,0 +1,27 @@ |
||||
package com.gitee.sop.openapi.util; |
||||
|
||||
import org.slf4j.helpers.FormattingTuple; |
||||
import org.slf4j.helpers.MessageFormatter; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 18:14 |
||||
*/ |
||||
public class LogFormatUtil { |
||||
|
||||
/** |
||||
* append log with pattern |
||||
* |
||||
* @param appendLogPattern like "aaa {} bbb {} ccc" |
||||
* @param appendLogArguments like "111, true" |
||||
*/ |
||||
public static String log(String appendLogPattern, Object ... appendLogArguments) { |
||||
FormattingTuple ft = MessageFormatter.arrayFormat(appendLogPattern, appendLogArguments); |
||||
return ft.getMessage(); |
||||
} |
||||
|
||||
|
||||
public static void main(String[] args) { |
||||
System.out.println(log("My name is {}, from {}", "hanmeimei", "London")); |
||||
} |
||||
} |
@ -0,0 +1,53 @@ |
||||
package com.gitee.sop.openapi.util; |
||||
|
||||
import com.gitee.sop.servercommon.exception.ServiceException; |
||||
|
||||
import java.security.MessageDigest; |
||||
import java.util.UUID; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 16:47 |
||||
*/ |
||||
public class MD5GeneratorUtil { |
||||
|
||||
private static final char[] hexCode = "0123456789abcdef".toCharArray(); |
||||
|
||||
private MD5GeneratorUtil() { |
||||
} |
||||
|
||||
public static String generateValue() throws ServiceException { |
||||
return generateValue(UUID.randomUUID().toString()); |
||||
} |
||||
|
||||
public static String toHexString(byte[] data) { |
||||
if (data == null) { |
||||
return null; |
||||
} else { |
||||
StringBuilder r = new StringBuilder(data.length * 2); |
||||
byte[] arr$ = data; |
||||
int len$ = data.length; |
||||
|
||||
for (int i$ = 0; i$ < len$; ++i$) { |
||||
byte b = arr$[i$]; |
||||
r.append(hexCode[b >> 4 & 15]); |
||||
r.append(hexCode[b & 15]); |
||||
} |
||||
|
||||
return r.toString(); |
||||
} |
||||
} |
||||
|
||||
public static String generateValue(String param) throws ServiceException { |
||||
try { |
||||
MessageDigest algorithm = MessageDigest.getInstance("MD5"); |
||||
algorithm.reset(); |
||||
algorithm.update(param.getBytes()); |
||||
byte[] messageDigest = algorithm.digest(); |
||||
return toHexString(messageDigest); |
||||
} catch (Exception var4) { |
||||
throw new ServiceException("OAuth Token cannot be generated."); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,30 @@ |
||||
package com.gitee.sop.openapi.util; |
||||
|
||||
import com.google.common.base.Joiner; |
||||
|
||||
/*** |
||||
* |
||||
* @author wangping created on 2020/11/2 17:47 |
||||
*/ |
||||
public class RedisKeyUtil { |
||||
|
||||
public static String CODE_PREFIX = "sop:app_auth_code"; |
||||
public static String AUTH_TOKEN_PREFIX = "sop:app_auth_token"; |
||||
public static String REFRESH_TOKEN_PREFIX = "sop:app_refresh_token"; |
||||
|
||||
|
||||
private static final Joiner JOINER = Joiner.on(":"); |
||||
|
||||
|
||||
public static String getCodeKey(String appId, Long userId) { |
||||
return JOINER.join(CODE_PREFIX, appId, userId); |
||||
} |
||||
|
||||
public static String getAuthTokenKey(String appId, Long userId) { |
||||
return JOINER.join(AUTH_TOKEN_PREFIX, appId, userId); |
||||
} |
||||
|
||||
public static String getRefreshTokenKey(String appId, Long userId) { |
||||
return JOINER.join(REFRESH_TOKEN_PREFIX, appId, userId); |
||||
} |
||||
} |
@ -0,0 +1,9 @@ |
||||
package com.gitee.sop.openapi.vo; |
||||
|
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class AuthCodeVo { |
||||
private String code; |
||||
private Integer state; |
||||
} |
@ -0,0 +1,16 @@ |
||||
spring: |
||||
redis: |
||||
cluster: |
||||
nodes: |
||||
- 10.1.11.46:7000 |
||||
- 10.1.11.46:7001 |
||||
- 10.1.11.46:7002 |
||||
jedis: |
||||
pool: |
||||
max-active: 10 |
||||
max-wait: -1 |
||||
max-idle: 3 |
||||
min-idle: 1 |
||||
timeout: 60000 |
||||
password: |
||||
|
@ -0,0 +1,16 @@ |
||||
spring: |
||||
redis: |
||||
sentinel: |
||||
master: mymaster |
||||
nodes: |
||||
- 192.168.0.114:26379 |
||||
- 192.168.0.114:26380 |
||||
- 192.168.0.114:26381 |
||||
jedis: |
||||
pool: |
||||
max-active: 10 |
||||
max-wait: -1 |
||||
max-idle: 9 |
||||
min-idle: 0 |
||||
timeout: 60000 |
||||
password: |
@ -0,0 +1,13 @@ |
||||
spring: |
||||
redis: |
||||
host: 10.0.0.39 |
||||
port: 6379 |
||||
jedis: |
||||
pool: |
||||
max-active: 10 |
||||
max-wait: -1 |
||||
max-idle: 9 |
||||
min-idle: 1 |
||||
database: 0 |
||||
timeout: 60000 |
||||
password: lt@3600 |
@ -0,0 +1,6 @@ |
||||
spring.profiles.active=dev |
||||
|
||||
server.port=8089 |
||||
spring.application.name=sop-openapi |
||||
# 注册中心 |
||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 |
@ -0,0 +1,4 @@ |
||||
isp.auth.user_id_empty_error=the user_id can NOT be null |
||||
|
||||
isp.auth.app_id_empty_error=the app_id can NOT be null |
||||
isp.auth.app_id_length_error=The app_id length must >= {0} and <= {1} |
@ -0,0 +1,9 @@ |
||||
# 用户登录凭证不能为空 |
||||
isp.auth.user_id_empty_error=\u7528\u6237\u0049\u0044\u4e0d\u80fd\u4e3a\u7a7a |
||||
# 用户登录凭证长度必须在{0}和{1}之间 |
||||
#isp.auth.user_token_length_error=\u7528\u6237\u767b\u5f55\u51ed\u8bc1\u957f\u5ea6\u5fc5\u987b\u5728{0}\u548c{1}\u4e4b\u95f4 |
||||
|
||||
# 应用ID不能为空 |
||||
isp.auth.app_id_empty_error=\u5e94\u7528\u0049\u0044\u4e0d\u80fd\u4e3a\u7a7a |
||||
# 应用ID长度必须在{0}和{1}之间 |
||||
isp.auth.app_id_length_error=\u5e94\u7528\u0049\u0044\u957f\u5ea6\u5fc5\u987b\u5728{0}\u548c{1}\u4e4b\u95f4 |
@ -0,0 +1,5 @@ |
||||
# 错误配置 |
||||
|
||||
# 系统配置 |
||||
isp.error_isv.common-error=The system is busy. |
||||
isp.error_isv.invalid-parameter=Invalid parameter, {0} |
@ -0,0 +1,9 @@ |
||||
# 错误配置 |
||||
|
||||
# 系统繁忙 |
||||
isp.error_isv.common-error=\u7cfb\u7edf\u7e41\u5fd9 |
||||
# 参数无效 |
||||
isp.error_isv.invalid-parameter=\u53c2\u6570\u65e0\u6548, {0} |
||||
|
||||
# ==== 参数配置 ==== |
||||
|
@ -0,0 +1,20 @@ |
||||
package com.gitee.sop; |
||||
|
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
/** |
||||
* Unit test for simple App. |
||||
*/ |
||||
public class AppTest |
||||
{ |
||||
/** |
||||
* Rigorous Test :-) |
||||
*/ |
||||
@Test |
||||
public void shouldAnswerWithTrue() |
||||
{ |
||||
assertTrue( true ); |
||||
} |
||||
} |
Loading…
Reference in new issue