Play
来源:互联网 发布:不用网络的游戏大全 编辑:程序博客网 时间:2024/04/28 10:20
Scope.Flash flash:一个存放在hash map中存储临时变量的变量范围。。通常情况下,在request的处理过程中把值放进去,然后在紧接着的下一个请求处理中被使用,下一个请求(第2个请求)处理完毕后,值即被销毁。
Scope.Params params:在整个请求的处理过程中都能被访问到,即存放当前请求的参数。
Scope.RenderArgs renderArgs:一个存放在hash map中并在渲染阶段使用的变量范围。存储在这里的变量在渲染阶段(即模板阶段)被使用。
Scope.RouteArgs routeArgs:一个存放在hash map中并在路由处理阶段使用的变量范围。
Scope.Session session:play 1.x framework中,session并不存储在服务器端,而是存储在客户端。实际上它存储在cookie中,因此大小被限制在4kb以内。
---------------------------------------------------------------------------------------------------
package controllers;
import org.apache.commons.lang.StringUtils;
import com.tom.basic.service.ServiceResult;
import basic.service.AuthService;
import infrastructure.play.control.ControllerUtils;
import infrastructure.support.Constants;
import play.data.validation.Required;
import play.mvc.Before;
import seller.model.Seller;
import seller.model.Store;
import seller.service.SellerService;
import user.model.User;
public class Secure extends ControllerBase {
/**
* @throws Throwable
*/
@Before(unless = { "login", "authenticate", "logout","Application.home","CommonMgr.imageNotify",
"TradeMgr.exportQrcodeImg","register","SellerMgr.doRegister","SellerMgr.doCheckField",
"SellerMgr.imageCode"})
static void checkAccess() throws Throwable {
String username = session.get(Constants.SESSION_KEY_USER);
if (username == null) {
session.clear();
// String[] url = request.action.split("\\.");
session.put(Constants.SESSION_KEY_RELOGIN_CONTROLLER, request.url);
if(request.headers.get("x-requested-with") != null){
renderJSON(ServiceResult.asSuccess("ReLogin"));
}
login();
} else {
User curUser = User.find("byName", username).first();
if (curUser == null) {
login();
} else {
//菜单权限过滤拦截
if(curUser.isSeller()){
Seller curSeller = Seller.findById(curUser.extendId);
String storeId = session.get(Constants.SESSION_KEY_STORE);
Store curStore = null;
if(StringUtils.isNotBlank(storeId)){
curStore = Store.findById(Long.parseLong(storeId));
}else{
if(curUser.getDefaultStoreIdFromOrderMeta() != null){
curStore = Store.findById(curUser.getDefaultStoreIdFromOrderMeta());
}
}
if(AuthService.isNeedAuth(request.actionMethod,curUser) &&
!request.action.equals("Application.index") && !AuthService.isSellerMenu(request.path,curUser,curSeller,curStore)){
renderTemplate("/Secure/nonAuthority.html");
}
threadSeller.set(curSeller);
ControllerUtils.addAttri4Render("currentSeller", curSeller);//模板中都可以获取到
threadStore.set(curStore);
ControllerUtils.addAttri4Render("currentStore", curStore);
}else{
if(AuthService.isNeedAuth(request.actionMethod,curUser) && StringUtils.isNotBlank(curUser.authSourcesStr)&&
!request.action.equals("Application.index") && !curUser.authSourcesStr.contains(request.actionMethod)){
renderTemplate("/Secure/nonAuthority.html");
}
threadSeller.set(null);
threadStore.set(null);
}
threadUser.set(curUser);
ControllerUtils.addAttri4Render("currentUser", curUser);
flash.put(Constants.SESSION_KEY_USER, curUser);
// 操作权限过滤拦截
if (!AuthService.authenticateByUrl(request.url,currentUser())){
renderJSON(ServiceResult.asFail("Current user cannot operate!").toJson());
}
}
}
}
// ~~~ Login
public static void login() throws Throwable {
User rootUser = User.find("byName", "root").first();
if (rootUser == null) {
rootUser = new User();
rootUser.password = "root";
rootUser.name = "root";
rootUser.save();
}
if(StringUtils.isNotBlank(session.get(Constants.SESSION_KEY_RELOGIN_CONTROLLER))){
render("Secure/relogin.html");
}
if(request.domain.contains("admin")){
render("Secure/login_admin.html");
}else{
render("Secure/login.html");
}
}
public static void authenticate(@Required String username, String password)
throws Throwable {
User curUser = User.find("byIsDeleteAndNameAndPassword", 0 ,username, password)
.first();
if (curUser != null) {
Long storeIdDefault = curUser.getDefaultStoreIdFromOrderMeta();
if(storeIdDefault == null){
session.put(Constants.SESSION_KEY_STORE, "");
}else{
session.put(Constants.SESSION_KEY_STORE, storeIdDefault);
}
session.put(Constants.SESSION_KEY_USER, username);
ServiceResult<String> result = SellerService
.checkPassword(password);
if (!result.success) {
Long userId = curUser.id;
renderTemplate("/Secure/modifyPassword.html", userId);
}
if(StringUtils.isNotBlank(session.get(Constants.SESSION_KEY_RELOGIN_CONTROLLER))){
String[] url = session.get(Constants.SESSION_KEY_RELOGIN_CONTROLLER).split("\\/");
if(url[url.length -1].startsWith("do")){
redirect("/admin/");
}
redirect(session.get(Constants.SESSION_KEY_RELOGIN_CONTROLLER));
}else{
redirect("/admin/");
}
} else {
params.flash();
flash.error("登录密码错误,请重试");
login();
}
}
public static void logout() throws Throwable {
session.clear();
login();
}
public static void register() {
render();
}
}
public static void addAttri4Render(String key, Object value) {
Scope.RenderArgs templateBinding = Scope.RenderArgs.current();
Map<String, Object> argsMap = new HashMap<String, Object>();
argsMap.put(key, value);
templateBinding.data.putAll(argsMap);
}
- play
- play
- Play!
- play
- Play
- play Framework play json
- Real Play
- Play More
- play-linux
- Play Eclipse
- Play jsAction
- play 教程
- play paino
- play! First
- play framework
- google play
- Package Play
- play!框架
- 从零开始学习OpenCL开发(四)shader
- 缓存策略交互 — 最长使用期限和最低新鲜度
- ASCII,Unicode和UTF-8
- linux压缩和解压缩命令大全
- js中几种实用的跨域方法原理详解
- Play
- HTTP协议(二):协议详解
- EOS 带参数的国际化
- $.each(json,function(index,item){ });
- 开启服务的方式:(1)start开启服务
- java数组基础
- SQL Server索引的维护
- Java反射机制总结
- UIFactory打开新页面,传递参数