Rest API ,统一使用 CommonResult 做了一次替换~

This commit is contained in:
YunaiV 2019-02-26 00:58:58 +08:00
parent 4162eda377
commit 6cbce27412
11 changed files with 63 additions and 22 deletions

View File

@ -39,6 +39,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
</dependencies>

View File

@ -1,5 +1,6 @@
package cn.iocoder.common.framework.vo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.util.Assert;
public class CommonResult<T> {
@ -72,10 +73,12 @@ public class CommonResult<T> {
this.data = data;
}
@JsonIgnore
public boolean isSuccess() {
return CODE_SUCCESS.equals(code);
}
@JsonIgnore
public boolean isError() {
return !isSuccess();
}

View File

@ -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<Void> mobileSend(@RequestParam("mobile") String mobile) {
return mobileCodeService.send(mobile);
}
// TODO 功能qq 登陆

View File

@ -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<UserVO> info() {
// TODO 芋艿正在实现中
return SecurityContextHolder.getContext().getUid();
UserVO user = new UserVO().setId(SecurityContextHolder.getContext().getUid());
return CommonResult.success(user);
}
}

View File

@ -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;
}
}

View File

@ -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<OAuth2AuthenticationBO> 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);

View File

@ -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<Void> send(String mobile) throws ServiceException;
}

View File

@ -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<OAuth2AuthenticationBO> checkToken(String accessToken);
// @see 刷新 token
// TODO @see 刷新 token
// @see 移除 token
// TODO @see 移除 token
}

View File

@ -99,20 +99,21 @@ public class MobileCodeServiceImpl implements MobileCodeService {
mobileCodeMapper.update(update);
}
public void send(String mobile) {
// TODO 芋艿后面要返回有效时间
public CommonResult<Void> 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);
}
}

View File

@ -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<OAuth2AuthenticationBO> 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) {

View File

@ -14,9 +14,9 @@
<select id="selectByTokenId" parameterType="String" resultType="OAuth2AccessTokenDO">
SELECT
id, valid, expires_time
id, uid, valid, expires_time
FROM oauth2_access_token
WHERE token_id = #{id}
WHERE id = #{id}
</select>
</mapper>