Play Framework 完整实现一个APP(十一)
添加权限控制
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(十一)的更多相关文章
- Play Framework 完整实现一个APP(五)
程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...
- Play Framework 完整实现一个APP(二)
1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...
- Play Framework 完整实现一个APP(十四)
添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...
- Play Framework 完整实现一个APP(十三)
添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...
- Play Framework 完整实现一个APP(十二)
1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...
- Play Framework 完整实现一个APP(十)
1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...
- Play Framework 完整实现一个APP(九)
添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...
- Play Framework 完整实现一个APP(八)
创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...
- Play Framework 完整实现一个APP(六)
需要为Blog添加 查看和发表评论的功能 1.创建查看功能 Application.java中添加 show() 方法 public static void show(Long id) { Post ...
随机推荐
- 追根溯源:EntityFramework 实体的状态变化
阅读目录: 1. 应用场景 2. 场景测试 3. 问题分析 4. 追根溯源 5. 简要总结 1. 应用场景 首先,应用程序使用 EntityFramework,应用场景中有两个实体 S_Class(班 ...
- C# 操作数据库表和数据库
<1>c#创建数据库表: private void CreatTable(string name) //创建数据库源数据表,name为表名 { con.ConnectionStr ...
- 【406错误】 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
今天遇到一个奇怪的错误,关于Springmvc的,我明明在Controller方法中写了@ResponseBody,返回一个Map,结果报了406错误. 结果发现,少了一个jar包: 加上去就没事了.
- bootstrap-简单实用的垂直手风琴滑动菜单列表特效
前端: <html lang="zh"> <head> <meta charset="UTF-8"> <meta ht ...
- 浅谈SQL Server中的三种物理连接操作
简介 在SQL Server中,我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge J ...
- 谈一谈SQL Server中的执行计划缓存(下)
简介 在上篇文章中我们谈到了查询优化器和执行计划缓存的关系,以及其二者之间的冲突.本篇文章中,我们会主要阐述执行计划缓存常见的问题以及一些解决办法. 将执行缓存考虑在内时的流程 上篇文章中提到了查询优 ...
- Windows Server 2012 R2在线安装.NET Framework3.5
Windows Server 2012 (R2) 默认没有安装 .NET Framework 3.5,但可以通过在线安装或指定备用源路径方式. 之前在这个 在Win Server 2012中安装.NE ...
- log4Net(写入日志文件)
这里就简单介绍下log4Net对写入日志文件的一些了解,写入数据库类似,就不在一一介绍了. 首先去log4net下载. 然后我们新建一个控制台应用程序,并引入log4net.dll程序集,log4ne ...
- asp.net mvc项目自定义区域
前言 直接上干货就是,就不废话了. 使用场景:分离模块,多站点等~~ 一.分离模块 自定义视图引擎,设置视图路径格式 项目结构图 1.Code: 在Global.asax Application_St ...
- hdu-1179-二分图最大匹配
Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...