Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
有来技术
youlai-mall
比较版本
fcf0c3fdc539f11d0990e136250f4d20738292d5...4b81b1e864eef5722da75ec9ddb938b1a1a871e5
Y
youlai-mall
项目概览
有来技术
/
youlai-mall
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
youlai-mall
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
源分支
4b81b1e864eef5722da75ec9ddb938b1a1a871e5
选择Git版本
...
目标分支
fcf0c3fdc539f11d0990e136250f4d20738292d5
选择Git版本
比较
Commits (4)
https://gitcode.net/u013737132/youlai-mall/-/commit/7be21be8d678811de973d6f842d267e7f7535951
fix: 修复未知异常被 `token` 无效处理器拦截报错token无效的错误
2024-03-05T00:23:06+08:00
hxr
1490493387@qq.com
https://gitcode.net/u013737132/youlai-mall/-/commit/ba7a543eb0d06c893e474cf9ceec91b9460ffbdb
fix: 短信验证码的参数名称修改
2024-03-05T00:23:59+08:00
hxr
1490493387@qq.com
https://gitcode.net/u013737132/youlai-mall/-/commit/f71ce019c0b56b8c70cb0eb5ed4e87d6fc1da958
fix: 从 JWT 获取 claim 添加非空判断
2024-03-05T00:24:34+08:00
hxr
1490493387@qq.com
https://gitcode.net/u013737132/youlai-mall/-/commit/4b81b1e864eef5722da75ec9ddb938b1a1a871e5
fix: 订单和商品服务错误修复
2024-03-05T00:25:00+08:00
hxr
1490493387@qq.com
隐藏空白更改
内联
并排
Showing
14 changed file
with
120 addition
and
99 deletion
+120
-99
mall-oms/oms-boot/src/test/java/com/youlai/mall/oms/controller/OrderControllerTest.java
...a/com/youlai/mall/oms/controller/OrderControllerTest.java
+6
-6
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/PmsSpuController.java
...om/youlai/mall/pms/controller/admin/PmsSpuController.java
+2
-2
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/app/SpuController.java
...ava/com/youlai/mall/pms/controller/app/SpuController.java
+2
-2
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/mapper/PmsSpuMapper.java
...rc/main/java/com/youlai/mall/pms/mapper/PmsSpuMapper.java
+10
-10
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/SpuService.java
...src/main/java/com/youlai/mall/pms/service/SpuService.java
+2
-2
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/SpuServiceImpl.java
...java/com/youlai/mall/pms/service/impl/SpuServiceImpl.java
+32
-36
mall-pms/pms-boot/src/main/resources/mapper/PmsSpuMapper.xml
mall-pms/pms-boot/src/main/resources/mapper/PmsSpuMapper.xml
+3
-3
youlai-auth/src/main/java/com/youlai/auth/oauth2/extension/sms/SmsAuthenticationConverter.java
...auth/oauth2/extension/sms/SmsAuthenticationConverter.java
+3
-3
youlai-auth/src/main/java/com/youlai/auth/oauth2/extension/sms/SmsAuthenticationProvider.java
.../auth/oauth2/extension/sms/SmsAuthenticationProvider.java
+3
-3
youlai-auth/src/main/java/com/youlai/auth/oauth2/extension/sms/SmsParameterNames.java
...m/youlai/auth/oauth2/extension/sms/SmsParameterNames.java
+1
-1
youlai-auth/src/test/java/com/youlai/auth/oauth2/SmsAuthenticationTests.java
...t/java/com/youlai/auth/oauth2/SmsAuthenticationTests.java
+1
-1
youlai-common/common-security/src/main/java/com/youlai/common/security/exception/MyAuthenticationEntryPoint.java
...common/security/exception/MyAuthenticationEntryPoint.java
+3
-11
youlai-common/common-security/src/main/java/com/youlai/common/security/util/SecurityUtils.java
...n/java/com/youlai/common/security/util/SecurityUtils.java
+46
-14
youlai-common/common-web/src/main/java/com/youlai/common/web/exception/GlobalExceptionHandler.java
...m/youlai/common/web/exception/GlobalExceptionHandler.java
+6
-5
未找到文件。
mall-oms/oms-boot/src/test/java/com/youlai/mall/oms/controller/OrderControllerTest.java
浏览文件 @
4b81b1e8
...
...
@@ -50,7 +50,7 @@ public class OrderControllerTest {
@Autowired
private
RestTemplate
restTemplate
;
private
final
String
mobile
=
"18866668888"
;
// 商城会员手机号
private
final
String
verifyC
ode
=
"666666"
;
// 短信验证码,666666是免校验验证码
private
final
String
c
ode
=
"666666"
;
// 短信验证码,666666是免校验验证码
private
final
Long
skuId
=
1L
;
// 购买商品ID
...
...
@@ -61,7 +61,7 @@ public class OrderControllerTest {
void
testPurchaseFlow_Normal
()
throws
Exception
{
// 会员登录
String
accessToken
=
acquireTokenByLogin
(
mobile
,
verifyC
ode
);
// 获取 accessToken,填充请求头用于身份认证
String
accessToken
=
acquireTokenByLogin
(
mobile
,
c
ode
);
// 获取 accessToken,填充请求头用于身份认证
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setBearerAuth
(
accessToken
);
...
...
@@ -86,7 +86,7 @@ public class OrderControllerTest {
void
testPurchaseFlow_PaymentTimeout
()
throws
Exception
{
// 会员登录
String
accessToken
=
acquireTokenByLogin
(
mobile
,
verifyC
ode
);
// 获取 accessToken,填充请求头用于身份认证
String
accessToken
=
acquireTokenByLogin
(
mobile
,
c
ode
);
// 获取 accessToken,填充请求头用于身份认证
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setBearerAuth
(
accessToken
);
...
...
@@ -204,10 +204,10 @@ public class OrderControllerTest {
* 登录获取访问令牌
*
* @param mobile 手机号
* @param
verifyC
ode 短信验证码
* @param
c
ode 短信验证码
* @return
*/
private
String
acquireTokenByLogin
(
String
mobile
,
String
verifyC
ode
)
{
private
String
acquireTokenByLogin
(
String
mobile
,
String
c
ode
)
{
String
clientId
=
"mall-app"
;
String
clientSecret
=
"123456"
;
String
tokenUrl
=
"http://localhost:9000/oauth2/token"
;
...
...
@@ -222,7 +222,7 @@ public class OrderControllerTest {
requestBody
.
add
(
"client_id"
,
clientId
);
requestBody
.
add
(
"client_secret"
,
clientSecret
);
requestBody
.
add
(
"mobile"
,
mobile
);
requestBody
.
add
(
"code"
,
verifyC
ode
);
requestBody
.
add
(
"code"
,
c
ode
);
// 创建 Basic Auth 头部
String
authHeader
=
clientId
+
":"
+
clientSecret
;
...
...
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/admin/PmsSpuController.java
浏览文件 @
4b81b1e8
...
...
@@ -30,8 +30,8 @@ public class PmsSpuController {
@Operation
(
summary
=
"商品分页列表"
)
@GetMapping
(
"/page"
)
public
PageResult
getSpuPage
(
SpuPageQuery
queryParams
)
{
IPage
<
PmsSpuPageVO
>
result
=
spuService
.
getSpuPage
(
queryParams
);
public
PageResult
listPagedSpu
(
SpuPageQuery
queryParams
)
{
IPage
<
PmsSpuPageVO
>
result
=
spuService
.
listPagedSpu
(
queryParams
);
return
PageResult
.
success
(
result
);
}
...
...
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/controller/app/SpuController.java
浏览文件 @
4b81b1e8
...
...
@@ -29,8 +29,8 @@ public class SpuController {
@Operation
(
summary
=
"商品分页列表"
)
@GetMapping
(
"/pages"
)
public
PageResult
getSpuPage
ForApp
(
SpuPageQuery
queryParams
)
{
IPage
<
SpuPageVO
>
result
=
spuService
.
getSpuPage
ForApp
(
queryParams
);
public
PageResult
<
SpuPageVO
>
listPagedSpu
ForApp
(
SpuPageQuery
queryParams
)
{
IPage
<
SpuPageVO
>
result
=
spuService
.
listPagedSpu
ForApp
(
queryParams
);
return
PageResult
.
success
(
result
);
}
...
...
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/mapper/PmsSpuMapper.java
浏览文件 @
4b81b1e8
...
...
@@ -14,22 +14,22 @@ import java.util.List;
public
interface
PmsSpuMapper
extends
BaseMapper
<
PmsSpu
>
{
/**
* Admin-
商品分页列表
* Admin-商品分页列表
*
* @param page
* @param queryParams
* @return
* @param page
分页参数
* @param queryParams
查询参数
* @return
商品分页列表
*/
List
<
PmsSpuPageVO
>
getSpuPage
(
Page
<
PmsSpuPageVO
>
page
,
SpuPageQuery
queryParams
);
List
<
PmsSpuPageVO
>
listPagedSpu
(
Page
<
PmsSpuPageVO
>
page
,
SpuPageQuery
queryParams
);
/**
*
「应用端」
商品分页列表
*
APP-
商品分页列表
*
* @param page
* @param queryParams
* @return
* @param page
分页参数
* @param queryParams
查询参数
* @return
商品分页列表
*/
List
<
SpuPageVO
>
getSpuPage
ForApp
(
Page
<
SpuPageVO
>
page
,
SpuPageQuery
queryParams
);
List
<
SpuPageVO
>
listPagedSpu
ForApp
(
Page
<
SpuPageVO
>
page
,
SpuPageQuery
queryParams
);
}
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/SpuService.java
浏览文件 @
4b81b1e8
...
...
@@ -24,7 +24,7 @@ public interface SpuService extends IService<PmsSpu> {
* @param queryParams
* @return
*/
IPage
<
PmsSpuPageVO
>
getSpuPage
(
SpuPageQuery
queryParams
);
IPage
<
PmsSpuPageVO
>
listPagedSpu
(
SpuPageQuery
queryParams
);
/**
* 「应用端」商品分页列表
...
...
@@ -32,7 +32,7 @@ public interface SpuService extends IService<PmsSpu> {
* @param queryParams
* @return
*/
IPage
<
SpuPageVO
>
getSpuPage
ForApp
(
SpuPageQuery
queryParams
);
IPage
<
SpuPageVO
>
listPagedSpu
ForApp
(
SpuPageQuery
queryParams
);
/**
...
...
mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/SpuServiceImpl.java
浏览文件 @
4b81b1e8
...
...
@@ -33,12 +33,11 @@ import org.springframework.transaction.annotation.Transactional;
import
java.util.*
;
import
java.util.stream.Collectors
;
/**
* 商品业务实现类
*
* @author
<a href="mailto:xianrui0365@163.com">haoxr</a>
* @
date 2021/8/
8
* @author
Ray Hao
* @
since 2021/08/0
8
*/
@Service
@RequiredArgsConstructor
...
...
@@ -47,35 +46,33 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
private
final
SkuService
skuService
;
private
final
SpuAttributeService
spuAttributeService
;
private
final
MemberFeignClient
memberFeignClient
;
private
final
SpuConverter
spuConverter
;
private
final
SpuAttributeConverter
spuAttributeConverter
;
/**
* Admin-商品分页列表
*
* @param queryParams
* @return
* @param queryParams
查询参数
* @return
商品分页列表 IPage<PmsSpuPageVO>
*/
@Override
public
IPage
<
PmsSpuPageVO
>
getSpuPage
(
SpuPageQuery
queryParams
)
{
public
IPage
<
PmsSpuPageVO
>
listPagedSpu
(
SpuPageQuery
queryParams
)
{
Page
<
PmsSpuPageVO
>
page
=
new
Page
<>(
queryParams
.
getPageNum
(),
queryParams
.
getPageSize
());
List
<
PmsSpuPageVO
>
list
=
this
.
baseMapper
.
getSpuPage
(
page
,
queryParams
);
List
<
PmsSpuPageVO
>
list
=
this
.
baseMapper
.
listPagedSpu
(
page
,
queryParams
);
page
.
setRecords
(
list
);
return
page
;
}
/**
*
「应用端」
商品分页列表
*
APP-
商品分页列表
*
* @param queryParams
* @return
* @param queryParams
查询参数
* @return
商品分页列表 IPage<SpuPageVO>
*/
@Override
public
IPage
<
SpuPageVO
>
getSpuPage
ForApp
(
SpuPageQuery
queryParams
)
{
public
IPage
<
SpuPageVO
>
listPagedSpu
ForApp
(
SpuPageQuery
queryParams
)
{
Page
<
SpuPageVO
>
page
=
new
Page
<>(
queryParams
.
getPageNum
(),
queryParams
.
getPageSize
());
List
<
SpuPageVO
>
list
=
this
.
baseMapper
.
getSpuPage
ForApp
(
page
,
queryParams
);
List
<
SpuPageVO
>
list
=
this
.
baseMapper
.
listPagedSpu
ForApp
(
page
,
queryParams
);
page
.
setRecords
(
list
);
return
page
;
}
...
...
@@ -84,7 +81,7 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
* App-获取商品详情
*
* @param spuId 商品ID
* @return
* @return
商品详情
*/
@Override
public
SpuDetailVO
getSpuDetailForApp
(
Long
spuId
)
{
...
...
@@ -178,7 +175,7 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
* 获取商品详情
*
* @param spuId 商品ID
* @return
* @return
商品详情
*/
@Override
public
PmsSpuDetailVO
getSpuDetail
(
Long
spuId
)
{
...
...
@@ -212,8 +209,8 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
/**
* 添加商品
*
* @param formData
* @return
* @param formData
商品表单
* @return
是否成功
*/
@Override
@Transactional
...
...
@@ -245,7 +242,7 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
*
* @param spuId 商品ID
* @param formData 商品表单
* @return
* @return
是否成功
*/
@Transactional
@Override
...
...
@@ -277,7 +274,7 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
* 删除商品
*
* @param ids 商品ID,多个以英文逗号(,)分割
* @return
* @return
是否成功
*/
@Override
@Transactional
...
...
@@ -301,7 +298,7 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
/**
* 获取商品秒杀接口
*
* @return
* @return
商品秒杀列表
*/
@Override
public
List
<
SeckillingSpuVO
>
listSeckillingSpu
()
{
...
...
@@ -309,27 +306,26 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
.
select
(
PmsSpu:
:
getId
,
PmsSpu:
:
getName
,
PmsSpu:
:
getPicUrl
,
PmsSpu:
:
getPrice
)
.
orderByDesc
(
PmsSpu:
:
getCreateTime
)
);
List
<
SeckillingSpuVO
>
list
=
spuConverter
.
entity2SeckillingVO
(
entities
);
return
list
;
return
spuConverter
.
entity2SeckillingVO
(
entities
);
}
/**
* 保存SKU,需要替换提交表单中的临时规格ID
*
* @param
goodsId
* @param skuList
* @param specTempIdIdMap
* @return
* @param
spuId 商品ID
* @param skuList
SKU列表
* @param specTempIdIdMap
临时规格ID和持久化数据库得到的规格ID的映射
* @return
是否成功
*/
private
boolean
saveSku
(
Long
goods
Id
,
List
<
PmsSku
>
skuList
,
Map
<
String
,
Long
>
specTempIdIdMap
)
{
private
boolean
saveSku
(
Long
spu
Id
,
List
<
PmsSku
>
skuList
,
Map
<
String
,
Long
>
specTempIdIdMap
)
{
// 删除SKU
List
<
Long
>
formSkuIds
=
skuList
.
stream
().
map
(
PmsSku:
:
getId
).
collect
(
Collectors
.
toList
()
);
List
<
Long
>
formSkuIds
=
skuList
.
stream
().
map
(
PmsSku:
:
getId
).
toList
(
);
List
<
Long
>
dbSkuIds
=
skuService
.
list
(
new
LambdaQueryWrapper
<
PmsSku
>().
eq
(
PmsSku:
:
getSpuId
,
goods
Id
)
List
<
Long
>
dbSkuIds
=
skuService
.
list
(
new
LambdaQueryWrapper
<
PmsSku
>().
eq
(
PmsSku:
:
getSpuId
,
spu
Id
)
.
select
(
PmsSku:
:
getId
)).
stream
().
map
(
PmsSku:
:
getId
)
.
collect
(
Collectors
.
toList
()
);
.
toList
(
);
List
<
Long
>
removeSkuIds
=
dbSkuIds
.
stream
().
filter
(
dbSkuId
->
!
formSkuIds
.
contains
(
dbSkuId
)).
collect
(
Collectors
.
toList
());
...
...
@@ -344,7 +340,7 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
.
map
(
specId
->
specId
.
startsWith
(
ProductConstants
.
SPEC_TEMP_ID_PREFIX
)
?
specTempIdIdMap
.
get
(
specId
)
+
""
:
specId
)
.
collect
(
Collectors
.
joining
(
"_"
));
sku
.
setSpecIds
(
specIds
);
sku
.
setSpuId
(
goods
Id
);
sku
.
setSpuId
(
spu
Id
);
return
sku
;
}).
collect
(
Collectors
.
toList
());
return
skuService
.
saveOrUpdateBatch
(
pmsSkuList
);
...
...
@@ -366,14 +362,14 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
List
<
Long
>
retainAttrIds
=
attrList
.
stream
()
.
filter
(
item
->
item
.
getId
()
!=
null
)
.
map
(
item
->
Convert
.
toLong
(
item
.
getId
()))
.
collect
(
Collectors
.
toList
()
);
.
toList
(
);
// 1.2 获取原商品属性ID集合
List
<
Long
>
originAttrIds
=
spuAttributeService
.
list
(
new
LambdaQueryWrapper
<
PmsSpuAttribute
>()
.
eq
(
PmsSpuAttribute:
:
getSpuId
,
spuId
).
eq
(
PmsSpuAttribute:
:
getType
,
AttributeTypeEnum
.
ATTR
.
getValue
())
.
select
(
PmsSpuAttribute:
:
getId
))
.
stream
()
.
map
(
PmsSpuAttribute:
:
getId
)
.
collect
(
Collectors
.
toList
()
);
.
toList
(
);
// 1.3 需要删除的商品属性:原商品属性-此次提交保留的属性
List
<
Long
>
removeAttrValIds
=
originAttrIds
.
stream
()
.
filter
(
id
->
!
retainAttrIds
.
contains
(
id
))
...
...
@@ -413,7 +409,7 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
List
<
Long
>
retainSpuSpecIds
=
specList
.
stream
()
.
filter
(
item
->
!
item
.
getId
().
startsWith
(
ProductConstants
.
SPEC_TEMP_ID_PREFIX
))
.
map
(
item
->
Convert
.
toLong
(
item
.
getId
()))
.
collect
(
Collectors
.
toList
()
);
.
toList
(
);
// 1.2 原商品规格
List
<
Long
>
originSpuSpecIds
=
spuAttributeService
.
list
(
new
LambdaQueryWrapper
<
PmsSpuAttribute
>()
...
...
@@ -421,7 +417,7 @@ public class SpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements
.
eq
(
PmsSpuAttribute:
:
getType
,
AttributeTypeEnum
.
SPEC
.
getValue
())
.
select
(
PmsSpuAttribute:
:
getId
))
.
stream
().
map
(
PmsSpuAttribute:
:
getId
)
.
collect
(
Collectors
.
toList
()
);
.
toList
(
);
// 1.3 需要删除的商品规格:原商品规格-此次提交保留的规格
List
<
Long
>
removeSpuSpecIds
=
originSpuSpecIds
.
stream
().
filter
(
id
->
!
retainSpuSpecIds
.
contains
(
id
))
...
...
mall-pms/pms-boot/src/main/resources/mapper/PmsSpuMapper.xml
浏览文件 @
4b81b1e8
...
...
@@ -33,7 +33,7 @@
</resultMap>
<!--Admin-商品分页列表-->
<select
id=
"
getSpuPage
"
resultMap=
"BaseResultMap"
>
<select
id=
"
listPagedSpu
"
resultMap=
"BaseResultMap"
>
SELECT
t1.id,
t1.name,
...
...
@@ -65,7 +65,7 @@
<!--「应用端」商品分页列表-->
<select
id=
"list
SpuPages
"
resultType=
"com.youlai.mall.pms.model.vo.SpuPageVO"
>
<select
id=
"list
PagedSpuForApp
"
resultType=
"com.youlai.mall.pms.model.vo.SpuPageVO"
>
SELECT
id,
NAME,
...
...
@@ -84,7 +84,7 @@
</where>
ORDER BY
<if
test=
'queryParams.sortField!=null and queryParams.sortField.trim() neq "" and queryParams.sortField !=null and queryParams.sort.trim() neq ""'
>
#{queryParams.sortField} #
{queryParams.sort} ,
${queryParams.sortField} $
{queryParams.sort} ,
</if>
create_time desc
</select>
...
...
youlai-auth/src/main/java/com/youlai/auth/oauth2/extension/sms/SmsAuthenticationConverter.java
浏览文件 @
4b81b1e8
...
...
@@ -66,11 +66,11 @@ public class SmsAuthenticationConverter implements AuthenticationConverter {
}
// 验证码(必需)
String
verifyCode
=
parameters
.
getFirst
(
SmsParameterNames
.
VERIFY_
CODE
);
if
(
StrUtil
.
isBlank
(
verifyC
ode
))
{
String
code
=
parameters
.
getFirst
(
SmsParameterNames
.
CODE
);
if
(
StrUtil
.
isBlank
(
c
ode
))
{
OAuth2EndpointUtils
.
throwError
(
OAuth2ErrorCodes
.
INVALID_REQUEST
,
SmsParameterNames
.
VERIFY_
CODE
,
SmsParameterNames
.
CODE
,
OAuth2EndpointUtils
.
ACCESS_TOKEN_REQUEST_ERROR_URI
);
}
...
...
youlai-auth/src/main/java/com/youlai/auth/oauth2/extension/sms/SmsAuthenticationProvider.java
浏览文件 @
4b81b1e8
...
...
@@ -86,13 +86,13 @@ public class SmsAuthenticationProvider implements AuthenticationProvider {
// 短信验证码校验
Map
<
String
,
Object
>
additionalParameters
=
smsAuthenticationToken
.
getAdditionalParameters
();
String
mobile
=
(
String
)
additionalParameters
.
get
(
SmsParameterNames
.
MOBILE
);
String
verifyCode
=
(
String
)
additionalParameters
.
get
(
SmsParameterNames
.
VERIFY_
CODE
);
String
code
=
(
String
)
additionalParameters
.
get
(
SmsParameterNames
.
CODE
);
if
(!
verifyC
ode
.
equals
(
"666666"
))
{
// 666666 是后门,因为短信收费,正式环境删除这个if
if
(!
c
ode
.
equals
(
"666666"
))
{
// 666666 是后门,因为短信收费,正式环境删除这个if
String
codeKey
=
RedisConstants
.
LOGIN_SMS_CODE_PREFIX
+
mobile
;
String
cacheCode
=
(
String
)
redisTemplate
.
opsForValue
().
get
(
codeKey
);
if
(!
StrUtil
.
equals
(
verifyC
ode
,
cacheCode
))
{
if
(!
StrUtil
.
equals
(
c
ode
,
cacheCode
))
{
throw
new
OAuth2AuthenticationException
(
"验证码错误"
);
}
}
...
...
youlai-auth/src/main/java/com/youlai/auth/oauth2/extension/sms/SmsParameterNames.java
浏览文件 @
4b81b1e8
...
...
@@ -32,7 +32,7 @@ public final class SmsParameterNames {
/**
* 验证码
*/
public
static
final
String
VERIFY_CODE
=
"verifyC
ode"
;
public
static
final
String
CODE
=
"c
ode"
;
private
SmsParameterNames
()
{
...
...
youlai-auth/src/test/java/com/youlai/auth/oauth2/SmsAuthenticationTests.java
浏览文件 @
4b81b1e8
...
...
@@ -32,7 +32,7 @@ public class SmsAuthenticationTests {
this
.
mvc
.
perform
(
post
(
"/oauth2/token"
)
.
param
(
OAuth2ParameterNames
.
GRANT_TYPE
,
"sms_code"
)
.
param
(
"mobile"
,
"18866668888"
)
.
param
(
"
verifyC
ode"
,
"666666"
)
.
param
(
"
c
ode"
,
"666666"
)
.
headers
(
headers
))
.
andDo
(
print
())
.
andExpect
(
status
().
isOk
())
...
...
youlai-common/common-security/src/main/java/com/youlai/common/security/exception/MyAuthenticationEntryPoint.java
浏览文件 @
4b81b1e8
...
...
@@ -15,7 +15,7 @@ import java.io.IOException;
* 自定义 token 无效异常
*
* @author haoxr
* @
dat
e 2022/11/13
* @
sinc
e 2022/11/13
*/
@Component
public
class
MyAuthenticationEntryPoint
implements
AuthenticationEntryPoint
{
...
...
@@ -23,16 +23,8 @@ public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
public
void
commence
(
HttpServletRequest
request
,
HttpServletResponse
response
,
AuthenticationException
authException
)
throws
IOException
{
response
.
setContentType
(
"application/json"
);
int
status
=
response
.
getStatus
();
ObjectMapper
mapper
=
new
ObjectMapper
();
if
(
HttpServletResponse
.
SC_NOT_FOUND
==
status
)
{
response
.
setStatus
(
HttpServletResponse
.
SC_NOT_FOUND
);
mapper
.
writeValue
(
response
.
getOutputStream
(),
Result
.
failed
(
ResultCode
.
RESOURCE_NOT_FOUND
));
}
else
{
response
.
setStatus
(
HttpServletResponse
.
SC_UNAUTHORIZED
);
mapper
.
writeValue
(
response
.
getOutputStream
(),
Result
.
failed
(
ResultCode
.
TOKEN_INVALID
));
}
response
.
setStatus
(
HttpServletResponse
.
SC_UNAUTHORIZED
);
mapper
.
writeValue
(
response
.
getOutputStream
(),
Result
.
failed
(
ResultCode
.
TOKEN_INVALID
));
}
}
youlai-common/common-security/src/main/java/com/youlai/common/security/util/SecurityUtils.java
浏览文件 @
4b81b1e8
...
...
@@ -21,18 +21,27 @@ import java.util.stream.Collectors;
public
class
SecurityUtils
{
public
static
Long
getUserId
()
{
return
Convert
.
toLong
(
getTokenAttributes
().
get
(
"userId"
));
Map
<
String
,
Object
>
tokenAttributes
=
getTokenAttributes
();
if
(
tokenAttributes
!=
null
)
{
return
Convert
.
toLong
(
tokenAttributes
.
get
(
"userId"
));
}
return
null
;
}
public
static
String
getUsername
()
{
Authentication
authentication
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
return
authentication
.
getName
();
if
(
authentication
!=
null
)
{
return
authentication
.
getName
();
}
return
null
;
}
public
static
Map
<
String
,
Object
>
getTokenAttributes
()
{
Authentication
authentication
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
JwtAuthenticationToken
jwtAuthenticationToken
=
(
JwtAuthenticationToken
)
authentication
;
return
jwtAuthenticationToken
.
getTokenAttributes
();
if
(
authentication
instanceof
JwtAuthenticationToken
jwtAuthenticationToken
)
{
return
jwtAuthenticationToken
.
getTokenAttributes
();
}
return
null
;
}
...
...
@@ -41,30 +50,45 @@ public class SecurityUtils {
*/
public
static
Set
<
String
>
getRoles
()
{
Authentication
authentication
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
return
AuthorityUtils
.
authorityListToSet
(
authentication
.
getAuthorities
())
.
stream
()
.
collect
(
Collectors
.
collectingAndThen
(
Collectors
.
toSet
(),
Collections:
:
unmodifiableSet
));
if
(
authentication
!=
null
)
{
return
AuthorityUtils
.
authorityListToSet
(
authentication
.
getAuthorities
())
.
stream
()
.
collect
(
Collectors
.
collectingAndThen
(
Collectors
.
toSet
(),
Collections:
:
unmodifiableSet
));
}
return
null
;
}
/**
* 获取部门ID
*/
public
static
Long
getDeptId
()
{
return
Convert
.
toLong
(
getTokenAttributes
().
get
(
"deptId"
));
Map
<
String
,
Object
>
tokenAttributes
=
getTokenAttributes
();
if
(
tokenAttributes
!=
null
)
{
return
Convert
.
toLong
(
tokenAttributes
.
get
(
"deptId"
));
}
return
null
;
}
public
static
boolean
isRoot
()
{
return
getRoles
().
contains
(
SystemConstants
.
ROOT_ROLE_CODE
);
Set
<
String
>
roles
=
getRoles
();
return
roles
!=
null
&&
roles
.
contains
(
SystemConstants
.
ROOT_ROLE_CODE
);
}
public
static
String
getJti
()
{
return
String
.
valueOf
(
getTokenAttributes
().
get
(
"jti"
));
Map
<
String
,
Object
>
tokenAttributes
=
getTokenAttributes
();
if
(
tokenAttributes
!=
null
)
{
return
String
.
valueOf
(
tokenAttributes
.
get
(
"jti"
));
}
return
null
;
}
public
static
Long
getExp
()
{
return
Convert
.
toLong
(
getTokenAttributes
().
get
(
"exp"
));
Map
<
String
,
Object
>
tokenAttributes
=
getTokenAttributes
();
if
(
tokenAttributes
!=
null
)
{
return
Convert
.
toLong
(
tokenAttributes
.
get
(
"exp"
));
}
return
null
;
}
/**
...
...
@@ -74,7 +98,11 @@ public class SecurityUtils {
* @see com.youlai.common.mybatis.enums.DataScopeEnum
*/
public
static
Integer
getDataScope
()
{
return
Convert
.
toInt
(
getTokenAttributes
().
get
(
"dataScope"
));
Map
<
String
,
Object
>
tokenAttributes
=
getTokenAttributes
();
if
(
tokenAttributes
!=
null
)
{
return
Convert
.
toInt
(
tokenAttributes
.
get
(
"dataScope"
));
}
return
null
;
}
/**
...
...
@@ -83,6 +111,10 @@ public class SecurityUtils {
* @return 会员ID
*/
public
static
Long
getMemberId
()
{
return
Convert
.
toLong
(
getTokenAttributes
().
get
(
"memberId"
));
Map
<
String
,
Object
>
tokenAttributes
=
getTokenAttributes
();
if
(
tokenAttributes
!=
null
)
{
return
Convert
.
toLong
(
tokenAttributes
.
get
(
"memberId"
));
}
return
null
;
}
}
youlai-common/common-web/src/main/java/com/youlai/common/web/exception/GlobalExceptionHandler.java
浏览文件 @
4b81b1e8
...
...
@@ -23,6 +23,7 @@ import org.springframework.web.servlet.NoHandlerFoundException;
import
jakarta.servlet.ServletException
;
import
jakarta.validation.ConstraintViolation
;
import
jakarta.validation.ConstraintViolationException
;
import
java.sql.SQLSyntaxErrorException
;
import
java.util.concurrent.CompletionException
;
import
java.util.regex.Matcher
;
...
...
@@ -201,15 +202,15 @@ public class GlobalExceptionHandler {
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
@ExceptionHandler
(
Exception
.
class
)
public
<
T
>
Result
<
T
>
handleException
(
Exception
e
)
{
e
.
printStackTrace
(
);
log
.
error
(
"unknown exception:{}"
,
e
.
getMessage
(),
e
);
String
errorMsg
=
e
.
getMessage
();
if
(
StrUtil
.
isNotBlank
(
errorMsg
)
&&
errorMsg
.
contains
(
"denied to user"
))
{
return
Result
.
failed
(
ResultCode
.
FORBIDDEN_OPERATION
);
}
else
{
log
.
error
(
"unknown exception"
);
errorMsg
=
e
.
getCause
().
getMessage
();
return
Result
.
failed
(
errorMsg
);
}
if
(
StrUtil
.
isBlank
(
errorMsg
))
{
errorMsg
=
"系统异常"
;
}
return
Result
.
failed
(
errorMsg
);
}
/**
...
...