Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
xiaolang
/
expert
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7d37085b
authored
Mar 24, 2020
by
xiaolang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
短信验证码
parent
72f87dc7
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
321 additions
and
27 deletions
+321
-27
pom.xml
+19
-0
src/main/java/com/asset/common/Common.java
+30
-23
src/main/java/com/asset/common/Sms.java
+45
-0
src/main/java/com/asset/controller/UserController.java
+58
-0
src/main/java/com/asset/controller/system/CaptchaController.java
+1
-1
src/main/java/com/asset/controller/system/SmsController.java
+87
-0
src/main/java/com/asset/domain/system/Code.java
+24
-0
src/main/java/com/asset/enums/CodeEnum.java
+7
-3
src/main/java/com/asset/mapper/system/CodeMapper.java
+9
-0
src/main/java/com/asset/service/impl/system/CodeServiceImpl.java
+19
-0
src/main/java/com/asset/service/system/CodeService.java
+12
-0
src/main/resources/application.properties
+2
-0
src/main/resources/mybatis/mapper/CodeMapper.xml
+8
-0
No files found.
pom.xml
View file @
7d37085b
...
@@ -108,6 +108,25 @@
...
@@ -108,6 +108,25 @@
<artifactId>
easy-captcha
</artifactId>
<artifactId>
easy-captcha
</artifactId>
<version>
1.6.2
</version>
<version>
1.6.2
</version>
</dependency>
</dependency>
<!-- 短信https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.5.12
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
<groupId>
commons-httpclient
</groupId>
<artifactId>
commons-httpclient
</artifactId>
<version>
3.1
</version>
</dependency>
<!-- 替换模板里的变量https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>
org.freemarker
</groupId>
<artifactId>
freemarker
</artifactId>
<version>
2.3.30
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
src/main/java/com/asset/common/Common.java
View file @
7d37085b
package
com
.
asset
.
common
;
package
com
.
asset
.
common
;
import
freemarker.cache.StringTemplateLoader
;
import
freemarker.template.Configuration
;
import
freemarker.template.Template
;
import
freemarker.template.TemplateException
;
import
java.io.IOException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.StringWriter
;
import
java.net.InetAddress
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
import
java.net.UnknownHostException
;
import
java.text.ParseException
;
import
java.text.ParseException
;
...
@@ -87,29 +93,6 @@ public class Common {
...
@@ -87,29 +93,6 @@ public class Common {
}
}
/**
/**
* 发送短信
* 成功返回TRUE,失败返回错误码
* @param mobile 手机号码 多手机号用逗号分隔如
* @param msg 短信内容
* @return mixed
*/
public
static
boolean
send_msg
(
String
mobile
,
String
msg
)
{
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
map
.
put
(
"account"
,
"tbnn30"
);
map
.
put
(
"pswd"
,
"JT2k8yx8"
);
map
.
put
(
"mobile"
,
mobile
);
map
.
put
(
"msg"
,
msg
);
String
data
=
http_build_query
(
map
);
//$return = file_get_contents("http://send.18sms.com/msg/HttpBatchSendSM?"+data);
//$status = explode(',', $return)[1];
//return $status == 0 ? true : $status;
return
true
;
}
/**
* Java实现PHP中的http_build_query()效果
* Java实现PHP中的http_build_query()效果
* @param array
* @param array
* key=value形式的二位数组
* key=value形式的二位数组
...
@@ -162,4 +145,28 @@ public class Common {
...
@@ -162,4 +145,28 @@ public class Common {
return
ip
;
return
ip
;
}
}
/**
* @param input 替换的数据流
* @templateStr 替换的模板
* */
public
static
String
freemarkerProcess
(
Map
input
,
String
templateStr
)
{
StringTemplateLoader
stringLoader
=
new
StringTemplateLoader
();
String
template
=
"content"
;
stringLoader
.
putTemplate
(
template
,
templateStr
);
Configuration
cfg
=
new
Configuration
();
cfg
.
setTemplateLoader
(
stringLoader
);
try
{
Template
templateCon
=
cfg
.
getTemplate
(
template
);
StringWriter
writer
=
new
StringWriter
();
templateCon
.
process
(
input
,
writer
);
return
writer
.
toString
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
TemplateException
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
}
}
src/main/java/com/asset/common/Sms.java
0 → 100644
View file @
7d37085b
package
com
.
asset
.
common
;
import
org.apache.commons.httpclient.Header
;
import
org.apache.commons.httpclient.HttpClient
;
import
org.apache.commons.httpclient.NameValuePair
;
import
org.apache.commons.httpclient.methods.PostMethod
;
import
java.io.IOException
;
/*
* @author xiaol
* @date 2020/03/24
*/
public
class
Sms
{
private
static
String
url
=
"http://send.18sms.com/msg/HttpBatchSendSM"
;
private
static
String
account
=
"tbnn30"
;
private
static
String
pswd
=
"Zgec1234"
;
public
static
String
sendSms
(
String
phone
,
String
msg
)
throws
IOException
{
HttpClient
client
=
new
HttpClient
();
PostMethod
post
=
new
PostMethod
(
url
);
post
.
addRequestHeader
(
"Content-Type"
,
"application/x-www-form-urlencoded;charset=UTF-8"
);
NameValuePair
[]
data
=
{
new
NameValuePair
(
"account"
,
account
),
new
NameValuePair
(
"pswd"
,
pswd
),
new
NameValuePair
(
"mobile"
,
phone
),
new
NameValuePair
(
"msg"
,
msg
)
};
post
.
setRequestBody
(
data
);
client
.
executeMethod
(
post
);
Header
[]
headers
=
post
.
getResponseHeaders
();
int
statusCode
=
post
.
getStatusCode
();
System
.
out
.
println
(
"statusCode:"
+
statusCode
);
for
(
Header
h
:
headers
)
{
System
.
out
.
println
(
h
.
toString
());
}
String
result
=
new
String
(
post
.
getResponseBodyAsString
().
getBytes
(
"UTF-8"
));
System
.
out
.
println
(
result
);
post
.
releaseConnection
();
String
[]
arr
=
result
.
split
(
","
);
return
arr
[
1
];
}
}
src/main/java/com/asset/controller/UserController.java
View file @
7d37085b
...
@@ -6,11 +6,13 @@ import com.asset.domain.Result;
...
@@ -6,11 +6,13 @@ import com.asset.domain.Result;
import
com.asset.domain.User
;
import
com.asset.domain.User
;
import
com.asset.domain.system.Captcha
;
import
com.asset.domain.system.Captcha
;
import
com.asset.domain.system.Certificate
;
import
com.asset.domain.system.Certificate
;
import
com.asset.domain.system.Code
;
import
com.asset.domain.system.LoginLog
;
import
com.asset.domain.system.LoginLog
;
import
com.asset.enums.CodeEnum
;
import
com.asset.enums.CodeEnum
;
import
com.asset.service.UserService
;
import
com.asset.service.UserService
;
import
com.asset.service.system.CaptchaService
;
import
com.asset.service.system.CaptchaService
;
import
com.asset.service.system.CertificateService
;
import
com.asset.service.system.CertificateService
;
import
com.asset.service.system.CodeService
;
import
com.asset.service.system.LoginLogService
;
import
com.asset.service.system.LoginLogService
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -48,6 +50,9 @@ public class UserController {
...
@@ -48,6 +50,9 @@ public class UserController {
@Autowired
@Autowired
CaptchaService
captchaService
;
CaptchaService
captchaService
;
@Autowired
CodeService
codeService
;
@RequestMapping
(
"/register"
)
@RequestMapping
(
"/register"
)
public
Result
register
(
@Validated
(
value
={
Insert
.
class
})
@RequestBody
User
user
,
BindingResult
result
)
throws
UnsupportedEncodingException
{
public
Result
register
(
@Validated
(
value
={
Insert
.
class
})
@RequestBody
User
user
,
BindingResult
result
)
throws
UnsupportedEncodingException
{
if
(
result
.
hasErrors
())
{
if
(
result
.
hasErrors
())
{
...
@@ -297,4 +302,57 @@ public class UserController {
...
@@ -297,4 +302,57 @@ public class UserController {
return
ResultUtil
.
success
(
CodeEnum
.
SUCCESS
);
return
ResultUtil
.
success
(
CodeEnum
.
SUCCESS
);
}
}
@GetMapping
(
"/checkSmsChangePassword"
)
public
Result
checkSmsChangePassword
(
String
phone
,
String
verifyCode
)
throws
ParseException
{
//先判断账号是否存在
QueryWrapper
<
User
>
queryWrapper
=
new
QueryWrapper
<>();
queryWrapper
.
eq
(
"mobile"
,
phone
).
or
().
eq
(
"email"
,
phone
);
User
user
=
userService
.
getOne
(
queryWrapper
);
if
(
user
==
null
){
return
ResultUtil
.
success
(
CodeEnum
.
ISEXITE_NOT_ERROR
);
}
else
{
QueryWrapper
<
Code
>
CodeWrapper
=
new
QueryWrapper
<>();
CodeWrapper
.
eq
(
"code"
,
verifyCode
);
Code
co
=
codeService
.
getOne
(
CodeWrapper
);
if
(
co
==
null
){
return
ResultUtil
.
success
(
CodeEnum
.
ISEXITE_SMS_CODE
);
}
else
{
//比较看验证码是否过期
if
(
Common
.
getCompareDate
(
co
.
getExpire
())){
return
ResultUtil
.
success
(
CodeEnum
.
CODE_SMS_EXpired
);
}
else
{
//没过期判断对错
if
(
co
==
null
||
!
co
.
getCode
().
equals
(
verifyCode
)){
return
ResultUtil
.
success
(
CodeEnum
.
CODE_SMS_ERROR
);
}
}
}
}
return
ResultUtil
.
success
(
CodeEnum
.
SUCCESS
);
}
@GetMapping
(
"/changePassword"
)
public
Result
changePassword
(
String
phone
,
String
psw
,
String
confirmpwd
)
throws
UnsupportedEncodingException
{
//先判断账号是否存在
QueryWrapper
<
User
>
queryWrapper
=
new
QueryWrapper
<>();
queryWrapper
.
eq
(
"mobile"
,
phone
).
or
().
eq
(
"email"
,
phone
);
User
user
=
userService
.
getOne
(
queryWrapper
);
if
(
user
==
null
){
return
ResultUtil
.
success
(
CodeEnum
.
ISEXITE_NOT_ERROR
);
}
else
{
//判断2次密码是否一样
if
(
psw
.
equals
(
confirmpwd
)){
String
pass
=
MD5Util
.
getEncryption
(
psw
);
User
user1
=
new
User
();
user1
.
setPassword
(
pass
);
QueryWrapper
<
User
>
updateWrapper
=
new
QueryWrapper
<>();
updateWrapper
.
eq
(
"mobile"
,
phone
).
or
().
eq
(
"email"
,
phone
);
if
(!
userService
.
update
(
user1
,
updateWrapper
)){
return
ResultUtil
.
success
(
CodeEnum
.
ERROR
);
}
}
else
{
return
ResultUtil
.
success
(
CodeEnum
.
PWD_DIFF_ERROR
);
}
}
return
ResultUtil
.
success
(
CodeEnum
.
SUCCESS
);
}
}
}
src/main/java/com/asset/controller/CaptchaController.java
→
src/main/java/com/asset/controller/
system/
CaptchaController.java
View file @
7d37085b
package
com
.
asset
.
controller
;
package
com
.
asset
.
controller
.
system
;
import
com.asset.common.Common
;
import
com.asset.common.Common
;
import
com.asset.common.ResultUtil
;
import
com.asset.common.ResultUtil
;
...
...
src/main/java/com/asset/controller/system/SmsController.java
0 → 100644
View file @
7d37085b
package
com
.
asset
.
controller
.
system
;
import
com.asset.common.Common
;
import
com.asset.common.ResultUtil
;
import
com.asset.common.Sms
;
import
com.asset.domain.Result
;
import
com.asset.domain.system.Code
;
import
com.asset.enums.CodeEnum
;
import
com.asset.service.system.CodeService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.io.*
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Random
;
/*
* @author xiaol
* @date 2020/3/23
*/
@RestController
@RequestMapping
(
value
=
"/sms"
)
public
class
SmsController
{
@Autowired
CodeService
codeService
;
//消息模板
private
static
String
msg
=
"您正在申请修改中国e车会员密码,验证码是:${verifyCode},请勿将验证码告知他人!"
;
@GetMapping
(
"/sendSmsChangePassword"
)
public
Result
sendSmsChangePassword
(
String
phone
)
throws
IOException
{
String
verifyCode
=
String
.
valueOf
(
new
Random
().
nextInt
(
899999
)
+
100000
);
Map
m
=
new
HashMap
();
m
.
put
(
"verifyCode"
,
verifyCode
);
String
passwordMsg
=
Common
.
freemarkerProcess
(
m
,
msg
);
String
re
=
Sms
.
sendSms
(
phone
,
passwordMsg
);
if
(
Integer
.
parseInt
(
re
)
==
0
){
String
ex
=
Common
.
getDateToString
(
Common
.
getTime
()
+
2
*
60
*
1000
);
//发送成功短信验证码入库
Code
co
=
new
Code
();
co
.
setPhone
(
phone
);
co
.
setCode
(
verifyCode
);
co
.
setExpire
(
ex
);
codeService
.
save
(
co
);
return
ResultUtil
.
success
(
CodeEnum
.
SUCCESS
);
}
else
{
return
ResultUtil
.
success
(
CodeEnum
.
ERROR
);
}
}
/*
private static String url = "http://send.18sms.com/msg/HttpBatchSendSM";
private static String account = "tbnn30";
private static String pswd = "Zgec1234";
private static String phone = "18814820090";
@GetMapping("/sendSms")
public String sendSms() throws IOException {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://send.18sms.com/msg/HttpBatchSendSM");
post.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");// 在头文件中设置转码
NameValuePair[] data = { new NameValuePair("account", account), // 注册的用户名
new NameValuePair("pswd", pswd), // 注册成功后,登录网站使用的密钥
new NameValuePair("mobile", phone), // 手机号码
new NameValuePair("msg", msg) };
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println("statusCode:" + statusCode);
for (Header h : headers) {
System.out.println(h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes(
"UTF-8"));
System.out.println(result);
post.releaseConnection();
return result;
}
*/
}
src/main/java/com/asset/domain/system/Code.java
0 → 100644
View file @
7d37085b
package
com
.
asset
.
domain
.
system
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
lombok.Data
;
import
lombok.EqualsAndHashCode
;
import
lombok.ToString
;
import
lombok.experimental.Accessors
;
@Data
@EqualsAndHashCode
(
callSuper
=
false
)
@Accessors
(
chain
=
true
)
@TableName
(
"ex_verifycode"
)
@ToString
public
class
Code
{
private
Integer
id
;
private
String
phone
;
private
String
code
;
private
String
expire
;
}
src/main/java/com/asset/enums/CodeEnum.java
View file @
7d37085b
...
@@ -19,9 +19,13 @@ public enum CodeEnum {
...
@@ -19,9 +19,13 @@ public enum CodeEnum {
LOGINOUT_SUCCESS
(
20004
,
"成功!"
),
LOGINOUT_SUCCESS
(
20004
,
"成功!"
),
ISEXITE_MOBILE
(
20006
,
"手机号已经存在!"
),
ISEXITE_MOBILE
(
20006
,
"手机号已经存在!"
),
FORMAT_MOBILE
(
20007
,
"手机号格式不正确!"
),
FORMAT_MOBILE
(
20007
,
"手机号格式不正确!"
),
CODE_ERROR
(
20008
,
"验证码错误!"
),
CODE_ERROR
(
20008
,
"图形验证码错误!"
),
ISEXITE_CODE
(
20010
,
"验证码不存在!"
),
ISEXITE_CODE
(
20010
,
"图形验证码不存在!"
),
CODE_EXpired
(
20009
,
"验证码过期!"
)
CODE_EXpired
(
20009
,
"图形验证码过期!"
),
ISEXITE_SMS_CODE
(
20011
,
"短信验证码不存在!"
),
CODE_SMS_EXpired
(
20012
,
"短信验证码过期!"
),
CODE_SMS_ERROR
(
20013
,
"短信验证码错误!"
),
PWD_DIFF_ERROR
(
20014
,
"二次密码不一样!"
)
;
;
...
...
src/main/java/com/asset/mapper/system/CodeMapper.java
0 → 100644
View file @
7d37085b
package
com
.
asset
.
mapper
.
system
;
import
com.asset.domain.system.Code
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
public
interface
CodeMapper
extends
BaseMapper
<
Code
>
{
}
src/main/java/com/asset/service/impl/system/CodeServiceImpl.java
0 → 100644
View file @
7d37085b
package
com
.
asset
.
service
.
impl
.
system
;
import
com.asset.domain.system.Code
;
import
com.asset.mapper.system.CodeMapper
;
import
com.asset.service.system.CodeService
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
/*
* @author xiaol
* @date 2020/03/23
*/
@Service
@Transactional
public
class
CodeServiceImpl
extends
ServiceImpl
<
CodeMapper
,
Code
>
implements
CodeService
{
}
src/main/java/com/asset/service/system/CodeService.java
0 → 100644
View file @
7d37085b
package
com
.
asset
.
service
.
system
;
import
com.asset.domain.system.Code
;
import
com.baomidou.mybatisplus.extension.service.IService
;
/*
* @author xiaol
* @date 2020/03/23
*/
public
interface
CodeService
extends
IService
<
Code
>
{
}
src/main/resources/application.properties
View file @
7d37085b
...
@@ -6,3 +6,5 @@ filePath=D:/phpStudy/WWW/images/
...
@@ -6,3 +6,5 @@ filePath=D:/phpStudy/WWW/images/
#图片访问域名
#图片访问域名
tempContextUrl
=
http://localhost:8002/
tempContextUrl
=
http://localhost:8002/
src/main/resources/mybatis/mapper/CodeMapper.xml
0 → 100644
View file @
7d37085b
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace=
"com.asset.mapper.system.CodeMapper"
>
</mapper>
\ No newline at end of file
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