简体   繁体   English

用户认证 - Play 框架,Java

[英]User Authentication - Play Framework, Java

I am new to Play Framework.我是 Play 框架的新手。 I am doing practicing (Play+Java) by creating various types of scenario eg link with the DB, retrieve data from Database using JPA, Master Detail forms, saving data into database etc.我正在通过创建各种类型的场景来练习(Play + Java),例如与数据库链接、使用 JPA 从数据库中检索数据、主详细信息表单、将数据保存到数据库等。

I want to implement user authentication.我想实现用户身份验证。 User login to application.用户登录到应用程序。 To access application user must have authorization.要访问应用程序用户必须有授权。

For example : Employee - Add/Edit/View/Delete.例如:员工 - 添加/编辑/查看/删除。 Some users may have all the authorization and some users have View only or Add/Edit/Delete or only Edit.一些用户可能拥有所有权限,而一些用户只有查看或添加/编辑/删除或仅编辑。 Even in Employee Edit option user can only edit certain field eg Emplyee name is not allowed but employee's address can edit.即使在员工编辑选项中,用户也只能编辑某些字段,例如不允许员工姓名但员工地址可以编辑。 ie field level authorization.即现场级授权。

Thanks谢谢

  1. use an Authentication Controller to check user login id/password class AuthController @Inject()(cache: CacheApi, cached: Cached, actorSystem: ActorSystem) extends Controller {使用身份验证控制器检查用户登录 ID/密码class AuthController @Inject()(cache: CacheApi, cached: Cached, actorSystem: ActorSystem) extends Controller {

  2. if id/password is correct, generate an UUID key, and stored user object (uservo) in memory cache with UUID key.如果 id/password 正确,则生成一个 UUID 密钥,并使用 UUID 密钥将用户对象 (uservo) 存储在内存缓存中。 The key can be stored in browser cookie.密钥可以存储在浏览器 cookie 中。

val cacheTimeout = 10.minutes val key = UUID.randomUUID.toString cache.set(key, uservo, cacheTimeout) Ok().withSession( request.session + ("key" -> key) )

  1. We can get cookie with "key" value in the following html requests.我们可以在以下 html 请求中获取带有“key”值的 cookie。 If the key can be retrieved from memory cache, the user is valid to do the action.如果可以从内存缓存中检索密钥,则用户可以执行该操作。 Remember to set the cookie value again, or else the cached uservo in memory cache will timeout after 10 minutes记得重新设置cookie值,否则内存缓存中缓存的uservo会在10分钟后超时

(request.session.get("key").flatMap { key => cache.get[Uservo](key) } map { uservo => // need to set cache with "key" again, or else the memory will timeout in 10 minutes cache.set(request.session.get("key").get, uservo, cacheTimeout) // do the action.. }).orElse { // failed and return to login home page Some(Future(Redirect("/").withNewSession)) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM