From 6cbce274124bb03f9ebbb8b92906ca55915f6572 Mon Sep 17 00:00:00 2001
From: YunaiV <>
Date: Tue, 26 Feb 2019 00:58:58 +0800
Subject: [PATCH] =?UTF-8?q?Rest=20API=20=EF=BC=8C=E7=BB=9F=E4=B8=80?=
=?UTF-8?q?=E4=BD=BF=E7=94=A8=20CommonResult=20=E5=81=9A=E4=BA=86=E4=B8=80?=
=?UTF-8?q?=E6=AC=A1=E6=9B=BF=E6=8D=A2~?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
common/common-framework/pom.xml | 5 +++++
.../common/framework/vo/CommonResult.java | 3 +++
.../user/controller/PassportController.java | 7 +++++--
.../mall/user/controller/UserController.java | 7 +++++--
.../java/cn/iocoder/mall/user/vo/UserVO.java | 19 +++++++++++++++++++
.../sdk/interceptor/SecurityInterceptor.java | 7 ++++++-
.../user/service/api/MobileCodeService.java | 3 ++-
.../mall/user/service/api/OAuth2Service.java | 8 ++++----
.../user/service/MobileCodeServiceImpl.java | 10 ++++++----
.../mall/user/service/OAuth2ServiceImpl.java | 12 ++++++------
.../mapper/OAuth2AccessTokenMapper.xml | 4 ++--
11 files changed, 63 insertions(+), 22 deletions(-)
create mode 100644 user/user-application/src/main/java/cn/iocoder/mall/user/vo/UserVO.java
diff --git a/common/common-framework/pom.xml b/common/common-framework/pom.xml
index 2f2a61192..2a6830d67 100644
--- a/common/common-framework/pom.xml
+++ b/common/common-framework/pom.xml
@@ -39,6 +39,11 @@
org.slf4j
slf4j-api
+
+ com.fasterxml.jackson.core
+ jackson-annotations
+ 2.9.7
+
diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java
index 6e13134ce..a71bf9cf4 100644
--- a/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java
+++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java
@@ -1,5 +1,6 @@
package cn.iocoder.common.framework.vo;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.util.Assert;
public class CommonResult {
@@ -72,10 +73,12 @@ public class CommonResult {
this.data = data;
}
+ @JsonIgnore
public boolean isSuccess() {
return CODE_SUCCESS.equals(code);
}
+ @JsonIgnore
public boolean isError() {
return !isSuccess();
}
diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java
index 856dfe4ca..d8406f93f 100644
--- a/user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java
+++ b/user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java
@@ -36,10 +36,13 @@ public class PassportController {
/**
* 手机号 + 验证码登陆
*
+ * @see #mobileRegister2(String, String) 使用替代
+ *
* @param mobile 手机号
* @param code 验证码
* @return 授权信息
*/
+ @Deprecated
@PermitAll
@PostMapping("/mobile/login")
public OAuth2AccessTokenBO mobileRegister(@RequestParam("mobile") String mobile,
@@ -96,8 +99,8 @@ public class PassportController {
*/
@PermitAll
@PostMapping("mobile/send")
- public void mobileSend(@RequestParam("mobile") String mobile) {
- mobileCodeService.send(mobile);
+ public CommonResult mobileSend(@RequestParam("mobile") String mobile) {
+ return mobileCodeService.send(mobile);
}
// TODO 功能:qq 登陆
diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/controller/UserController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/controller/UserController.java
index 4caf39635..f7d78d6d6 100644
--- a/user/user-application/src/main/java/cn/iocoder/mall/user/controller/UserController.java
+++ b/user/user-application/src/main/java/cn/iocoder/mall/user/controller/UserController.java
@@ -1,6 +1,8 @@
package cn.iocoder.mall.user.controller;
+import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.user.sdk.context.SecurityContextHolder;
+import cn.iocoder.mall.user.vo.UserVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -10,9 +12,10 @@ import org.springframework.web.bind.annotation.RestController;
public class UserController {
@GetMapping("/info")
- public Long info() {
+ public CommonResult info() {
// TODO 芋艿,正在实现中
- return SecurityContextHolder.getContext().getUid();
+ UserVO user = new UserVO().setId(SecurityContextHolder.getContext().getUid());
+ return CommonResult.success(user);
}
}
\ No newline at end of file
diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/vo/UserVO.java b/user/user-application/src/main/java/cn/iocoder/mall/user/vo/UserVO.java
new file mode 100644
index 000000000..4766053f4
--- /dev/null
+++ b/user/user-application/src/main/java/cn/iocoder/mall/user/vo/UserVO.java
@@ -0,0 +1,19 @@
+package cn.iocoder.mall.user.vo;
+
+public class UserVO {
+
+ /**
+ * 用户编号
+ */
+ private Long id;
+
+ public Long getId() {
+ return id;
+ }
+
+ public UserVO setId(Long id) {
+ this.id = id;
+ return this;
+ }
+
+}
\ No newline at end of file
diff --git a/user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/SecurityInterceptor.java b/user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/SecurityInterceptor.java
index f2a6db8ab..511f18c5f 100644
--- a/user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/SecurityInterceptor.java
+++ b/user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/SecurityInterceptor.java
@@ -1,6 +1,7 @@
package cn.iocoder.mall.user.sdk.interceptor;
import cn.iocoder.common.framework.exception.ServiceException;
+import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.user.sdk.annotation.PermitAll;
import cn.iocoder.mall.user.sdk.context.SecurityContext;
import cn.iocoder.mall.user.sdk.context.SecurityContextHolder;
@@ -31,7 +32,11 @@ public class SecurityInterceptor extends HandlerInterceptorAdapter {
String accessToken = obtainAccess(request);
OAuth2AuthenticationBO authentication = null;
if (accessToken != null) {
- authentication = oauth2Service.checkToken(accessToken);
+ CommonResult result = oauth2Service.checkToken(accessToken);
+ if (result.isError()) { // TODO 芋艿,如果访问的地址无需登录,这里也不用抛异常
+ throw new ServiceException(result.getCode(), result.getMessage());
+ }
+ authentication = result.getData();
// 添加到 SecurityContext
SecurityContext context = new SecurityContext(authentication.getUid());
SecurityContextHolder.setContext(context);
diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/MobileCodeService.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/MobileCodeService.java
index d7cc99969..821b66a10 100644
--- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/MobileCodeService.java
+++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/MobileCodeService.java
@@ -1,6 +1,7 @@
package cn.iocoder.mall.user.service.api;
import cn.iocoder.common.framework.exception.ServiceException;
+import cn.iocoder.common.framework.vo.CommonResult;
public interface MobileCodeService {
@@ -9,6 +10,6 @@ public interface MobileCodeService {
*
* @param mobile 手机号
*/
- void send(String mobile) throws ServiceException;
+ CommonResult send(String mobile) throws ServiceException;
}
diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/OAuth2Service.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/OAuth2Service.java
index 7cd568291..1ac861335 100644
--- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/OAuth2Service.java
+++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/OAuth2Service.java
@@ -17,6 +17,7 @@ public interface OAuth2Service {
* @param code 验证码
* @return 授权信息
*/
+ @Deprecated
OAuth2AccessTokenBO getAccessToken(String mobile, String code)
throws ServiceException;
@@ -28,11 +29,10 @@ public interface OAuth2Service {
* @param accessToken 访问令牌
* @return 授权信息
*/
- OAuth2AuthenticationBO checkToken(String accessToken)
- throws ServiceException;
+ CommonResult checkToken(String accessToken);
- // @see 刷新 token
+ // TODO @see 刷新 token
- // @see 移除 token
+ // TODO @see 移除 token
}
\ No newline at end of file
diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/MobileCodeServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/MobileCodeServiceImpl.java
index e497a134b..0615f4093 100644
--- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/MobileCodeServiceImpl.java
+++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/MobileCodeServiceImpl.java
@@ -99,20 +99,21 @@ public class MobileCodeServiceImpl implements MobileCodeService {
mobileCodeMapper.update(update);
}
- public void send(String mobile) {
+ // TODO 芋艿,后面要返回有效时间
+ public CommonResult send(String mobile) {
// TODO 芋艿,校验手机格式
// 校验手机号码是否已经注册
if (userService.getUser(mobile) != null) {
- throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode());
+ return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode());
}
// 校验是否可以发送验证码
MobileCodeDO lastMobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile);
if (lastMobileCodePO != null) {
if (lastMobileCodePO.getTodayIndex() >= sendMaximumQuantityPerDay) { // 超过当天发送的上限。
- throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY.getCode());
+ return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY.getCode());
}
if (System.currentTimeMillis() - lastMobileCodePO.getCreateTime().getTime() < sendFrequency) { // 发送过于频繁
- throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_SEND_TOO_FAST.getCode());
+ return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_SEND_TOO_FAST.getCode());
}
// TODO 提升,每个 IP 每天可发送数量
// TODO 提升,每个 IP 每小时可发送数量
@@ -124,6 +125,7 @@ public class MobileCodeServiceImpl implements MobileCodeService {
.setUsed(false).setCreateTime(new Date());
mobileCodeMapper.insert(newMobileCodePO);
// TODO 发送验证码短信
+ return CommonResult.success(null);
}
}
\ No newline at end of file
diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java
index 7eabd7ca2..c06960b2b 100644
--- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java
+++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java
@@ -89,25 +89,25 @@ public class OAuth2ServiceImpl implements OAuth2Service {
// 创建访问令牌
OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(userDO.getId(), oauth2RefreshTokenDO.getId());
// 标记已使用
-// mobileCodeService.useMobileCode(result.getData().getId(), userDO.getId());
+ mobileCodeService.useMobileCode(result.getData().getId(), userDO.getId());
// 转换返回
return CommonResult.success(OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO));
}
@Override
- public OAuth2AuthenticationBO checkToken(String accessToken) throws ServiceException {
+ public CommonResult checkToken(String accessToken) throws ServiceException {
OAuth2AccessTokenDO accessTokenDO = oauth2AccessTokenMapper.selectByTokenId(accessToken);
if (accessTokenDO == null) { // 不存在
- throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_TOKEN_NOT_FOUND.getCode());
+ return ServiceExceptionUtil.error(UserErrorCodeEnum.OAUTH_INVALID_TOKEN_NOT_FOUND.getCode());
}
if (accessTokenDO.getExpiresTime().getTime() < System.currentTimeMillis()) { // 已过期
- throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_TOKEN_EXPIRED.getCode());
+ return ServiceExceptionUtil.error(UserErrorCodeEnum.OAUTH_INVALID_TOKEN_EXPIRED.getCode());
}
if (!accessTokenDO.getValid()) { // 无效
- throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_TOKEN_INVALID.getCode());
+ return ServiceExceptionUtil.error(UserErrorCodeEnum.OAUTH_INVALID_TOKEN_INVALID.getCode());
}
// 转换返回
- return OAuth2Convert.INSTANCE.convertToAuthentication(accessTokenDO);
+ return CommonResult.success(OAuth2Convert.INSTANCE.convertToAuthentication(accessTokenDO));
}
private OAuth2AccessTokenDO createOAuth2AccessToken(Long uid, String refreshToken) {
diff --git a/user/user-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml b/user/user-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml
index d942e9ed3..763b77790 100644
--- a/user/user-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml
+++ b/user/user-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml
@@ -14,9 +14,9 @@
\ No newline at end of file