Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
A
Afrishop refactored project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Whispa
Afrishop refactored project
Commits
169167f4
提交
169167f4
authored
10月 31, 2020
作者:
Whispa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commit commit
上级
592edc89
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
156 行增加
和
4 行删除
+156
-4
pom.xml
pom.xml
+6
-0
UserController.java
...a/com/example/afrishop_v3/controllers/UserController.java
+33
-4
EmailHelper.java
src/main/java/com/example/afrishop_v3/util/EmailHelper.java
+77
-0
EmailTemplateBo.java
...in/java/com/example/afrishop_v3/util/EmailTemplateBo.java
+27
-0
EmailTemplateConstant.java
...a/com/example/afrishop_v3/util/EmailTemplateConstant.java
+13
-0
没有找到文件。
pom.xml
浏览文件 @
169167f4
...
@@ -44,6 +44,12 @@
...
@@ -44,6 +44,12 @@
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-jdbc
</artifactId>
<artifactId>
spring-boot-starter-data-jdbc
</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-freemarker
</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-jpa
</artifactId>
<artifactId>
spring-boot-starter-data-jpa
</artifactId>
...
...
src/main/java/com/example/afrishop_v3/controllers/UserController.java
浏览文件 @
169167f4
...
@@ -6,13 +6,15 @@ import com.example.afrishop_v3.models.ResetBody;
...
@@ -6,13 +6,15 @@ import com.example.afrishop_v3.models.ResetBody;
import
com.example.afrishop_v3.models.TbCfUserInfo
;
import
com.example.afrishop_v3.models.TbCfUserInfo
;
import
com.example.afrishop_v3.repository.UserRepository
;
import
com.example.afrishop_v3.repository.UserRepository
;
import
com.example.afrishop_v3.security.services.AuthenticationUser
;
import
com.example.afrishop_v3.security.services.AuthenticationUser
;
import
com.example.afrishop_v3.util.
SMSUtil
;
import
com.example.afrishop_v3.util.
*
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.http.Consts
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
java.net.URLDecoder
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.Optional
;
import
java.util.Optional
;
...
@@ -22,12 +24,14 @@ public class UserController extends Controller {
...
@@ -22,12 +24,14 @@ public class UserController extends Controller {
private
final
UserRepository
repository
;
private
final
UserRepository
repository
;
private
final
AuthenticationUser
user
;
private
final
AuthenticationUser
user
;
private
final
PasswordEncoder
encoder
;
private
final
PasswordEncoder
encoder
;
private
final
EmailHelper
emailHelper
;
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
UserController
.
class
);
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
UserController
.
class
);
public
UserController
(
UserRepository
repository
,
AuthenticationUser
user
,
PasswordEncoder
encoder
)
{
public
UserController
(
UserRepository
repository
,
AuthenticationUser
user
,
PasswordEncoder
encoder
,
EmailHelper
emailHelper
)
{
this
.
repository
=
repository
;
this
.
repository
=
repository
;
this
.
user
=
user
;
this
.
user
=
user
;
this
.
encoder
=
encoder
;
this
.
encoder
=
encoder
;
this
.
emailHelper
=
emailHelper
;
}
}
@PutMapping
(
"bindPhoneOrEmail"
)
@PutMapping
(
"bindPhoneOrEmail"
)
...
@@ -36,9 +40,16 @@ public class UserController extends Controller {
...
@@ -36,9 +40,16 @@ public class UserController extends Controller {
if
(
StringUtils
.
isNotBlank
(
email
)
){
if
(
StringUtils
.
isNotBlank
(
email
)
){
if
(
isEmailValid
(
email
)
)
user
.
setEmail
(
email
);
boolean
detect
=
StringUtils
.
isNotBlank
(
code
)
&&
code
.
equals
(
user
.
getVerificationCode
());
if
(
isEmailValid
(
email
)
&&
detect
){
else
return
new
Result
(
ResultCodeEnum
.
ILLEGAL_ARGUMENT
.
getCode
(),
"Email address is not valid"
);
if
(
user
.
getEmail
()
!=
null
&&
user
.
getEmail
().
equals
(
user
.
getFirebaseUid
())){
user
.
setFirebaseUid
(
email
);
}
user
.
setEmail
(
email
);
}
else
return
new
Result
(
ResultCodeEnum
.
ILLEGAL_ARGUMENT
.
getCode
(),
detect
?
"Email address is not valid"
:
"Code verification error"
);
}
}
...
@@ -58,6 +69,24 @@ public class UserController extends Controller {
...
@@ -58,6 +69,24 @@ public class UserController extends Controller {
}
}
@GetMapping
(
"/getEmailCode"
)
public
Result
getEmailCode
(
@RequestParam
(
"email"
)
String
email
){
try
{
EmailTemplateBo
emailTemplateBo
=
new
EmailTemplateBo
();
String
name
=
URLDecoder
.
decode
(
email
,
Consts
.
UTF_8
.
name
());
Integer
randomCode
=
RandomCodeHelper
.
producedRandomCode
(
6
);
emailTemplateBo
.
setNick
(
name
);
emailTemplateBo
.
setIdentifyCode
(
randomCode
);
Integer
identifyCode
=
emailHelper
.
sendIdentifyEmail
(
email
,
EmailTemplateConstant
.
REGISTER
,
emailTemplateBo
);
TbCfUserInfo
user
=
this
.
user
.
user
();
user
.
setVerificationCode
(
String
.
valueOf
(
identifyCode
));
repository
.
save
(
user
);
return
new
Result
(
"Verification code has been sent"
);
}
catch
(
Exception
e
){
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
e
.
getMessage
());
}
}
@GetMapping
(
"/identifyCode/{phone}"
)
@GetMapping
(
"/identifyCode/{phone}"
)
public
Result
getUserPhoneCode
(
@PathVariable
(
"phone"
)
String
phone
)
{
public
Result
getUserPhoneCode
(
@PathVariable
(
"phone"
)
String
phone
)
{
...
...
src/main/java/com/example/afrishop_v3/util/EmailHelper.java
0 → 100644
浏览文件 @
169167f4
package
com
.
example
.
afrishop_v3
.
util
;
import
com.example.afrishop_v3.config.DomainProperties
;
import
freemarker.template.Configuration
;
import
freemarker.template.Template
;
import
freemarker.template.TemplateException
;
import
org.apache.commons.mail.EmailException
;
import
org.apache.commons.mail.HtmlEmail
;
import
org.apache.http.Consts
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.ui.freemarker.FreeMarkerTemplateUtils
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
@Component
(
"emailHelper"
)
public
class
EmailHelper
{
private
final
Configuration
configuration
;
private
final
DomainProperties
domainProperties
;
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
EmailHelper
.
class
);
public
EmailHelper
(
Configuration
configuration
,
DomainProperties
domainProperties
)
{
this
.
configuration
=
configuration
;
this
.
domainProperties
=
domainProperties
;
}
public
Integer
sendIdentifyEmail
(
String
toEmail
,
String
templateName
,
EmailTemplateBo
emailTemplateBo
)
throws
IOException
,
TemplateException
,
EmailException
{
/*Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
// 初始化一些配置
cfg.setDirectoryForTemplateLoading(new File("/"));
cfg.setDefaultEncoding(Consts.UTF_8.name());
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);*/
Template
t
=
configuration
.
getTemplate
(
templateName
);
Map
<
String
,
Object
>
model
=
new
HashMap
<>();
model
.
put
(
"identifyCode"
,
emailTemplateBo
.
getIdentifyCode
());
model
.
put
(
"nick"
,
emailTemplateBo
.
getNick
());
String
html
=
FreeMarkerTemplateUtils
.
processTemplateIntoString
(
t
,
model
);
HtmlEmail
email
=
new
HtmlEmail
();
//创建一个HtmlEmail实例对象
email
.
setHostName
(
domainProperties
.
getProperty
(
"email.hostName"
));
//邮箱的SMTP服务器,一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com
email
.
setSslSmtpPort
(
"465"
);
email
.
setSSLOnConnect
(
true
);
email
.
setCharset
(
Consts
.
UTF_8
.
name
());
//设置发送的字符类型
//email.addTo("zhengfanguang@163.com");//设置收件人
email
.
addTo
(
toEmail
);
//设置收件人
email
.
setFrom
(
domainProperties
.
getProperty
(
"email.from.email"
),
domainProperties
.
getProperty
(
"email.from.name"
));
//发送人的邮箱为自己的,用户名可以随便填
email
.
setAuthentication
(
domainProperties
.
getProperty
(
"email.authentication.userName"
),
domainProperties
.
getProperty
(
"email.authentication.password"
));
//设置发送人的邮箱和用户名和授权码(授权码是自己设置的)
email
.
setSubject
(
domainProperties
.
getProperty
(
"email.subject"
));
//设置发送主题
email
.
setMsg
(
html
);
//设置发送内容
String
res
=
email
.
send
();
//logger.info("邮件发送结果:"+res);
return
emailTemplateBo
.
getIdentifyCode
();
}
public
void
sendEmail
(
String
email
,
String
templateName
)
throws
IOException
,
TemplateException
,
EmailException
{
Template
t
=
configuration
.
getTemplate
(
templateName
);
Map
<
String
[],
Object
>
model
=
new
HashMap
<>();
String
html
=
FreeMarkerTemplateUtils
.
processTemplateIntoString
(
t
,
model
);
HtmlEmail
emails
=
new
HtmlEmail
();
//创建一个HtmlEmail实例对象
emails
.
setHostName
(
domainProperties
.
getProperty
(
"email.hostName"
));
//邮箱的SMTP服务器,一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com
emails
.
setCharset
(
Consts
.
UTF_8
.
name
());
//设置发送的字符类型
//email.addTo("zhengfanguang@163.com");//设置收件人
emails
.
addTo
(
email
);
//设置收件人
emails
.
setFrom
(
domainProperties
.
getProperty
(
"email.from.email"
),
domainProperties
.
getProperty
(
"email.from.name"
));
//发送人的邮箱为自己的,用户名可以随便填
emails
.
setAuthentication
(
domainProperties
.
getProperty
(
"email.authentication.userName"
),
domainProperties
.
getProperty
(
"email.authentication.password"
));
//设置发送人的邮箱和用户名和授权码(授权码是自己设置的)
emails
.
setSubject
(
domainProperties
.
getProperty
(
"email.project"
));
//设置发送主题
emails
.
setMsg
(
html
);
//设置发送内容
String
res
=
emails
.
send
();
if
(
res
!=
null
){
// tbCfUserInfoService.updateIsSend(email);
}
logger
.
info
(
"邮件发送结果:"
+
res
);
}
}
src/main/java/com/example/afrishop_v3/util/EmailTemplateBo.java
0 → 100644
浏览文件 @
169167f4
package
com
.
example
.
afrishop_v3
.
util
;
/**
* templates/email-template.ftl 对应的参数
*/
public
class
EmailTemplateBo
{
//用户名
private
String
nick
;
//验证码
private
Integer
identifyCode
;
public
String
getNick
()
{
return
nick
;
}
public
void
setNick
(
String
nick
)
{
this
.
nick
=
nick
;
}
public
Integer
getIdentifyCode
()
{
return
identifyCode
;
}
public
void
setIdentifyCode
(
Integer
identifyCode
)
{
this
.
identifyCode
=
identifyCode
;
}
}
src/main/java/com/example/afrishop_v3/util/EmailTemplateConstant.java
0 → 100644
浏览文件 @
169167f4
package
com
.
example
.
afrishop_v3
.
util
;
/**
* 发送email所用的模板文件
*/
public
class
EmailTemplateConstant
{
//注册
public
static
String
REGISTER
=
"email-template-a.ftl"
;
//注册
public
static
String
REGISTERS
=
"email-template-a.ftl"
;
//20191227
public
static
String
REGISTER20191227
=
"email-template-a.ftl"
;
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论