Playframework2的Security使用
来源:互联网 发布:js实现拖拽工作流配置 编辑:程序博客网 时间:2024/05/20 16:00
该trait需要实现的方法包括:
1. username定义了一个Function1的对象,用于实现从Session中取得登录成功用户的名称,或者是用户ID,根据实际情况进行修改;主要用于Security.Authenticated方法调用是的第一个参数:
* @tparam A the type of the user info value (e.g. `String` if user info consists only in a user name)
* @param userinfo function used to retrieve the user info from the request header
* @param onUnauthorized function used to generate alternative result if the user is not authenticated
* @param action the action to wrap
def Authenticated[A]( userinfo: RequestHeader => Option[A], onUnauthorized: RequestHeader => Result)(action: A => EssentialAction): EssentialAction
如果该函数返回的Option对象未定义,将会触发onUnauthorized方法。实现方式,如
def username(request: RequestHeader) = request.session.get("email")
2. 主要负责在未找到用户登录认证的情况下,将页面跳转到登录页面;
def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Application.login)
3. Controller中Action的包装器,注意传入参数的类型定义,是一个层层传递的参数的过程函数,首先是String类型的用户登录凭证(存储于Session当中);其次,是Action常规使用的RequestHeader,最后是返回的Action的Result类型;
def isAuthenticated(f: => String => Request[AnyContent] => Result) = {<span style="white-space:pre"></span>Authenticated(username, onUnauthorized) { user =><span style="white-space:pre"></span>Action(request => f(user)(request))<span style="white-space:pre"></span>} }
通过以上的思路,实现一个trait(特质)的时候,可以将其mixin到Controller中,并用isAuthenticated来包装原有的Action.
trait实现样例:
可以将其放在Application控制器相同的文件当中。
控制器的写法变为:
object Application extends Controller with Secured
原有每个Action的实现,将使用withAuth(前面介绍的isAuthenticated方法,只是在实际实现的时候,换了个名字)进行替换:
def listUser = withAuth {userid =>implicit request =>业务代码}
通过这样的方式在当前的业务代码中,可以直接使用当前登录用户的凭证,userid。
另外,在trait实现的过程中,增加了一个扩展,withUser。它使用withAuth进行包装,在其中实现通过用户登录凭证,从后台获取的完整的用户信息。那么Action的包装就变为:
def listUser = withUser {user =>implicit request =>业务代码}这样就可以在业务代码中直接通过user访问到完整的登录用户信息。
- Playframework2的Security使用
- playFramework2
- Spring Security的使用
- spring security的使用
- Spring Security中<@security.authorize的使用
- java.security.MessageDigest的使用
- spring security的简单使用
- java.security.MessageDigest的使用
- Spring Security的配置使用
- springboot+security 的BCryptPasswordEncoder 使用
- playFramework1 V2 playFramework2
- Playframework2.4 学习
- playFramework2 JDBC连接数据库
- playframework2.1迁移2.2
- 编写一个使用WS-Security的WebMethod
- 图解Spring Security默认使用的过滤器
- java.security.MessageDigest的使用(1)!
- java.security.MessageDigest的使用(1)!
- csdn的blog开发小组,求求你们别再毁这个blog系统了!
- 【最短路】 HDU 4725 The Shortest Path in Nya Graph
- 集线器、交换机、路由器、网桥、网关以及它们之间的区别
- ffmpeg中swscale 的用法
- 博客,我们是写给谁看的
- Playframework2的Security使用
- 【LeetCode】Convert Sorted List to Binary Search Tree
- 为什么写技术博客对新人如此重要?
- 关于 Java Collections
- 图的遍历(BFS,DFS)
- SMALI 语法学习
- 比memcpy更快的内存拷贝【转】
- 数据结构C++版第7章节笔记
- 【公告】博客数据异常已全部恢复