Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yudao-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hblj
yudao-cloud
Commits
4162eda3
提交
4162eda3
authored
2月 25, 2019
作者:
YunaiV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新增 CommonResult 。
将使用手机号进行注册登陆的逻辑,进行变更~
上级
4ae211db
隐藏空白字符变更
内嵌
并排
正在显示
27 个修改的文件
包含
361 行增加
和
86 行删除
+361
-86
.gitignore
.gitignore
+1
-0
GlobalExceptionHandler.java
...coder/common/framework/config/GlobalExceptionHandler.java
+4
-10
GlobalResponseBodyAdvice.java
...der/common/framework/config/GlobalResponseBodyAdvice.java
+4
-0
ExceptionUtil.java
.../java/cn/iocoder/common/framework/util/ExceptionUtil.java
+35
-0
ServiceExceptionUtil.java
...n/iocoder/common/framework/util/ServiceExceptionUtil.java
+11
-0
CommonResult.java
...ain/java/cn/iocoder/common/framework/vo/CommonResult.java
+84
-0
RestResult.java
.../main/java/cn/iocoder/common/framework/vo/RestResult.java
+2
-0
pom.xml
user/user-application/pom.xml
+6
-0
PassportController.java
...a/cn/iocoder/mall/user/controller/PassportController.java
+29
-4
UserController.java
.../java/cn/iocoder/mall/user/controller/UserController.java
+7
-4
SecurityInterceptor.java
...ocoder/mall/user/sdk/interceptor/SecurityInterceptor.java
+2
-2
OAuth2Service.java
.../java/cn/iocoder/mall/user/service/api/OAuth2Service.java
+6
-3
UserService.java
...in/java/cn/iocoder/mall/user/service/api/UserService.java
+5
-2
OAuth2AccessTokenBO.java
...iocoder/mall/user/service/api/bo/OAuth2AccessTokenBO.java
+1
-1
OAuth2AuthenticationBO.java
...oder/mall/user/service/api/bo/OAuth2AuthenticationBO.java
+3
-3
UserBO.java
...main/java/cn/iocoder/mall/user/service/api/bo/UserBO.java
+4
-4
MobileCodeDTO.java
...a/cn/iocoder/mall/user/service/api/dto/MobileCodeDTO.java
+0
-16
pom.xml
user/user-service-impl/pom.xml
+27
-2
OAuth2Convert.java
...main/java/cn/iocoder/mall/user/convert/OAuth2Convert.java
+16
-7
UserConvert.java
...c/main/java/cn/iocoder/mall/user/convert/UserConvert.java
+18
-0
OAuth2AccessTokenDO.java
.../cn/iocoder/mall/user/dataobject/OAuth2AccessTokenDO.java
+5
-5
OAuth2RefreshTokenDO.java
...cn/iocoder/mall/user/dataobject/OAuth2RefreshTokenDO.java
+5
-5
MobileCodeServiceImpl.java
...a/cn/iocoder/mall/user/service/MobileCodeServiceImpl.java
+25
-0
OAuth2ServiceImpl.java
.../java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java
+36
-9
UserServiceImpl.java
...in/java/cn/iocoder/mall/user/service/UserServiceImpl.java
+19
-4
OAuth2AccessTokenMapper.xml
...mpl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml
+4
-3
OAuth2RefreshTokenMapper.xml
...pl/src/main/resources/mapper/OAuth2RefreshTokenMapper.xml
+2
-2
没有找到文件。
.gitignore
浏览文件 @
4162eda3
...
...
@@ -15,6 +15,7 @@
*.iws
*.iml
*.ipr
target/*
### NetBeans ###
/nbproject/private/
...
...
common/common-framework/src/main/java/cn/iocoder/common/framework/config/GlobalExceptionHandler.java
浏览文件 @
4162eda3
...
...
@@ -2,6 +2,7 @@ package cn.iocoder.common.framework.config;
import
cn.iocoder.common.framework.constant.SysErrorCodeEnum
;
import
cn.iocoder.common.framework.exception.ServiceException
;
import
cn.iocoder.common.framework.util.ExceptionUtil
;
import
cn.iocoder.common.framework.vo.RestResult
;
import
org.springframework.web.bind.MissingServletRequestParameterException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
...
...
@@ -9,7 +10,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
javax.servlet.http.HttpServletRequest
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.UndeclaredThrowableException
;
@ControllerAdvice
...
...
@@ -21,19 +21,13 @@ public class GlobalExceptionHandler {
return
RestResult
.
error
(
ex
.
getCode
(),
ex
.
getMessage
());
}
// 处理 Spring 动态代理调用时,发生 UndeclaredThrowableException 的情况。
// 不了解的胖友,可以看看 https://segmentfault.com/a/1190000012262244 文章
@ResponseBody
@ExceptionHandler
(
value
=
UndeclaredThrowableException
.
class
)
public
RestResult
undeclaredThrowableExceptionHandler
(
HttpServletRequest
req
,
UndeclaredThrowableException
e
)
{
// 尝试获得 ServiceException 异常。如果是,则使用 serviceExceptionHandler 方法处理。
Throwable
undeclaredThrowable
=
e
.
getUndeclaredThrowable
();
if
(
undeclaredThrowable
instanceof
InvocationTargetException
)
{
InvocationTargetException
invocationTargetException
=
(
InvocationTargetException
)
undeclaredThrowable
;
Throwable
targetException
=
invocationTargetException
.
getTargetException
();
if
(
targetException
!=
null
&
targetException
instanceof
ServiceException
)
{
return
serviceExceptionHandler
(
req
,
(
ServiceException
)
targetException
);
}
ServiceException
serviceException
=
ExceptionUtil
.
getServiceException
(
e
);
if
(
serviceException
!=
null
)
{
return
serviceExceptionHandler
(
req
,
serviceException
);
}
// 获得不到,使用 异常日志 方法处理。
return
resultExceptionHandler
(
req
,
e
);
...
...
common/common-framework/src/main/java/cn/iocoder/common/framework/config/GlobalResponseBodyAdvice.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
common
.
framework
.
config
;
import
cn.iocoder.common.framework.vo.CommonResult
;
import
cn.iocoder.common.framework.vo.RestResult
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.http.MediaType
;
...
...
@@ -21,6 +22,9 @@ public class GlobalResponseBodyAdvice implements ResponseBodyAdvice {
if
(
body
instanceof
RestResult
)
{
return
body
;
}
if
(
body
instanceof
CommonResult
)
{
// TODO 芋艿,后续要改下
return
body
;
}
return
RestResult
.
ok
(
body
);
}
...
...
common/common-framework/src/main/java/cn/iocoder/common/framework/util/ExceptionUtil.java
0 → 100644
浏览文件 @
4162eda3
package
cn
.
iocoder
.
common
.
framework
.
util
;
import
cn.iocoder.common.framework.exception.ServiceException
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.UndeclaredThrowableException
;
public
class
ExceptionUtil
{
public
static
ServiceException
getServiceException
(
Exception
e
)
{
if
(
e
instanceof
UndeclaredThrowableException
)
{
return
getServiceException
((
UndeclaredThrowableException
)
e
);
}
return
null
;
}
// 处理 Spring 动态代理调用时,发生 UndeclaredThrowableException 的情况。
// 不了解的胖友,可以先看看 https://segmentfault.com/a/1190000012262244 文章
// 原因是:
// 1. Dubbo 动态代理 Wrapper 会将抛出的异常,包装成 InvocationTargetException 异常
// 2. Spring AOP 发现是 InvocationTargetException 异常是非方法定义的异常,则会包装成 UndeclaredThrowableException 异常。
public
static
ServiceException
getServiceException
(
UndeclaredThrowableException
e
)
{
Throwable
undeclaredThrowable
=
e
.
getUndeclaredThrowable
();
if
(
undeclaredThrowable
instanceof
InvocationTargetException
)
{
InvocationTargetException
invocationTargetException
=
(
InvocationTargetException
)
undeclaredThrowable
;
Throwable
targetException
=
invocationTargetException
.
getTargetException
();
if
(
targetException
!=
null
&
targetException
instanceof
ServiceException
)
{
return
(
ServiceException
)
targetException
;
}
}
return
null
;
}
}
\ No newline at end of file
common/common-framework/src/main/java/cn/iocoder/common/framework/util/ServiceExceptionUtil.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
common
.
framework
.
util
;
import
cn.iocoder.common.framework.exception.ServiceException
;
import
cn.iocoder.common.framework.vo.CommonResult
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -38,6 +39,16 @@ public class ServiceExceptionUtil {
ServiceExceptionUtil
.
messages
.
put
(
code
,
message
);
}
// TODO 芋艿,可能不是目前最优解,目前暂时这样
public
static
<
T
>
CommonResult
<
T
>
error
(
Integer
code
)
{
return
CommonResult
.
error
(
code
,
messages
.
get
(
code
));
}
public
static
CommonResult
error
(
Integer
code
,
Object
...
params
)
{
String
message
=
doFormat
(
code
,
messages
.
get
(
code
),
params
);
return
CommonResult
.
error
(
code
,
message
);
}
/**
* 创建指定编号的 ServiceException 的异常
*
...
...
common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java
0 → 100644
浏览文件 @
4162eda3
package
cn
.
iocoder
.
common
.
framework
.
vo
;
import
org.springframework.util.Assert
;
public
class
CommonResult
<
T
>
{
public
static
Integer
CODE_SUCCESS
=
0
;
/**
* 错误码
*/
private
Integer
code
;
/**
* 错误提示
*/
private
String
message
;
/**
* 返回数据
*/
private
T
data
;
/**
* 将传入的 result 对象,转换成另外一个泛型结果的对象
*
* 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
*
* @param result 传入的 result 对象
* @param <T> 返回的泛型
* @return 新的 CommonResult 对象
*/
public
static
<
T
>
CommonResult
<
T
>
error
(
CommonResult
<?>
result
)
{
return
error
(
result
.
getCode
(),
result
.
getMessage
());
}
public
static
<
T
>
CommonResult
<
T
>
error
(
Integer
code
,
String
message
)
{
Assert
.
isTrue
(!
CODE_SUCCESS
.
equals
(
code
),
"code 必须是错误的!"
);
CommonResult
<
T
>
result
=
new
CommonResult
<>();
result
.
code
=
code
;
result
.
message
=
message
;
return
result
;
}
public
static
<
T
>
CommonResult
<
T
>
success
(
T
data
)
{
CommonResult
<
T
>
result
=
new
CommonResult
<>();
result
.
code
=
CODE_SUCCESS
;
result
.
data
=
data
;
result
.
message
=
""
;
return
result
;
}
public
Integer
getCode
()
{
return
code
;
}
public
void
setCode
(
Integer
code
)
{
this
.
code
=
code
;
}
public
String
getMessage
()
{
return
message
;
}
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
}
public
T
getData
()
{
return
data
;
}
public
void
setData
(
T
data
)
{
this
.
data
=
data
;
}
public
boolean
isSuccess
()
{
return
CODE_SUCCESS
.
equals
(
code
);
}
public
boolean
isError
()
{
return
!
isSuccess
();
}
}
\ No newline at end of file
common/common-framework/src/main/java/cn/iocoder/common/framework/vo/RestResult.java
浏览文件 @
4162eda3
...
...
@@ -53,4 +53,5 @@ public class RestResult {
public
void
setData
(
Object
data
)
{
this
.
data
=
data
;
}
}
\ No newline at end of file
user/user-application/pom.xml
浏览文件 @
4162eda3
...
...
@@ -92,6 +92,12 @@
<scope>
compile
</scope>
</dependency>
<dependency>
<groupId>
org.mapstruct
</groupId>
<artifactId>
mapstruct
</artifactId>
<!-- use mapstruct-jdk8 for Java 8 or higher -->
<version>
${org.mapstruct.version}
</version>
</dependency>
</dependencies>
<build>
...
...
user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
controller
;
import
cn.iocoder.common.framework.exception.ServiceException
;
import
cn.iocoder.common.framework.util.ExceptionUtil
;
import
cn.iocoder.common.framework.vo.CommonResult
;
import
cn.iocoder.mall.user.sdk.annotation.PermitAll
;
import
cn.iocoder.mall.user.service.api.MobileCodeService
;
import
cn.iocoder.mall.user.service.api.OAuth2Service
;
import
cn.iocoder.mall.user.service.api.UserService
;
import
cn.iocoder.mall.user.service.api.bo.OAuth2AccessTokenBO
;
import
cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum
;
import
cn.iocoder.mall.user.service.api.dto.OAuth2AccessTokenBO
;
import
com.alibaba.dubbo.config.annotation.Reference
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
...
...
@@ -35,7 +37,7 @@ public class PassportController {
* 手机号 + 验证码登陆
*
* @param mobile 手机号
* @param code 验证码
* @param code
验证码
* @return 授权信息
*/
@PermitAll
...
...
@@ -47,7 +49,11 @@ public class PassportController {
try
{
accessTokenDTO
=
oauth2Service
.
getAccessToken
(
mobile
,
code
);
return
accessTokenDTO
;
}
catch
(
ServiceException
serviceException
)
{
}
catch
(
Exception
ex
)
{
ServiceException
serviceException
=
ExceptionUtil
.
getServiceException
(
ex
);
if
(
serviceException
==
null
)
{
throw
ex
;
}
if
(!
serviceException
.
getCode
().
equals
(
UserErrorCodeEnum
.
USER_MOBILE_NOT_REGISTERED
.
getCode
()))
{
// 如果是未注册异常,忽略。下面发起自动注册逻辑。
throw
serviceException
;
}
...
...
@@ -55,7 +61,11 @@ public class PassportController {
// 上面尝试授权失败,说明用户未注册,发起自动注册。
try
{
userService
.
createUser
(
mobile
,
code
);
}
catch
(
ServiceException
serviceException
)
{
}
catch
(
Exception
ex
)
{
ServiceException
serviceException
=
ExceptionUtil
.
getServiceException
(
ex
);
if
(
serviceException
==
null
)
{
throw
ex
;
}
if
(!
serviceException
.
getCode
().
equals
(
UserErrorCodeEnum
.
USER_MOBILE_ALREADY_REGISTERED
.
getCode
()))
{
// 如果是已注册异常,忽略。下面再次发起授权
throw
serviceException
;
}
...
...
@@ -65,11 +75,26 @@ public class PassportController {
return
accessTokenDTO
;
}
/**
* 手机号 + 验证码登陆
*
* @param mobile 手机号
* @param code 验证码
* @return 授权信息
*/
@PermitAll
@PostMapping
(
"/mobile/login2"
)
public
CommonResult
<
OAuth2AccessTokenBO
>
mobileRegister2
(
@RequestParam
(
"mobile"
)
String
mobile
,
@RequestParam
(
"code"
)
String
code
)
{
return
oauth2Service
.
getAccessToken2
(
mobile
,
code
);
}
/**
* 发送手机验证码
*
* @param mobile 手机号
*/
@PermitAll
@PostMapping
(
"mobile/send"
)
public
void
mobileSend
(
@RequestParam
(
"mobile"
)
String
mobile
)
{
mobileCodeService
.
send
(
mobile
);
...
...
user/user-application/src/main/java/cn/iocoder/mall/user/controller/UserController.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
controller
;
import
cn.iocoder.mall.user.sdk.context.SecurityContextHolder
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
//
@RestController
//
@RequestMapping("/user")
@RestController
@RequestMapping
(
"/user"
)
public
class
UserController
{
@GetMapping
(
"/info"
)
public
Long
info
()
{
// TODO 芋艿,正在实现中
// return SecurityContextHolder.getContext().getUid();
return
null
;
return
SecurityContextHolder
.
getContext
().
getUid
();
}
}
\ No newline at end of file
user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/SecurityInterceptor.java
浏览文件 @
4162eda3
...
...
@@ -5,7 +5,7 @@ import cn.iocoder.mall.user.sdk.annotation.PermitAll;
import
cn.iocoder.mall.user.sdk.context.SecurityContext
;
import
cn.iocoder.mall.user.sdk.context.SecurityContextHolder
;
import
cn.iocoder.mall.user.service.api.OAuth2Service
;
import
cn.iocoder.mall.user.service.api.
dto.OAuth2AuthenticationDT
O
;
import
cn.iocoder.mall.user.service.api.
bo.OAuth2AuthenticationB
O
;
import
com.alibaba.dubbo.config.annotation.Reference
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
...
...
@@ -29,7 +29,7 @@ public class SecurityInterceptor extends HandlerInterceptorAdapter {
public
boolean
preHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
)
throws
Exception
{
// 校验访问令牌是否正确。若正确,返回授权信息
String
accessToken
=
obtainAccess
(
request
);
OAuth2Authentication
DT
O
authentication
=
null
;
OAuth2Authentication
B
O
authentication
=
null
;
if
(
accessToken
!=
null
)
{
authentication
=
oauth2Service
.
checkToken
(
accessToken
);
// 添加到 SecurityContext
...
...
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/OAuth2Service.java
浏览文件 @
4162eda3
...
...
@@ -2,8 +2,9 @@ package cn.iocoder.mall.user.service.api;
import
cn.iocoder.common.framework.exception.ServiceException
;
import
cn.iocoder.mall.user.service.api.dto.OAuth2AccessTokenBO
;
import
cn.iocoder.mall.user.service.api.dto.OAuth2AuthenticationDTO
;
import
cn.iocoder.common.framework.vo.CommonResult
;
import
cn.iocoder.mall.user.service.api.bo.OAuth2AccessTokenBO
;
import
cn.iocoder.mall.user.service.api.bo.OAuth2AuthenticationBO
;
public
interface
OAuth2Service
{
...
...
@@ -19,13 +20,15 @@ public interface OAuth2Service {
OAuth2AccessTokenBO
getAccessToken
(
String
mobile
,
String
code
)
throws
ServiceException
;
CommonResult
<
OAuth2AccessTokenBO
>
getAccessToken2
(
String
mobile
,
String
code
);
/**
* 校验访问令牌,获取身份信息( 不包括 accessToken 等等 )
*
* @param accessToken 访问令牌
* @return 授权信息
*/
OAuth2Authentication
DT
O
checkToken
(
String
accessToken
)
OAuth2Authentication
B
O
checkToken
(
String
accessToken
)
throws
ServiceException
;
// @see 刷新 token
...
...
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/UserService.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
service
.
api
;
import
cn.iocoder.common.framework.exception.ServiceException
;
import
cn.iocoder.mall.user.service.api.
dto.UserDT
O
;
import
cn.iocoder.mall.user.service.api.
bo.UserB
O
;
public
interface
UserService
{
/**
* 创建用户。一般在用户注册时,调用该方法
*
* TODO 芋艿,此处要传递一些用户注册时的相关信息,例如说 ip、ua、客户端来源等等。用于数据分析、风控等等。
*
* @param mobile 手机号
* @param code 手机验证码
* @return 用户
*/
User
DT
O
createUser
(
String
mobile
,
String
code
)
throws
ServiceException
;
User
B
O
createUser
(
String
mobile
,
String
code
)
throws
ServiceException
;
}
\ No newline at end of file
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/
dt
o/OAuth2AccessTokenBO.java
→
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/
b
o/OAuth2AccessTokenBO.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
service
.
api
.
dt
o
;
package
cn
.
iocoder
.
mall
.
user
.
service
.
api
.
b
o
;
import
java.io.Serializable
;
...
...
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/
dto/OAuth2AuthenticationDT
O.java
→
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/
bo/OAuth2AuthenticationB
O.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
service
.
api
.
dt
o
;
package
cn
.
iocoder
.
mall
.
user
.
service
.
api
.
b
o
;
import
java.io.Serializable
;
public
class
OAuth2Authentication
DT
O
implements
Serializable
{
public
class
OAuth2Authentication
B
O
implements
Serializable
{
/**
* 用户编号
...
...
@@ -13,7 +13,7 @@ public class OAuth2AuthenticationDTO implements Serializable {
return
uid
;
}
public
OAuth2Authentication
DT
O
setUid
(
Long
uid
)
{
public
OAuth2Authentication
B
O
setUid
(
Long
uid
)
{
this
.
uid
=
uid
;
return
this
;
}
...
...
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/
dto/UserDT
O.java
→
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/
bo/UserB
O.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
service
.
api
.
dt
o
;
package
cn
.
iocoder
.
mall
.
user
.
service
.
api
.
b
o
;
public
class
User
DT
O
{
public
class
User
B
O
{
/**
* 用户编号
...
...
@@ -15,7 +15,7 @@ public class UserDTO {
return
uid
;
}
public
User
DT
O
setUid
(
Long
uid
)
{
public
User
B
O
setUid
(
Long
uid
)
{
this
.
uid
=
uid
;
return
this
;
}
...
...
@@ -24,7 +24,7 @@ public class UserDTO {
return
mobile
;
}
public
User
DT
O
setMobile
(
String
mobile
)
{
public
User
B
O
setMobile
(
String
mobile
)
{
this
.
mobile
=
mobile
;
return
this
;
}
...
...
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/dto/MobileCodeDTO.java
deleted
100644 → 0
浏览文件 @
4ae211db
package
cn
.
iocoder
.
mall
.
user
.
service
.
api
.
dto
;
public
class
MobileCodeDTO
{
private
String
code
;
public
String
getCode
()
{
return
code
;
}
public
MobileCodeDTO
setCode
(
String
code
)
{
this
.
code
=
code
;
return
this
;
}
}
\ No newline at end of file
user/user-service-impl/pom.xml
浏览文件 @
4162eda3
...
...
@@ -9,6 +9,10 @@
</parent>
<modelVersion>
4.0.0
</modelVersion>
<properties>
<org.mapstruct.version>
1.3.0.Final
</org.mapstruct.version>
</properties>
<artifactId>
user-service-impl
</artifactId>
<dependencies>
<dependency>
...
...
@@ -41,11 +45,31 @@
<dependency>
<groupId>
org.mapstruct
</groupId>
<artifactId>
mapstruct
</artifactId>
<version>
1.3.0.Final
</version>
<scope>
compile
</scope>
<version>
${org.mapstruct.version}
</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 提供给 mapstruct 使用 -->
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.5.1
</version>
<configuration>
<source>
1.8
</source>
<!-- or higher, depending on your project -->
<target>
1.8
</target>
<!-- or higher, depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>
org.mapstruct
</groupId>
<artifactId>
mapstruct-processor
</artifactId>
<version>
${org.mapstruct.version}
</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
user/user-service-impl/src/main/java/cn/iocoder/mall/user/convert/OAuth2Convert.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
convert
;
import
cn.iocoder.mall.user.dataobject.OAuth2AccessTokenDO
;
import
cn.iocoder.mall.user.service.api.dto.OAuth2AccessTokenBO
;
import
cn.iocoder.mall.user.service.api.bo.OAuth2AccessTokenBO
;
import
cn.iocoder.mall.user.service.api.bo.OAuth2AuthenticationBO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.Mapping
;
import
org.mapstruct.Mappings
;
import
org.mapstruct.factory.Mappers
;
@Mapper
public
interface
OAuth2Convert
{
OAuth2Convert
INSTANCE
=
Mappers
.
getMapper
(
OAuth2Convert
.
class
);
@Mappings
({})
OAuth2AccessTokenBO
convert
(
OAuth2AccessTokenDO
oauth2AccessTokenDO
);
@Mappings
({
@Mapping
(
source
=
"id"
,
target
=
"accessToken"
)
})
OAuth2AccessTokenBO
convertToAccessToken
(
OAuth2AccessTokenDO
oauth2AccessTokenDO
);
default
OAuth2AccessTokenBO
convertWithExpiresIn
(
OAuth2AccessTokenDO
oauth2AccessTokenDO
)
{
OAuth2AccessTokenBO
bo
=
this
.
convert
(
oauth2AccessTokenDO
);
bo
.
setExpiresIn
(
Math
.
max
((
int
)
((
oauth2AccessTokenDO
.
getExpiresTime
().
getTime
()
-
System
.
currentTimeMillis
())
/
1000
),
0
));
return
bo
;
default
OAuth2AccessTokenBO
convertToAccessTokenWithExpiresIn
(
OAuth2AccessTokenDO
oauth2AccessTokenDO
)
{
return
this
.
convertToAccessToken
(
oauth2AccessTokenDO
)
.
setExpiresIn
(
Math
.
max
((
int
)
((
oauth2AccessTokenDO
.
getExpiresTime
().
getTime
()
-
System
.
currentTimeMillis
())
/
1000
),
0
));
}
@Mappings
({})
OAuth2AuthenticationBO
convertToAuthentication
(
OAuth2AccessTokenDO
oauth2AccessTokenDO
);
}
\ No newline at end of file
user/user-service-impl/src/main/java/cn/iocoder/mall/user/convert/UserConvert.java
0 → 100644
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
convert
;
import
cn.iocoder.mall.user.dataobject.UserDO
;
import
cn.iocoder.mall.user.service.api.bo.UserBO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.Mappings
;
import
org.mapstruct.factory.Mappers
;
@Mapper
public
interface
UserConvert
{
UserConvert
INSTANCE
=
Mappers
.
getMapper
(
UserConvert
.
class
);
@Mappings
({})
UserBO
convert
(
UserDO
userDO
);
}
\ No newline at end of file
user/user-service-impl/src/main/java/cn/iocoder/mall/user/dataobject/OAuth2AccessTokenDO.java
浏览文件 @
4162eda3
...
...
@@ -7,7 +7,7 @@ public class OAuth2AccessTokenDO {
/**
* 访问令牌
*/
private
String
tokenI
d
;
private
String
i
d
;
/**
* 刷新令牌
*/
...
...
@@ -29,12 +29,12 @@ public class OAuth2AccessTokenDO {
*/
private
Date
createTime
;
public
String
get
Token
Id
()
{
return
tokenI
d
;
public
String
getId
()
{
return
i
d
;
}
public
OAuth2AccessTokenDO
set
TokenId
(
String
tokenI
d
)
{
this
.
tokenId
=
tokenI
d
;
public
OAuth2AccessTokenDO
set
Id
(
String
i
d
)
{
this
.
id
=
i
d
;
return
this
;
}
...
...
user/user-service-impl/src/main/java/cn/iocoder/mall/user/dataobject/OAuth2RefreshTokenDO.java
浏览文件 @
4162eda3
...
...
@@ -12,7 +12,7 @@ public class OAuth2RefreshTokenDO {
/**
* 刷新令牌
*/
private
String
tokenI
d
;
private
String
i
d
;
/**
* 用户编号
*/
...
...
@@ -30,12 +30,12 @@ public class OAuth2RefreshTokenDO {
*/
private
Date
createTime
;
public
String
get
Token
Id
()
{
return
tokenI
d
;
public
String
getId
()
{
return
i
d
;
}
public
OAuth2RefreshTokenDO
set
TokenId
(
String
tokenI
d
)
{
this
.
tokenId
=
tokenI
d
;
public
OAuth2RefreshTokenDO
set
Id
(
String
i
d
)
{
this
.
id
=
i
d
;
return
this
;
}
...
...
user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/MobileCodeServiceImpl.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
service
;
import
cn.iocoder.common.framework.util.ServiceExceptionUtil
;
import
cn.iocoder.common.framework.vo.CommonResult
;
import
cn.iocoder.mall.user.dao.MobileCodeMapper
;
import
cn.iocoder.mall.user.dataobject.MobileCodeDO
;
import
cn.iocoder.mall.user.service.api.MobileCodeService
;
...
...
@@ -63,6 +64,30 @@ public class MobileCodeServiceImpl implements MobileCodeService {
return
mobileCodePO
;
}
/**
* 校验手机号的最后一个手机验证码是否有效
*
* @param mobile 手机号
* @param code 验证码
* @return 手机验证码信息
*/
public
CommonResult
<
MobileCodeDO
>
validLastMobileCode2
(
String
mobile
,
String
code
)
{
MobileCodeDO
mobileCodePO
=
mobileCodeMapper
.
selectLast1ByMobile
(
mobile
);
if
(
mobileCodePO
==
null
)
{
// 若验证码不存在,抛出异常
return
ServiceExceptionUtil
.
error
(
UserErrorCodeEnum
.
MOBILE_CODE_NOT_FOUND
.
getCode
());
}
if
(
System
.
currentTimeMillis
()
-
mobileCodePO
.
getCreateTime
().
getTime
()
>=
codeExpireTimes
)
{
// 验证码已过期
return
ServiceExceptionUtil
.
error
(
UserErrorCodeEnum
.
MOBILE_CODE_EXPIRED
.
getCode
());
}
if
(
mobileCodePO
.
getUsed
())
{
// 验证码已使用
return
ServiceExceptionUtil
.
error
(
UserErrorCodeEnum
.
MOBILE_CODE_USED
.
getCode
());
}
if
(!
mobileCodePO
.
getCode
().
equals
(
code
))
{
return
ServiceExceptionUtil
.
error
(
UserErrorCodeEnum
.
MOBILE_CODE_NOT_CORRECT
.
getCode
());
}
return
CommonResult
.
success
(
mobileCodePO
);
}
/**
* 更新手机验证码已使用
*
...
...
user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java
浏览文件 @
4162eda3
...
...
@@ -2,6 +2,7 @@ package cn.iocoder.mall.user.service;
import
cn.iocoder.common.framework.exception.ServiceException
;
import
cn.iocoder.common.framework.util.ServiceExceptionUtil
;
import
cn.iocoder.common.framework.vo.CommonResult
;
import
cn.iocoder.mall.user.convert.OAuth2Convert
;
import
cn.iocoder.mall.user.dao.OAuth2AccessTokenMapper
;
import
cn.iocoder.mall.user.dao.OAuth2RefreshTokenMapper
;
...
...
@@ -11,12 +12,13 @@ import cn.iocoder.mall.user.dataobject.OAuth2RefreshTokenDO;
import
cn.iocoder.mall.user.dataobject.UserDO
;
import
cn.iocoder.mall.user.service.api.OAuth2Service
;
import
cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum
;
import
cn.iocoder.mall.user.service.api.
dt
o.OAuth2AccessTokenBO
;
import
cn.iocoder.mall.user.service.api.
dto.OAuth2AuthenticationDT
O
;
import
cn.iocoder.mall.user.service.api.
b
o.OAuth2AccessTokenBO
;
import
cn.iocoder.mall.user.service.api.
bo.OAuth2AuthenticationB
O
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.util.Assert
;
import
java.util.Date
;
import
java.util.UUID
;
...
...
@@ -61,15 +63,39 @@ public class OAuth2ServiceImpl implements OAuth2Service {
// 创建刷新令牌
OAuth2RefreshTokenDO
oauth2RefreshTokenDO
=
createOAuth2RefreshToken
(
userDO
.
getId
());
// 创建访问令牌
OAuth2AccessTokenDO
oauth2AccessTokenDO
=
createOAuth2AccessToken
(
userDO
.
getId
(),
oauth2RefreshTokenDO
.
get
Token
Id
());
OAuth2AccessTokenDO
oauth2AccessTokenDO
=
createOAuth2AccessToken
(
userDO
.
getId
(),
oauth2RefreshTokenDO
.
getId
());
// 标记已使用
mobileCodeService
.
useMobileCode
(
mobileCodeDO
.
getId
(),
userDO
.
getId
());
// 转换返回
return
OAuth2Convert
.
INSTANCE
.
convertWithExpiresIn
(
oauth2AccessTokenDO
);
return
OAuth2Convert
.
INSTANCE
.
convert
ToAccessToken
WithExpiresIn
(
oauth2AccessTokenDO
);
}
@Override
public
OAuth2AuthenticationDTO
checkToken
(
String
accessToken
)
throws
ServiceException
{
@Transactional
public
CommonResult
<
OAuth2AccessTokenBO
>
getAccessToken2
(
String
mobile
,
String
code
)
{
// 校验传入的 mobile 和 code 是否合法
CommonResult
<
MobileCodeDO
>
result
=
mobileCodeService
.
validLastMobileCode2
(
mobile
,
code
);
if
(
result
.
isError
())
{
return
CommonResult
.
error
(
result
);
}
// 获取用户
UserDO
userDO
=
userService
.
getUser
(
mobile
);
if
(
userDO
==
null
)
{
// 用户不存在,则进行创建用户
userDO
=
userService
.
createUser
(
mobile
);
Assert
.
notNull
(
userDO
,
"创建用户必然成功"
);
}
// 创建刷新令牌
OAuth2RefreshTokenDO
oauth2RefreshTokenDO
=
createOAuth2RefreshToken
(
userDO
.
getId
());
// 创建访问令牌
OAuth2AccessTokenDO
oauth2AccessTokenDO
=
createOAuth2AccessToken
(
userDO
.
getId
(),
oauth2RefreshTokenDO
.
getId
());
// 标记已使用
// mobileCodeService.useMobileCode(result.getData().getId(), userDO.getId());
// 转换返回
return
CommonResult
.
success
(
OAuth2Convert
.
INSTANCE
.
convertToAccessTokenWithExpiresIn
(
oauth2AccessTokenDO
));
}
@Override
public
OAuth2AuthenticationBO
checkToken
(
String
accessToken
)
throws
ServiceException
{
OAuth2AccessTokenDO
accessTokenDO
=
oauth2AccessTokenMapper
.
selectByTokenId
(
accessToken
);
if
(
accessTokenDO
==
null
)
{
// 不存在
throw
ServiceExceptionUtil
.
exception
(
UserErrorCodeEnum
.
OAUTH_INVALID_TOKEN_NOT_FOUND
.
getCode
());
...
...
@@ -81,11 +107,11 @@ public class OAuth2ServiceImpl implements OAuth2Service {
throw
ServiceExceptionUtil
.
exception
(
UserErrorCodeEnum
.
OAUTH_INVALID_TOKEN_INVALID
.
getCode
());
}
// 转换返回
return
new
OAuth2AuthenticationDTO
().
setUid
(
accessTokenDO
.
getUid
()
);
return
OAuth2Convert
.
INSTANCE
.
convertToAuthentication
(
accessTokenDO
);
}
private
OAuth2AccessTokenDO
createOAuth2AccessToken
(
Long
uid
,
String
refreshToken
)
{
OAuth2AccessTokenDO
accessToken
=
new
OAuth2AccessTokenDO
().
set
Token
Id
(
generateAccessToken
())
OAuth2AccessTokenDO
accessToken
=
new
OAuth2AccessTokenDO
().
setId
(
generateAccessToken
())
.
setRefreshToken
(
refreshToken
)
.
setUid
(
uid
)
.
setExpiresTime
(
new
Date
(
System
.
currentTimeMillis
()
+
accessTokenExpireTimeMillis
))
...
...
@@ -95,7 +121,7 @@ public class OAuth2ServiceImpl implements OAuth2Service {
}
private
OAuth2RefreshTokenDO
createOAuth2RefreshToken
(
Long
uid
)
{
OAuth2RefreshTokenDO
refreshToken
=
new
OAuth2RefreshTokenDO
().
set
Token
Id
(
generateRefreshToken
())
OAuth2RefreshTokenDO
refreshToken
=
new
OAuth2RefreshTokenDO
().
setId
(
generateRefreshToken
())
.
setUid
(
uid
)
.
setExpiresTime
(
new
Date
(
System
.
currentTimeMillis
()
+
refreshTokenExpireTimeMillis
))
.
setValid
(
true
);
...
...
@@ -111,4 +137,4 @@ public class OAuth2ServiceImpl implements OAuth2Service {
return
UUID
.
randomUUID
().
toString
().
replaceAll
(
"-"
,
""
);
}
}
}
\ No newline at end of file
user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/UserServiceImpl.java
浏览文件 @
4162eda3
package
cn
.
iocoder
.
mall
.
user
.
service
;
import
cn.iocoder.common.framework.util.ServiceExceptionUtil
;
import
cn.iocoder.mall.user.convert.UserConvert
;
import
cn.iocoder.mall.user.dao.UserMapper
;
import
cn.iocoder.mall.user.dao.UserRegisterMapper
;
import
cn.iocoder.mall.user.dataobject.UserDO
;
import
cn.iocoder.mall.user.dataobject.UserRegisterDO
;
import
cn.iocoder.mall.user.service.api.UserService
;
import
cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum
;
import
cn.iocoder.mall.user.service.api.dto.UserDTO
;
import
com.alibaba.dubbo.config.annotation.Service
;
import
cn.iocoder.mall.user.service.api.bo.UserBO
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.transaction.annotation.Transactional
;
...
...
@@ -34,7 +34,7 @@ public class UserServiceImpl implements UserService {
@Override
@Transactional
public
User
DT
O
createUser
(
String
mobile
,
String
code
)
{
public
User
B
O
createUser
(
String
mobile
,
String
code
)
{
// TODO 芋艿,校验手机格式
// 校验手机号的最后一个手机验证码是否有效
mobileCodeService
.
validLastMobileCode
(
mobile
,
code
);
...
...
@@ -48,7 +48,22 @@ public class UserServiceImpl implements UserService {
// 插入注册信息
createUserRegister
(
userDO
);
// 转换返回
return
new
UserDTO
().
setUid
(
userDO
.
getId
());
return
UserConvert
.
INSTANCE
.
convert
(
userDO
);
}
@Transactional
public
UserDO
createUser
(
String
mobile
)
{
// 校验用户是否已经存在
if
(
getUser
(
mobile
)
!=
null
)
{
throw
ServiceExceptionUtil
.
exception
(
UserErrorCodeEnum
.
USER_MOBILE_ALREADY_REGISTERED
.
getCode
());
}
// 创建用户
UserDO
userDO
=
new
UserDO
().
setMobile
(
mobile
).
setCreateTime
(
new
Date
());
userMapper
.
insert
(
userDO
);
// 插入注册信息
createUserRegister
(
userDO
);
// 转换返回
return
userDO
;
}
private
void
createUserRegister
(
UserDO
userDO
)
{
...
...
user/user-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml
浏览文件 @
4162eda3
...
...
@@ -4,10 +4,10 @@
<insert
id=
"insert"
parameterType=
"OAuth2AccessTokenDO"
>
INSERT INTO oauth2_access_token (
token_id, refresh_token,
id, valid, expires_time,
id, refresh_token, u
id, valid, expires_time,
create_time
) VALUES (
#{
tokenId}, #{refreshToken}, #{
id}, #{valid}, #{expiresTime},
#{
id}, #{refreshToken}, #{u
id}, #{valid}, #{expiresTime},
#{createTime}
)
</insert>
...
...
@@ -16,7 +16,7 @@
SELECT
id, valid, expires_time
FROM oauth2_access_token
WHERE token_id = #{
tokenI
d}
WHERE token_id = #{
i
d}
</select>
</mapper>
\ No newline at end of file
user/user-service-impl/src/main/resources/mapper/OAuth2RefreshTokenMapper.xml
浏览文件 @
4162eda3
...
...
@@ -4,9 +4,9 @@
<insert
id=
"insert"
parameterType=
"OAuth2RefreshTokenDO"
>
INSERT INTO oauth2_refresh_token (
token_id,
id, valid, expires_time, create_time
id, u
id, valid, expires_time, create_time
) VALUES (
#{
tokenId}, #{
id}, #{valid}, #{expiresTime}, #{createTime}
#{
id}, #{u
id}, #{valid}, #{expiresTime}, #{createTime}
)
</insert>
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论