Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yudao-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hblj
yudao-cloud
Commits
6cbce274
提交
6cbce274
authored
2月 25, 2019
作者:
YunaiV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rest API ,统一使用 CommonResult 做了一次替换~
上级
4162eda3
隐藏空白字符变更
内嵌
并排
正在显示
11 个修改的文件
包含
68 行增加
和
22 行删除
+68
-22
pom.xml
common/common-framework/pom.xml
+5
-0
CommonResult.java
...ain/java/cn/iocoder/common/framework/vo/CommonResult.java
+3
-0
PassportController.java
...a/cn/iocoder/mall/user/controller/PassportController.java
+5
-2
UserController.java
.../java/cn/iocoder/mall/user/controller/UserController.java
+6
-2
UserVO.java
...ication/src/main/java/cn/iocoder/mall/user/vo/UserVO.java
+20
-0
SecurityInterceptor.java
...ocoder/mall/user/sdk/interceptor/SecurityInterceptor.java
+6
-1
MobileCodeService.java
...a/cn/iocoder/mall/user/service/api/MobileCodeService.java
+2
-1
OAuth2Service.java
.../java/cn/iocoder/mall/user/service/api/OAuth2Service.java
+5
-4
MobileCodeServiceImpl.java
...a/cn/iocoder/mall/user/service/MobileCodeServiceImpl.java
+7
-4
OAuth2ServiceImpl.java
.../java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java
+6
-6
OAuth2AccessTokenMapper.xml
...mpl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml
+3
-2
没有找到文件。
common/common-framework/pom.xml
浏览文件 @
6cbce274
...
...
@@ -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>
...
...
common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java
浏览文件 @
6cbce274
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
();
}
...
...
user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java
浏览文件 @
6cbce274
...
...
@@ -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 登陆
...
...
user/user-application/src/main/java/cn/iocoder/mall/user/controller/UserController.java
浏览文件 @
6cbce274
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
);
}
}
\ No newline at end of file
user/user-application/src/main/java/cn/iocoder/mall/user/vo/UserVO.java
0 → 100644
浏览文件 @
6cbce274
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
user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/SecurityInterceptor.java
浏览文件 @
6cbce274
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
);
...
...
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/MobileCodeService.java
浏览文件 @
6cbce274
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
;
}
user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/OAuth2Service.java
浏览文件 @
6cbce274
...
...
@@ -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
}
\ No newline at end of file
user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/MobileCodeServiceImpl.java
浏览文件 @
6cbce274
...
...
@@ -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
);
}
}
\ No newline at end of file
user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java
浏览文件 @
6cbce274
...
...
@@ -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
)
{
...
...
user/user-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml
浏览文件 @
6cbce274
...
...
@@ -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>
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论