最近刚刚参与一个基于Play框架的管理平台的升级工作,其中涉及到了用户的验证工作。第一次接触play框架,直接看已有代码,有点晕。因此,自己实现了一个简单的用户验证功能。

首先,新建一个User类,包含两个属性,包含两个属性email和password。并在构造器中对密码进行了加密。

@Entity
public class User extends Model {
@Id
private String email;
private String password; // Constructor
public User(String email, String password) {
String passwordHash = BCrypt.hashpw(password, BCrypt.gensalt());
this.email = email;
this.password = passwordHash;
}
} 接下来,新增控制器Application.java,其中主要包含包含两个动作和一个表单类Registration。一个动作register()用于显示注册页面,另一个动作postRegister处理表单提交的信息,
并增加相应的数据库记录。Registration则对应注册页面所显示的表格:
public class Application extends Controller {
public static class Registration {
@Email
public String email;
@Required
public String password;
} public static Result register() {
Form<Registration> userForm = Form.form(Registration.class);
return ok(views.html.register.render(userForm));
} public static Result postRegister() {
Form<Registration> userForm =
Form.form(Registration.class).bindFromRequest();
User user = new User(userForm.get().email, userForm.get().password);
user.save();
return ok("registered");
}
} 其后,新增Rigister所对应的前端页面,并在Routes文件中为Appication所对应的动作增加访问路径。
<!DOCTYPE html>
<html>
<body>
<h1> Registration </h1>
@helper.form(action = routes.Application.postRegister()) {
@helper.inputText(userForm("email"))
@helper.inputPassword(userForm("password"))
<input type="submit">
}
</body>
</html> Routes文件:
GET     /register                   controllers.Application.register()
POST /register controllers.Application.postRegister() 其后,访问页面,输入用户名和密码,可以看到数据库中新增了一条记录。
接下来,将用户验证的逻辑加入到User类中,修改User类,新增authenticate()方法。authenticate()接收的是明文密码。上面的验证中,首先检查用户邮箱是否存在。如果存在,则检查密码是否符合数据库的记录。
如果邮箱或者密码错误,将返回null。否则返回正确的用户对象。
// Query
public static Model.Finder<Integer, User> find =
new Model.Finder<>(Integer.class, User.class); // Authentification
public static User authenticate(String email, String password) {
User user = find.where()
.eq("email", email)
.findUnique();
if (user == null) {
return user;
} else if (BCrypt.checkpw(password, user.password)) {
return user;
} else {
return null;
}
}
接下来,进一步修改Application控制器,增加两个动作和一个表单类。动作login()用于显示登录页面,动作postLogin()用于处理登录表单填写的信息,并根据信息决定是否登入用户。Login类对应登录页面的表单。
 public static class Login {
@Email
public String email;
@Required
public String password; public String validate() {
if (User.authenticate(email, password) == null) {
return "Invalid user or password";
}
return null;
}
} public static Result login() {
Form<Login> userForm = Form.form(Login.class);
return ok(views.html.login.render(userForm));
} public static Result postLogin() {
Form<Login> userForm = Form.form(Login.class).bindFromRequest();
if (userForm.hasErrors()) {
return badRequest("Wrong user/password");
} else {
return ok("Valid user");
}
}
其中,在静态类Login中,增加了validate()方法,并在其中调用User的验证逻辑。正如postLogin()中所示,表单的hasErrors()方法将自动检查validate()方法的返回值。如果validate()方法返回为null,
则说明表单无误。postLogin()的if结构,将根据登录是否合法,来返回不同的结果。 最后,同样的在Routes文件中新增两条对应的URL
GET     /login                      controllers.Application.login()
POST /login controllers.Application.postLogin() 其后,访问/login页面,并尝试登录。发现已增加了验证功能。

												

最新文章

  1. 2016 小马哥 IOS
  2. Kali Linux Web 渗透测试视频教程— 第八课 nessus
  3. Linux snmp监控
  4. vs2010工程迁移问题,x64到Win32
  5. ACM1998
  6. (转)Tomcat内存设置
  7. 50道经典的JAVA编程题(26-30)
  8. sublime text3输入中文的问题.
  9. css中表格的table-layout属性特殊用法
  10. Bloom Filter 算法具体解释
  11. [转]Hibernate中property-ref的应用,常用来解决遗留数据库One To Many关系
  12. 记使用aliyun-log-logback-appender 报错no applicable action for [encoder], current ElementPath is [[configuration][appender][encoder]]
  13. Python3+Selenium2完整的自动化测试实现之旅(五):自动化测试框架、Python面向对象以及POM设计模型简介
  14. Python的虚拟机安装已经如何配置Scrapy for Mac
  15. SQL查询日期格式化
  16. python --常用内置模块01
  17. ModBus通信协议的【主从模式】
  18. day 7 -10 集合,文本、文件操作,函数
  19. Win8下IIS的安装和站点的公布
  20. 常用软件安装及VS插件工具

热门文章

  1. QT入门学习笔记1:为什么要选QT及QT软件下载
  2. 安装VS2010 无法打开数据文件deffactory.dat
  3. 在Unix系统上,从源文件、目标文件、可执行文件的编译过程
  4. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest J. Set
  5. Linux学习-Linux的账号与群组
  6. Linux学习-账号管理
  7. VS2017生成.net core项目报错:The current .NET SDK does not support targeting .NET Core 2.1. Either
  8. 28、editText只输入英文字母和&#39;-&#39;,用于授权码输入
  9. 《Nginx高性能Web服务器详解》
  10. day03_09 编码部分历史及文件编码简介