Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
liyeyun-user-web
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cfld-backend
liyeyun-user-web
Commits
1e8587ab
Commit
1e8587ab
authored
Dec 14, 2023
by
zhouxudong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改提示语 优化代码
parent
d541b591
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
257 additions
and
37 deletions
+257
-37
WebConfig.java
src/main/java/com/lyy/user/config/auth/WebConfig.java
+4
-4
MsgConstants.java
src/main/java/com/lyy/user/constant/MsgConstants.java
+19
-0
SysUserInfoServiceImpl.java
...user/moudle/user/service/impl/SysUserInfoServiceImpl.java
+20
-31
IpUtils.java
src/main/java/com/lyy/user/util/IpUtils.java
+187
-0
RedisUtil.java
src/main/java/com/lyy/user/util/redis/RedisUtil.java
+25
-0
application.yml
src/main/resources/application.yml
+2
-2
No files found.
src/main/java/com/lyy/user/config/auth/WebConfig.java
View file @
1e8587ab
...
...
@@ -25,10 +25,10 @@ public class WebConfig implements WebMvcConfigurer {
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
//
registry
//
.addInterceptor(signterceptor)
//
.addPathPatterns(getIncludePathPatterns())
//
.excludePathPatterns(Arrays.asList(swagger()));
registry
.
addInterceptor
(
signterceptor
)
.
addPathPatterns
(
getIncludePathPatterns
())
.
excludePathPatterns
(
Arrays
.
asList
(
swagger
()));
// 添加需要拦截的路径,以及处理拦截的拦截器
registry
// 拦截器
...
...
src/main/java/com/lyy/user/constant/MsgConstants.java
0 → 100644
View file @
1e8587ab
package
com
.
lyy
.
user
.
constant
;
/**
* @Author:zhouxudong
* @version: 1.0
* @Date: 2023/12/14 14:57
* @Description:
*/
public
class
MsgConstants
{
public
static
final
String
sms
=
"短信发送频繁,每日短信发送次数为%s条"
;
public
static
final
String
codeError
=
"验证码错误"
;
public
static
final
String
codeNone
=
"验证码无效,请重新发送"
;
public
static
final
String
codeUse
=
"验证码失效,请重新发送"
;
public
static
final
String
noUser
=
"当前用户不存在"
;
public
static
final
String
passwordError
=
"用户名或密码不正确"
;
public
static
final
String
needSign
=
"该用户不存在,请先注册"
;
}
src/main/java/com/lyy/user/moudle/user/service/impl/SysUserInfoServiceImpl.java
View file @
1e8587ab
...
...
@@ -14,6 +14,7 @@ import com.lyy.user.config.LyyConfig;
import
com.lyy.user.config.exception.ServiceException
;
import
com.lyy.user.config.other.BaseContextHandler
;
import
com.lyy.user.constant.Constants
;
import
com.lyy.user.constant.MsgConstants
;
import
com.lyy.user.constant.RedisConstants
;
import
com.lyy.user.domain.JwtInfo
;
import
com.lyy.user.enums.BusinessTypeEnum
;
...
...
@@ -95,10 +96,10 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
userQuery
.
eq
(
SysUserInfo:
:
getPhonenumber
,
loginVo
.
getPhone
());
SysUserInfo
sysUserInfo
=
this
.
getOne
(
userQuery
);
if
(
ObjectUtils
.
isEmpty
(
sysUserInfo
))
{
throw
new
ServiceException
(
"当前用户不存在"
);
throw
new
ServiceException
(
MsgConstants
.
noUser
);
}
if
(
ObjectUtils
.
isEmpty
(
sysUserInfo
.
getPassword
()))
{
throw
new
ServiceException
(
"用户名或密码不正确"
);
throw
new
ServiceException
(
MsgConstants
.
passwordError
);
}
if
(!
checkPassword
(
sysUserInfo
.
getPassword
(),
loginVo
.
getPassword
()))
{
throw
new
ServiceException
(
"当前用户密码输入错误"
);
...
...
@@ -137,10 +138,10 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
String
code
=
phoneLoginVo
.
getCode
();
Object
msgCode
=
getPhoneKey
(
phone
,
SendMsgTypeEnum
.
LOGIN
.
getCode
());
if
(
ObjectUtils
.
isEmpty
(
msgCode
))
{
throw
new
ServiceException
(
"验证码失效,请重新发送"
);
throw
new
ServiceException
(
MsgConstants
.
codeUse
);
}
if
(!
code
.
equals
(
msgCode
))
{
throw
new
ServiceException
(
"验证码有误"
);
throw
new
ServiceException
(
MsgConstants
.
codeError
);
}
String
token
=
JwtTokenUtil
.
createToken
(
sysUserInfo
,
null
);
RedisUtil
.
set
(
...
...
@@ -192,24 +193,6 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
lambdaQueryWrapper
.
eq
(
SysUserInfo:
:
getPhonenumber
,
phone
);
return
this
.
count
(
lambdaQueryWrapper
)
>
0
;
}
/**
* @description: 手机号每日发送次数
* @date: 2023/11/27 15:32
* @param: [phone]
* @return: void
*/
private
void
limitSms
(
String
phone
)
{
String
key
=
RedisConstants
.
LIMIT_SMS
+
phone
;
DateTime
now
=
DateUtil
.
date
();
DateTime
startTime
=
DateUtil
.
offsetDay
(
now
,
this
.
intervalTime
*
-
1
);
RedisUtil
.
delZset
(
key
,
0
,
startTime
.
getTime
());
Long
count
=
RedisUtil
.
countZset
(
key
,
startTime
.
getTime
(),
now
.
getTime
());
if
(
count
==
null
||
count
<=
limitSms
)
{
RedisUtil
.
addZset
(
key
,
now
.
getTime
(),
now
.
getTime
());
}
else
{
throw
new
ServiceException
(
"短信发送频繁,每日短信发送次数为"
+
limitSms
);
}
}
/**
* @description: 发送短信
...
...
@@ -228,7 +211,6 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
if
(
StringUtils
.
isEmpty
(
SendMsgTypeEnum
.
getTemplateId
(
sendPhoneVo
.
getType
())))
{
throw
new
ServiceException
(
"参数有误"
);
}
limitSms
(
sendPhoneVo
.
getPhone
());
String
key
=
RedisConstants
.
MSG_KEY
+
sendPhoneVo
.
getType
()
+
":"
+
sendPhoneVo
.
getPhone
();
//
Long
expire
=
RedisUtil
.
getExpire
(
key
);
...
...
@@ -236,6 +218,13 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
if
(
expire
!=
null
&&
expire
>
Constants
.
INTERVAL_TIME
)
{
throw
new
ServiceException
(
"短信已发送,请稍后重试"
);
}
String
limitKey
=
RedisConstants
.
LIMIT_SMS
+
sendPhoneVo
.
getPhone
();
boolean
limitResult
=
RedisUtil
.
rateLimit
(
limitKey
,
intervalTime
,
limitSms
);
if
(!
limitResult
){
throw
new
ServiceException
(
String
.
format
(
MsgConstants
.
sms
,
limitSms
));
}
int
randomInt
=
RandomUtil
.
randomInt
(
100000
,
999999
);
List
<
Integer
>
temp
=
new
ArrayList
<>();
temp
.
add
(
randomInt
);
...
...
@@ -284,10 +273,10 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
}
Object
code
=
getPhoneKey
(
phone
,
SendMsgTypeEnum
.
REGISTER
.
getCode
());
if
(
ObjectUtils
.
isEmpty
(
code
))
{
throw
new
ServiceException
(
"验证码已失效"
);
throw
new
ServiceException
(
MsgConstants
.
codeUse
);
}
if
(!
registerVo
.
getCode
().
equals
(
code
))
{
throw
new
ServiceException
(
"验证码有误"
);
throw
new
ServiceException
(
MsgConstants
.
codeError
);
}
SysUserInfo
userInfo
=
new
SysUserInfo
();
userInfo
.
setLoginName
(
phone
);
...
...
@@ -331,15 +320,15 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
userQuery
.
eq
(
SysUserInfo:
:
getPhonenumber
,
phone
);
SysUserInfo
sysUserInfo
=
this
.
getOne
(
userQuery
);
if
(
ObjectUtils
.
isEmpty
(
sysUserInfo
))
{
throw
new
ServiceException
(
"该用户不存在,请先注册"
);
throw
new
ServiceException
(
MsgConstants
.
needSign
);
}
String
key
=
RedisConstants
.
MSG_KEY
+
type
+
":"
+
phone
;
Object
code
=
RedisUtil
.
get
(
key
);
if
(
ObjectUtils
.
isEmpty
(
code
))
{
throw
new
ServiceException
(
"验证码无效,请重新发送"
);
throw
new
ServiceException
(
MsgConstants
.
codeNone
);
}
if
(!
code
.
equals
(
forgetVo
.
getCode
()))
{
throw
new
ServiceException
(
"验证码错误"
);
throw
new
ServiceException
(
MsgConstants
.
codeError
);
}
String
newPassword
=
DigestUtils
.
md5DigestAsHex
(
...
...
@@ -363,15 +352,15 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
userQuery
.
eq
(
SysUserInfo:
:
getPhonenumber
,
phone
);
SysUserInfo
sysUserInfo
=
this
.
getOne
(
userQuery
);
if
(
ObjectUtils
.
isEmpty
(
sysUserInfo
))
{
throw
new
ServiceException
(
"该用户不存在,请先注册"
);
throw
new
ServiceException
(
MsgConstants
.
needSign
);
}
String
key
=
RedisConstants
.
MSG_KEY
+
type
+
":"
+
phone
;
Object
code
=
RedisUtil
.
get
(
key
);
if
(
ObjectUtils
.
isEmpty
(
code
))
{
throw
new
ServiceException
(
"验证码无效,请重新发送"
);
throw
new
ServiceException
(
MsgConstants
.
codeNone
);
}
if
(!
code
.
equals
(
updateVo
.
getCode
()))
{
throw
new
ServiceException
(
"验证码错误"
);
throw
new
ServiceException
(
MsgConstants
.
codeError
);
}
String
newPassword
=
DigestUtils
.
md5DigestAsHex
(
...
...
src/main/java/com/lyy/user/util/IpUtils.java
0 → 100644
View file @
1e8587ab
package
com
.
lyy
.
user
.
util
;
import
org.apache.commons.lang3.ObjectUtils
;
import
javax.servlet.http.HttpServletRequest
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
/**
* 获取IP方法
*
* @author ruoyi
*/
public
class
IpUtils
{
public
static
String
getIpAddr
(
HttpServletRequest
request
)
{
if
(
request
==
null
)
{
return
null
;
}
String
ip
=
null
;
// X-Forwarded-For:Squid 服务代理
String
ipAddresses
=
request
.
getHeader
(
"X-Forwarded-For"
);
if
(
ipAddresses
==
null
||
ipAddresses
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddresses
))
{
// Proxy-Client-IP:apache 服务代理
ipAddresses
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
if
(
ipAddresses
==
null
||
ipAddresses
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddresses
))
{
// WL-Proxy-Client-IP:weblogic 服务代理
ipAddresses
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
if
(
ipAddresses
==
null
||
ipAddresses
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddresses
))
{
// HTTP_CLIENT_IP:有些代理服务器
ipAddresses
=
request
.
getHeader
(
"HTTP_CLIENT_IP"
);
}
if
(
ipAddresses
==
null
||
ipAddresses
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddresses
))
{
// X-Real-IP:nginx服务代理
ipAddresses
=
request
.
getHeader
(
"X-Real-IP"
);
}
// 有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
if
(
ipAddresses
!=
null
&&
ipAddresses
.
length
()
!=
0
)
{
ip
=
ipAddresses
.
split
(
","
)[
0
];
}
// 还是不能获取到,最后再通过request.getRemoteAddr();获取
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddresses
))
{
ip
=
request
.
getRemoteAddr
();
}
return
ip
.
equals
(
"0:0:0:0:0:0:0:1"
)
?
"127.0.0.1"
:
ip
;
}
public
static
boolean
internalIp
(
String
ip
)
{
byte
[]
addr
=
textToNumericFormatV4
(
ip
);
return
internalIp
(
addr
)
||
"127.0.0.1"
.
equals
(
ip
);
}
private
static
boolean
internalIp
(
byte
[]
addr
)
{
if
(
ObjectUtils
.
isEmpty
(
addr
)
||
addr
.
length
<
2
)
{
return
true
;
}
final
byte
b0
=
addr
[
0
];
final
byte
b1
=
addr
[
1
];
// 10.x.x.x/8
final
byte
SECTION_1
=
0x0A
;
// 172.16.x.x/12
final
byte
SECTION_2
=
(
byte
)
0xAC
;
final
byte
SECTION_3
=
(
byte
)
0x10
;
final
byte
SECTION_4
=
(
byte
)
0x1F
;
// 192.168.x.x/16
final
byte
SECTION_5
=
(
byte
)
0xC0
;
final
byte
SECTION_6
=
(
byte
)
0xA8
;
switch
(
b0
)
{
case
SECTION_1:
return
true
;
case
SECTION_2:
if
(
b1
>=
SECTION_3
&&
b1
<=
SECTION_4
)
{
return
true
;
}
case
SECTION_5:
switch
(
b1
)
{
case
SECTION_6:
return
true
;
}
default
:
return
false
;
}
}
/**
* 将IPv4地址转换成字节
*
* @param text IPv4地址
* @return byte 字节
*/
public
static
byte
[]
textToNumericFormatV4
(
String
text
)
{
if
(
text
.
length
()
==
0
)
{
return
null
;
}
byte
[]
bytes
=
new
byte
[
4
];
String
[]
elements
=
text
.
split
(
"\\."
,
-
1
);
try
{
long
l
;
int
i
;
switch
(
elements
.
length
)
{
case
1
:
l
=
Long
.
parseLong
(
elements
[
0
]);
if
((
l
<
0L
)
||
(
l
>
4294967295L
))
{
return
null
;
}
bytes
[
0
]
=
(
byte
)
(
int
)
(
l
>>
24
&
0xFF
);
bytes
[
1
]
=
(
byte
)
(
int
)
((
l
&
0xFFFFFF
)
>>
16
&
0xFF
);
bytes
[
2
]
=
(
byte
)
(
int
)
((
l
&
0xFFFF
)
>>
8
&
0xFF
);
bytes
[
3
]
=
(
byte
)
(
int
)
(
l
&
0xFF
);
break
;
case
2
:
l
=
Integer
.
parseInt
(
elements
[
0
]);
if
((
l
<
0L
)
||
(
l
>
255L
))
{
return
null
;
}
bytes
[
0
]
=
(
byte
)
(
int
)
(
l
&
0xFF
);
l
=
Integer
.
parseInt
(
elements
[
1
]);
if
((
l
<
0L
)
||
(
l
>
16777215L
))
{
return
null
;
}
bytes
[
1
]
=
(
byte
)
(
int
)
(
l
>>
16
&
0xFF
);
bytes
[
2
]
=
(
byte
)
(
int
)
((
l
&
0xFFFF
)
>>
8
&
0xFF
);
bytes
[
3
]
=
(
byte
)
(
int
)
(
l
&
0xFF
);
break
;
case
3
:
for
(
i
=
0
;
i
<
2
;
++
i
)
{
l
=
Integer
.
parseInt
(
elements
[
i
]);
if
((
l
<
0L
)
||
(
l
>
255L
))
{
return
null
;
}
bytes
[
i
]
=
(
byte
)
(
int
)
(
l
&
0xFF
);
}
l
=
Integer
.
parseInt
(
elements
[
2
]);
if
((
l
<
0L
)
||
(
l
>
65535L
))
{
return
null
;
}
bytes
[
2
]
=
(
byte
)
(
int
)
(
l
>>
8
&
0xFF
);
bytes
[
3
]
=
(
byte
)
(
int
)
(
l
&
0xFF
);
break
;
case
4
:
for
(
i
=
0
;
i
<
4
;
++
i
)
{
l
=
Integer
.
parseInt
(
elements
[
i
]);
if
((
l
<
0L
)
||
(
l
>
255L
))
{
return
null
;
}
bytes
[
i
]
=
(
byte
)
(
int
)
(
l
&
0xFF
);
}
break
;
default
:
return
null
;
}
}
catch
(
NumberFormatException
e
)
{
return
null
;
}
return
bytes
;
}
public
static
String
getHostIp
()
{
try
{
return
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
UnknownHostException
e
)
{
}
return
"127.0.0.1"
;
}
public
static
String
getHostName
()
{
try
{
return
InetAddress
.
getLocalHost
().
getHostName
();
}
catch
(
UnknownHostException
e
)
{
}
return
"未知"
;
}
}
src/main/java/com/lyy/user/util/redis/RedisUtil.java
View file @
1e8587ab
package
com
.
lyy
.
user
.
util
.
redis
;
import
cn.hutool.core.date.DateTime
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.extra.spring.SpringUtil
;
import
com.lyy.user.config.exception.ServiceException
;
import
com.lyy.user.constant.RedisConstants
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
java.util.Collection
;
...
...
@@ -327,4 +331,25 @@ public class RedisUtil {
public
static
boolean
addZset
(
String
key
,
Object
fileds
,
double
end
){
return
Boolean
.
TRUE
.
equals
(
redisTemplate
.
opsForZSet
().
add
(
key
,
fileds
,
end
));
}
//-------------流量控制-----------------
/**
* @description: 访问控制
* @date: 2023/12/14 14:30
* @param: [key, intervalTime 时间间隔(秒), max 最大访问次数]
* @return: void
**/
public
static
boolean
rateLimit
(
String
key
,
int
intervalTime
,
int
max
)
{
DateTime
now
=
DateUtil
.
date
();
DateTime
startTime
=
DateUtil
.
offsetSecond
(
now
,
intervalTime
*
-
1
);
delZset
(
key
,
0
,
startTime
.
getTime
());
Long
count
=
countZset
(
key
,
startTime
.
getTime
(),
now
.
getTime
());
if
(
count
==
null
||
count
<=
max
)
{
return
addZset
(
key
,
now
.
getTime
(),
now
.
getTime
());
}
else
{
return
false
;
}
}
}
src/main/resources/application.yml
View file @
1e8587ab
...
...
@@ -46,8 +46,8 @@ sms:
signature
:
立业云
#短信每日发送次数
limit
:
10
#短信时间间隔(每天限制次数) 单位
天
intervalTime
:
1
#短信时间间隔(每天限制次数) 单位
秒
intervalTime
:
86400
#RSA 前后端交互需要的私钥
sign
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment