-
Notifications
You must be signed in to change notification settings - Fork 145
通过Email邮箱重置用户密码(二十)
在单点登录系统中,少不了修改密码、忘记密码、重置密码、找回密码的功能,哪怕这个过程不是sso来胜任,肯定有一个模块甚至一个系统来做这个密码管理功能,但刚刚好cas自带了这个密码管理功能,非常简单也很好理解
先说一下我们日常的一些重置密码做法:
-
输入用户名/邮箱/手机号(其实就是确定到一个用户)
-
发送邮件/验证码进行验证
-
邮箱验证(打开链接)/手机验证(输入验证码)
-
加强验证回答问题(可选)
-
输入新密码,重置
但像qq那样,找回密码功能无非是多了一个审核功能,其实一般来说,邮箱/短信验证加上回答问题已经满足日常的需求了
这张不啰嗦那么多,直接来实战~
cas目前已经支持很多常用的特性,那么密码管理也是支持得不错的
-
邮件发送验证
-
短信发送验证
-
问题回答验证
-
忘记密码/重置密码UI
那么我们挑邮件发送验证,问题回答验证来进行简单的罗列一下,好给大家一个简单的介绍,当然了,这些界面是cas提供的,我们在上线的时候的一般需要自定义一套,有文章姐简单介绍了一下自定义主题:Cas 5.2.x版本使用 —— 自定义登录界面 / 自定义主题风格(十二)
上面的流程已经明确出一些东西,其中包括:
-
根据用户名寻找邮箱/手机号
-
问题数据查找
-
邮箱/手机发送配置
-
密码重置策略配置(修改密码接口,加密策略)
<!-- 添加邮箱的支持 --> <dependency> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-support-pm-jdbc</artifactId> <version>${cas.version}</version> </dependency><!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency>
<!--jdbc认证需要添加的,这个是cas的依赖包--> <dependency> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-support-jdbc</artifactId> <version>${cas.version}</version> </dependency>
以qq邮箱为例,开启SMTP服务 https://mail.qq.com/ ,创建授权码
生成授权码,这个授权码是我们配置poi的服务器密码,而不是我们登录的qq的密码。
如果是QQ
#发送邮件
spring.mail.host=smtp.qq.com
spring.mail.port=465
#邮箱用户名(qq账户)
[email protected]
#邮箱授权码(qq授权码)
spring.mail.password=xxxxxx
spring.mail.testConnection=false
spring.mail.properties.mail.smtp.auth=true
#必须ssl
spring.mail.properties.mail.smtp.ssl.enable=true
如果是阿里云
#密码管理开始
cas.authn.pm.enabled=true
#发送邮件
spring.mail.host=smtp.aliyun.com
spring.mail.port=465
#邮箱用户名(阿里邮箱)
[email protected]
#邮箱密码(阿里密码)
spring.mail.password=xxxxxx..
spring.mail.testConnection=false
spring.mail.properties.mail.smtp.auth=true
根据自身的邮箱环境进行配置即可。
完整email相关配置
##
# CAS Authentication Credentials
#
#cas.authn.accept.users=tingfeng::tingfeng
##
# Query Database Authentication 数据库查询校验用户名开始
#
# 查询账号密码sql,必须包含密码字段
cas.authn.jdbc.query[0].sql=select * from sys_user where username=?
cas.authn.jdbc.query[0].fieldPassword=password
cas.authn.jdbc.query[0].fieldExpired=expired
cas.authn.jdbc.query[0].fieldDisabled=disabled
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/cas_sso_record?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.authn.jdbc.query[0].user=casuser
cas.authn.jdbc.query[0].password=casuser
#默认加密策略,通过encodingAlgorithm来指定算法,默认NONE不加密
cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5
#
##密码管理开始
cas.authn.pm.enabled=true
#
##发送邮件
#spring.mail.host=smtp.mxhichina.com
#spring.mail.port=465
#邮箱用户名(公司邮箱)
#spring.mail.username=xxxxxx@wusongtech.com
#邮箱密码
#spring.mail.password=xxxxxx
#spring.mail.testConnection=false
#spring.mail.properties.mail.smtp.auth=true
#发送邮件
spring.mail.host=smtp.qq.com
spring.mail.port=465
spring.mail.username=1056856191@qq.com
#邮箱授权码(qq授权码)
spring.mail.password=xxxxxx
spring.mail.testConnection=false
spring.mail.properties.mail.smtp.auth=true
#必须ssl
spring.mail.properties.mail.smtp.ssl.enable=true
cas.authn.pm.reset.from=${spring.mail.username}
#发送邮件标题
cas.authn.pm.reset.subject=CAS-Server 重置密码
#邮件内容,必须要有%s,因为会生成一个连接并且带了token,否则无法打开链接,当然这个链接也和cas.server.prefix有关系
cas.authn.pm.reset.text=打开以下链接重置您的密码 (来自CAS-Server): %s
#token失效分钟数
cas.authn.pm.reset.expirationMinutes=10
cas.authn.pm.reset.emailAttribute=mail
#新密码必须匹配表达式
cas.authn.pm.policyPattern=\\w{6,24}
#邮箱查找
#根据用户名查找邮箱
cas.authn.pm.jdbc.sqlFindEmail=SELECT email FROM sys_user WHERE username = ?
#修改密码
cas.authn.pm.jdbc.sqlChangePassword=update sys_user set password=? where username=?
cas.authn.pm.jdbc.url=${cas.authn.jdbc.query[0].url}
cas.authn.pm.jdbc.user=${cas.authn.jdbc.query[0].user}
cas.authn.pm.jdbc.password=${cas.authn.jdbc.query[0].password}
cas.authn.pm.jdbc.dialect=${cas.authn.jdbc.query[0].dialect}
cas.authn.pm.jdbc.driverClass=${cas.authn.jdbc.query[0].driverClass}
#密码修改加密规则,这个必须要和原始密码加密规则一致
cas.authn.pm.jdbc.passwordEncoder.type=${cas.authn.jdbc.query[0].passwordEncoder.type}
cas.authn.pm.jdbc.passwordEncoder.characterEncoding=${cas.authn.jdbc.query[0].passwordEncoder.characterEncoding}
cas.authn.pm.jdbc.passwordEncoder.encodingAlgorithm=${cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm}
cas.authn.pm.jdbc.passwordEncoder.secret=${cas.authn.jdbc.query[0].passwordEncoder.secret}
#默认是false,不会提交update语句
cas.authn.pm.jdbc.autocommit=true
#是否开启问题回答
cas.authn.pm.reset.securityQuestionsEnabled=true
#根据用户名查找问题
cas.authn.pm.jdbc.sqlSecurityQuestions=select question, answer from sys_user_question where username=?
注意,最后面两句,如果不需要问题回答(查询问题表),把 securityQuestionsEnabled 设置为false即可。
由于已经发布的代码默认提交是false,导致不能修改密码成功请添加以下配置
cas.authn.pm.jdbc.autocommit=true
另外可能会怀疑为什么会有那些问题,因为在加入了以下数据
INSERT INTO SYS_USER_QUESTION VALUES ('admin', '使用过的密码是?', '123'); INSERT INTO SYS_USER_QUESTION VALUES ('admin', '你的年龄是?', '24'); INSERT INTO SYS_USER_QUESTION VALUES ('zhangsan', '我的名字是?', 'zhangsan'); INSERT INTO SYS_USER_QUESTION VALUES ('zhangsan', '我在哪里工作?', 'guangzhou');
点击重置密码
输入账号
发送成功
接收重置密码链接
浏览器访问链接,要求回答问题,如果将回答问题设置为false,就会直接进入重置页面,省略如下验证操作
验证成功进入重置
重置密码成功
如果是发送到了QQ邮箱,如下图效果
视频演示,关闭了问题验证。
https://v.qq.com/x/page/e06362scktw.html
https://github.com/X-rapido/CAS_SSO_Record
https://apereo.github.io/cas/5.1.x/installation/Configuration-Properties.html#password-management
https://apereo.github.io/cas/5.1.x/installation/Password-Policy-Enforcement.html
https://blog.csdn.net/u010475041/article/details/78249293
择善人而交,择善书而读;择善言而听,择善行而从。
- 首页
- SpringBoot内部运行(一)
- 外部Tomcat运行(二)
- 修改默认的用户名和密码(三)
- 通过yml的方式配置(四)
- 单点登录JDBC认证(五)
- 自定义密码验证(六)
- 单点登录自定义REST认证(七)
- Debug调试源码(八)
- 实现SSO单点登录(九)
- 退出登录后跳转到指定页面(十)
- Service配置介绍(十一)
- 自定义登录界面 / 自定义主题风格(十二)
- 客户端使用iframe嵌套方式实现SSO(十三)
- 代理认证实现SSO(十四)
- 代理认证拓展理解(十五)
- 存储Ticket到redis(十六)
- Restful API 方式验证Ticket(十七)
- Restful API 方式实现SSO(十八)
- 配置 Swagger API 集成(十九)
- 通过邮箱重置用户密码(二十)