Home > Java > javaTutorial > PlayFramework completely implements an APP (11)

PlayFramework completely implements an APP (11)

黄舟
Release: 2016-12-23 16:51:37
Original
1309 people have browsed it

标签:class   style   log   com   http   使用   it   src   la   

添加权限控制

 

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模块

PlayFramework completely implements an APP (11)

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


  

添加/views/admin.html



   


       Administration
       
       #{get ‘moreStyles‘ /}
                       href="@{‘/public/stylesheets/main.css‘}" />
                       href="@{‘/public/images/favicon.png‘}" />
       
       
   
   
       
       
       
       

           #{doLayout /}
       

       
       
       
   

  

添加/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()).first().isAdmin;
   }    return false;
}

 

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


   
   

           

  •            My posts
           

  •        #{secure.check ‘admin‘}
               

  •                Posts
               

  •            

  •                Tags
               

  •            

  •                Comments
               

  •            

  •                Users
               

  •        #{/secure.check}
       

   
   #{doLayout /}

  

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

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

PlayFramework completely implements an APP (11)

 以上就是PlayFramework 完整实现一个APP(十一)的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template