Rest API ,统一使用 CommonResult 做了一次替换~
This commit is contained in:
parent
4162eda377
commit
6cbce27412
@ -39,6 +39,11 @@
|
|||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-annotations</artifactId>
|
||||||
|
<version>2.9.7</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.iocoder.common.framework.vo;
|
package cn.iocoder.common.framework.vo;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
public class CommonResult<T> {
|
public class CommonResult<T> {
|
||||||
@ -72,10 +73,12 @@ public class CommonResult<T> {
|
|||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
public boolean isSuccess() {
|
public boolean isSuccess() {
|
||||||
return CODE_SUCCESS.equals(code);
|
return CODE_SUCCESS.equals(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
public boolean isError() {
|
public boolean isError() {
|
||||||
return !isSuccess();
|
return !isSuccess();
|
||||||
}
|
}
|
||||||
|
@ -36,10 +36,13 @@ public class PassportController {
|
|||||||
/**
|
/**
|
||||||
* 手机号 + 验证码登陆
|
* 手机号 + 验证码登陆
|
||||||
*
|
*
|
||||||
|
* @see #mobileRegister2(String, String) 使用替代
|
||||||
|
*
|
||||||
* @param mobile 手机号
|
* @param mobile 手机号
|
||||||
* @param code 验证码
|
* @param code 验证码
|
||||||
* @return 授权信息
|
* @return 授权信息
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
@PermitAll
|
@PermitAll
|
||||||
@PostMapping("/mobile/login")
|
@PostMapping("/mobile/login")
|
||||||
public OAuth2AccessTokenBO mobileRegister(@RequestParam("mobile") String mobile,
|
public OAuth2AccessTokenBO mobileRegister(@RequestParam("mobile") String mobile,
|
||||||
@ -96,8 +99,8 @@ public class PassportController {
|
|||||||
*/
|
*/
|
||||||
@PermitAll
|
@PermitAll
|
||||||
@PostMapping("mobile/send")
|
@PostMapping("mobile/send")
|
||||||
public void mobileSend(@RequestParam("mobile") String mobile) {
|
public CommonResult<Void> mobileSend(@RequestParam("mobile") String mobile) {
|
||||||
mobileCodeService.send(mobile);
|
return mobileCodeService.send(mobile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO 功能:qq 登陆
|
// TODO 功能:qq 登陆
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package cn.iocoder.mall.user.controller;
|
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.sdk.context.SecurityContextHolder;
|
||||||
|
import cn.iocoder.mall.user.vo.UserVO;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@ -10,9 +12,10 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
@GetMapping("/info")
|
@GetMapping("/info")
|
||||||
public Long info() {
|
public CommonResult<UserVO> info() {
|
||||||
// TODO 芋艿,正在实现中
|
// TODO 芋艿,正在实现中
|
||||||
return SecurityContextHolder.getContext().getUid();
|
UserVO user = new UserVO().setId(SecurityContextHolder.getContext().getUid());
|
||||||
|
return CommonResult.success(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package cn.iocoder.mall.user.sdk.interceptor;
|
package cn.iocoder.mall.user.sdk.interceptor;
|
||||||
|
|
||||||
import cn.iocoder.common.framework.exception.ServiceException;
|
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.annotation.PermitAll;
|
||||||
import cn.iocoder.mall.user.sdk.context.SecurityContext;
|
import cn.iocoder.mall.user.sdk.context.SecurityContext;
|
||||||
import cn.iocoder.mall.user.sdk.context.SecurityContextHolder;
|
import cn.iocoder.mall.user.sdk.context.SecurityContextHolder;
|
||||||
@ -31,7 +32,11 @@ public class SecurityInterceptor extends HandlerInterceptorAdapter {
|
|||||||
String accessToken = obtainAccess(request);
|
String accessToken = obtainAccess(request);
|
||||||
OAuth2AuthenticationBO authentication = null;
|
OAuth2AuthenticationBO authentication = null;
|
||||||
if (accessToken != 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
|
||||||
SecurityContext context = new SecurityContext(authentication.getUid());
|
SecurityContext context = new SecurityContext(authentication.getUid());
|
||||||
SecurityContextHolder.setContext(context);
|
SecurityContextHolder.setContext(context);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.iocoder.mall.user.service.api;
|
package cn.iocoder.mall.user.service.api;
|
||||||
|
|
||||||
import cn.iocoder.common.framework.exception.ServiceException;
|
import cn.iocoder.common.framework.exception.ServiceException;
|
||||||
|
import cn.iocoder.common.framework.vo.CommonResult;
|
||||||
|
|
||||||
public interface MobileCodeService {
|
public interface MobileCodeService {
|
||||||
|
|
||||||
@ -9,6 +10,6 @@ public interface MobileCodeService {
|
|||||||
*
|
*
|
||||||
* @param mobile 手机号
|
* @param mobile 手机号
|
||||||
*/
|
*/
|
||||||
void send(String mobile) throws ServiceException;
|
CommonResult<Void> send(String mobile) throws ServiceException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ public interface OAuth2Service {
|
|||||||
* @param code 验证码
|
* @param code 验证码
|
||||||
* @return 授权信息
|
* @return 授权信息
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
OAuth2AccessTokenBO getAccessToken(String mobile, String code)
|
OAuth2AccessTokenBO getAccessToken(String mobile, String code)
|
||||||
throws ServiceException;
|
throws ServiceException;
|
||||||
|
|
||||||
@ -28,11 +29,10 @@ public interface OAuth2Service {
|
|||||||
* @param accessToken 访问令牌
|
* @param accessToken 访问令牌
|
||||||
* @return 授权信息
|
* @return 授权信息
|
||||||
*/
|
*/
|
||||||
OAuth2AuthenticationBO checkToken(String accessToken)
|
CommonResult<OAuth2AuthenticationBO> checkToken(String accessToken);
|
||||||
throws ServiceException;
|
|
||||||
|
|
||||||
// @see 刷新 token
|
// TODO @see 刷新 token
|
||||||
|
|
||||||
// @see 移除 token
|
// TODO @see 移除 token
|
||||||
|
|
||||||
}
|
}
|
@ -99,20 +99,21 @@ public class MobileCodeServiceImpl implements MobileCodeService {
|
|||||||
mobileCodeMapper.update(update);
|
mobileCodeMapper.update(update);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(String mobile) {
|
// TODO 芋艿,后面要返回有效时间
|
||||||
|
public CommonResult<Void> send(String mobile) {
|
||||||
// TODO 芋艿,校验手机格式
|
// TODO 芋艿,校验手机格式
|
||||||
// 校验手机号码是否已经注册
|
// 校验手机号码是否已经注册
|
||||||
if (userService.getUser(mobile) != null) {
|
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);
|
MobileCodeDO lastMobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile);
|
||||||
if (lastMobileCodePO != null) {
|
if (lastMobileCodePO != null) {
|
||||||
if (lastMobileCodePO.getTodayIndex() >= sendMaximumQuantityPerDay) { // 超过当天发送的上限。
|
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) { // 发送过于频繁
|
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 每天可发送数量
|
||||||
// TODO 提升,每个 IP 每小时可发送数量
|
// TODO 提升,每个 IP 每小时可发送数量
|
||||||
@ -124,6 +125,7 @@ public class MobileCodeServiceImpl implements MobileCodeService {
|
|||||||
.setUsed(false).setCreateTime(new Date());
|
.setUsed(false).setCreateTime(new Date());
|
||||||
mobileCodeMapper.insert(newMobileCodePO);
|
mobileCodeMapper.insert(newMobileCodePO);
|
||||||
// TODO 发送验证码短信
|
// TODO 发送验证码短信
|
||||||
|
return CommonResult.success(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -89,25 +89,25 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
// 创建访问令牌
|
// 创建访问令牌
|
||||||
OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(userDO.getId(), oauth2RefreshTokenDO.getId());
|
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));
|
return CommonResult.success(OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OAuth2AuthenticationBO checkToken(String accessToken) throws ServiceException {
|
public CommonResult<OAuth2AuthenticationBO> checkToken(String accessToken) throws ServiceException {
|
||||||
OAuth2AccessTokenDO accessTokenDO = oauth2AccessTokenMapper.selectByTokenId(accessToken);
|
OAuth2AccessTokenDO accessTokenDO = oauth2AccessTokenMapper.selectByTokenId(accessToken);
|
||||||
if (accessTokenDO == null) { // 不存在
|
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()) { // 已过期
|
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()) { // 无效
|
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) {
|
private OAuth2AccessTokenDO createOAuth2AccessToken(Long uid, String refreshToken) {
|
||||||
|
@ -14,9 +14,9 @@
|
|||||||
|
|
||||||
<select id="selectByTokenId" parameterType="String" resultType="OAuth2AccessTokenDO">
|
<select id="selectByTokenId" parameterType="String" resultType="OAuth2AccessTokenDO">
|
||||||
SELECT
|
SELECT
|
||||||
id, valid, expires_time
|
id, uid, valid, expires_time
|
||||||
FROM oauth2_access_token
|
FROM oauth2_access_token
|
||||||
WHERE token_id = #{id}
|
WHERE id = #{id}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
Loading…
Reference in New Issue
Block a user