添加权限控制

1.导入Secure module,该模块提供了一个controllers.Secure控制器。

/conf/application.conf

# Import the secure module
module.secure=${play.path}/modules/secure

  

/conf/routes

# Import Secure routes
* / module:secure

  

2.在Post Comment User Tag控制器上添加标签

@With(Secure.class)
public class Posts extends CRUD {
}

启动Server,访问 localhost:9000/admin,如果页面报错(或编译失败),参考上一章添加CRUD模块

3.添加权限控制

创建权限控制器 /controllers/Security.java

import models.*;

public class Security extends Secure.Security {
static boolean authenticate(String username, String password) {
return User.connect(username, password) != null;
}
}

通过 localhost:9000/logout 退出登录,使用正确的用户名密码进行登录

4.添加登录页面

创建Admin Controller

@With(Secure.class)
public class Admin extends Controller {
@Before
static void setConnectedUser() {
if(Security.isConnected()) {
User user = User.find("byEmail", Security.connected()).first();
renderArgs.put("user", user.fullname);
}
} public static void index() {
render();
}
}

添加路由

# Administration
GET /admin/? Admin.index
* /admin module:crud

  

修改main.html

<ul id="tools">
<li>
<a href="@{Admin.index()}">Log in to write something</a>
</li>
</ul>

  

添加/views/admin.html

<!DOCTYPE html>
<html>
<head>
<title>Administration</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
#{get 'moreStyles' /}
<link rel="stylesheet" type="text/css" media="screen"
href="@{'/public/stylesheets/main.css'}" />
<link rel="shortcut icon" type="image/png"
href="@{'/public/images/favicon.png'}" />
<script src="@{'/public/javascripts/jquery-1.4.2.min.js'}"></script>
<script src="@{'/public/javascripts/jquery.tools-1.2.5.toolbox.expose.min.js'}"></script>
</head>
<body id="admin"> <div id="header">
<div id="logo">
yabe. <span>administration</span>
</div>
<ul id="tools">
<li>
<a href="@{Secure.logout()}">Log out</a>
</li>
</ul>
</div> <div id="main">
#{doLayout /}
</div> <p id="footer">
Yabe is a (not so) powerful blog engine built with the
<a href="http://www.playframework.org">Play framework</a>
as a tutorial application.
</p> </body>
</html>

  

添加/views/Admin/index.html

#{extends 'admin.html' /}

Welcome ${user}!

  

5.添加角色

Security控制器重写check方法

static boolean check(String profile) {
if("admin".equals(profile)) {
return User.find("byEmail", connected()).<User>first().isAdmin;
}
return false;
}

修改admin.html,显示用户是否有admin角色

<div id="main">

    <ul id="adminMenu">
<li class="${request.controller == 'Admin' ? 'selected' : ''}">
<a href="@{Admin.index()}">My posts</a>
</li>
#{secure.check 'admin'}
<li class="${request.controller == 'Posts' ? 'selected' : ''}">
<a href="@{Posts.list()}">Posts</a>
</li>
<li class="${request.controller == 'Tags' ? 'selected' : ''}">
<a href="@{Tags.list()}">Tags</a>
</li>
<li class="${request.controller == 'Comments' ? 'selected' : ''}">
<a href="@{Comments.list()}">Comments</a>
</li>
<li class="${request.controller == 'Users' ? 'selected' : ''}">
<a href="@{Users.list()}">Users</a>
</li>
#{/secure.check}
</ul> #{doLayout /}
</div>

  

在Post、Tag、User、Comment控制器上添加标签,只有admin role的User才能访问 http://localhost:9000/admin/{####}

@Check("admin")
@With(Secure.class)
public class Posts extends CRUD {
}

。。

Play Framework 完整实现一个APP(十一)的更多相关文章

  1. Play Framework 完整实现一个APP(五)

    程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...

  2. Play Framework 完整实现一个APP(二)

    1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...

  3. Play Framework 完整实现一个APP(十四)

    添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...

  4. Play Framework 完整实现一个APP(十三)

    添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...

  5. Play Framework 完整实现一个APP(十二)

    1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...

  6. Play Framework 完整实现一个APP(十)

    1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...

  7. Play Framework 完整实现一个APP(九)

    添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...

  8. Play Framework 完整实现一个APP(八)

    创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...

  9. Play Framework 完整实现一个APP(六)

    需要为Blog添加 查看和发表评论的功能 1.创建查看功能 Application.java中添加 show() 方法 public static void show(Long id) { Post ...

随机推荐

  1. 【续集】在 IIS 中部署 ASP.NET 5 应用程序遭遇的问题

    dudu 的一篇博文:在 IIS 中部署 ASP.NET 5 应用程序遭遇的问题 针对 IIS 部署 ASP.NET 5 应用程序的问题,在上面博文中主要采用两种方式尝试: VS2015 的 Publ ...

  2. ASP.NET Web API 异常日志记录

    如果在 ASP.NET MVC 应用程序中记录异常信息,我们只需要在 Global.asax 的 Application_Error 中添加代码就可以了,比如: public class MvcApp ...

  3. C算法编程题(三)画表格

    前言 上一篇<C算法编程题(二)正螺旋> 写东西前还是喜欢吐槽点东西,要不然写的真还没意思,一直的想法是在博客园把自己上学和工作时候整理的东西写出来和大家分享,就像前面写的<T-Sq ...

  4. C/C++ char a[ ] 和 char *a 的差别,改变 char *a爆内存错误的原因

    对于一些需要传入参数为 char * temp 指针类的函数: 我们定义一个 char a[10] 或char *a 传进去都是可以的. 但是, 如果该函数是会改变你所传入的参数的值时, 传入 cha ...

  5. 跨域post请求实现方案小结--转

    [名词解释] 跨域:https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript 同源策略 ...

  6. JS中的this

    JS中的this 最近在学习JavaScript面向对象,其中有个难点就是关于this的问题. 关于this,我们要知道的两个问题,一个是this指向什么?另一个是this可以写在哪? 关于this的 ...

  7. 无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS"

    无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间 2011-0 ...

  8. jquery实现更多内容效果

    体验效果:http://hovertree.com/texiao/jquery/33/ 写个“更多内容的展开/收起”的js 代码如下: <!DOCTYPE html> <html&g ...

  9. JS进阶之原型

    之前有在自己的文章中谈到对象,而说到对象我们就不可避免的要扯到原型,并且原型也是我们必须得理解到位的一个点,那接下来我们就来聊一聊js的原型吧. JS中一切皆为对象,那么原型也是一种对象.所以它有对象 ...

  10. HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法

    [问题] 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http的请求request,其中keepAlive设置为true,提交请求后,然后会有对应的response: resp = (Http ...