登录权限管理
在大部分网站中,普遍存在两种角色,如对网站进行管理的管理员,与消费的顾客,使用根据不同类型的人员有着不同的操作功能(本篇主要对管理者和普通用户分配不同的权限)
效果
管理者登录效果
用户登录效果
动态下拉列表
使用表
权限表(以树形菜单展示)
用户表(其中type=1的为管理员,type=2为普通用户)
中间表,根据rid来分配对应的权限
代码部分
关于实体类比较简单不一一展示
dao方法
Userdao:用户登录注册方法
/**
*
*/
package com.shegx.dao;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.shegx.entity.Power;
import com.shegx.entity.User;
import com.shegx.util.BaseDao;
import com.shegx.util.PageBean;
import com.shegx.util.StringUtils;
/**
* @author SHE
*
* 2020年7月1日下午2:31:24
* com.shegx.dao
*/
public class UserDao extends BaseDao<User> {
public User listuser(User user) throws Exception{
String name=user.getName();
String pwd=user.getPwd();
String sql="select * from t_easyui_user where true";
if(StringUtils.isNotBlank(name)) {
sql+=" and name='"+name+"'";
}
if(StringUtils.isNotBlank(pwd)) {
sql+=" and pwd='"+pwd+"'";
}
List<User> list=super.executeQuery(sql, null, User.class);
if(list.size()==0) {
return null;
}
return list.get(0);
}
public int add(User user) throws Exception {
String sql="insert into t_easyui_user(name,pwd) values(?,?) ";
return super.executeUpdate(sql, user, new String[] {"name","pwd"});
}
}
登录注册业务处理Action
/**
*
*/
package com.shegx.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.shegx.entity.User;
import com.shegx.dao.UserDao;
import com.zhuchenxi.framework.ActionSupport;
import com.zhuchenxi.framework.ModelDriven;
/**
* @author SHE
*
* 2020年7月3日下午3:00:50
* com.shegx.web
*/
public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user=new User();
private UserDao userdao=new UserDao();
/* (non-Javadoc)
* @see com.zhuchenxi.framework.ModelDriven#getModel()
*/
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
public String login(HttpServletRequest req,HttpServletResponse resp) {
try {
User listuser = this.userdao.listuser(user);
if(listuser==null) {
return "login";
}
req.getSession().setAttribute("user", listuser);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "login";
}
return "mainTemp";
}
public String register(HttpServletRequest req,HttpServletResponse resp) {
try {
this.userdao.add(user);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "register";
}
return "login";
}
public String wangcai(HttpServletRequest req,HttpServletResponse resp) {
return "register";
}
}
权限分配
根据一张中间表如下,在登录时获取登录人员的类型与下表中的rid进行匹配,来给予对应的权限(表中rid=1为管理者,rid=2位用户)
实体类
/**
*
*/
package com.shegx.dao;
import java.util.List;
import com.shegx.entity.Power;
import com.shegx.util.BaseDao;
import com.shegx.util.PageBean;
/**
* @author SHE
*
* 2020年7月5日下午6:15:53
* com.shegx.dao
*/
public class PowerDao extends BaseDao<Power>{
public List<Power> listPower(Power power,PageBean pageBean) throws Exception {
String sql="select * from t_easyui_role_permission where true";
long rid = power.getRid();
if(rid!=0) {
sql+=" and rid ="+rid;
}
return super.executeQuery(sql, pageBean, Power.class);
}
}
获取登录者类型来查询dao方法
package com.shegx.dao;
import java.util.List;
import com.shegx.entity.Power;
import com.shegx.util.BaseDao;
import com.shegx.util.PageBean;
/**
* @author SHE
*
* 2020年7月5日下午6:15:53
* com.shegx.dao
*/
public class PowerDao extends BaseDao<Power>{
public List<Power> listPower(Power power,PageBean pageBean) throws Exception {
String sql="select * from t_easyui_role_permission where true";
long rid = power.getRid();
if(rid!=0) {
sql+=" and rid ="+rid;
}
return super.executeQuery(sql, pageBean, Power.class);
}
}
处理权限及展开对应的树形菜单action
主要根据查到的rid来获取对应的pid权限,来加载树形菜单
package com.shegx.web;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.shegx.dao.PermissionDao;
import com.shegx.dao.PowerDao;
import com.shegx.entity.Permission;
import com.shegx.entity.Power;
import com.shegx.entity.User;
import com.zhuchenxi.framework.ActionSupport;
import com.zhuchenxi.framework.ModelDriven;
import com.shegx.util.PageBean;
import com.shegx.vo.TreeVo;
import com.shegx.util.ResponseUtil;
import com.shegx.util.StringUtils;
public class PermissionAction extends ActionSupport implements ModelDriven<Permission>{
private Permission ps = new Permission();
private PermissionDao pd = new PermissionDao();
private PowerDao powerdao=new PowerDao();
@Override
public Permission getModel() {
// TODO Auto-generated method stub
return ps;
}
public String menuTree(HttpServletRequest req,HttpServletResponse resp) {
try {
/*TreeVo<Permission> topNode = this.pd.topNode(null, null);
List<TreeVo<Permission>> list = new ArrayList<TreeVo<Permission>>();
list.add(topNode);*/
//获取登录时的用户
User user= (User) req.getSession().getAttribute("user");
//实例化中间表
Power power=new Power();
//得到登录用户的类型
power.setRid(user.getType());
//调用方法
List<Power> listPower = this.powerdao.listPower(power, null);
//转换格式
StringBuilder stringBuilder = new StringBuilder();
for (Power pw : listPower) {
//得到pid逗号分隔
//根据查到的rid来获取对应的pid权限
stringBuilder.append(",").append(pw.getPid());
}
//截取,号后数据
List<TreeVo<Permission>> topNodePower = this.pd.topNodePower(stringBuilder.substring(1), null);
ResponseUtil.writeJson(resp, topNodePower);
//ResponseUtil.writeJson(resp, this.pd.topNode(null, null));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
展开相应树形菜单方法:
关于树形菜单前面有介绍过
public List<Permission> listpower(String stringBuilder,PageBean pageBean) throws Exception {
String sql="select * from t_easyui_Permission where id in ("+stringBuilder+")";
return super.executeQuery(sql, pageBean, Permission.class);
}
public List<TreeVo<Permission>> topNodePower(String stringBuilder,PageBean pageBean) throws Exception {
List<Permission> list = this.listpower(stringBuilder, pageBean);
List<TreeVo<Permission>> nodes = new ArrayList<TreeVo<Permission>>();
TreeVo treeVo = null;
for (Permission p : list) {
treeVo = new TreeVo<>();
treeVo.setId(p.getId()+"");
treeVo.setText(p.getName());
treeVo.setParentId(p.getPid()+"");
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("self", p);
treeVo.setAttributes(attributes);
nodes.add(treeVo);
}
return BuildTree.buildList(nodes,"0");
}
权限管理后台到这结束了
动态加载下拉列表
使用表
实现
实体
/**
*
*/
package com.shegx.entity;
/**
* @author SHE
*
* 2020年7月5日下午8:50:48
* com.shegx.entity
*/
public class Category {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category(long id, String name) {
this.id = id;
this.name = name;
}
public Category() {
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}
方法
package com.shegx.dao;
import java.util.List;
import com.shegx.entity.Category;
import com.shegx.util.BaseDao;
import com.shegx.util.PageBean;
/**
* @author SHE
*
* 2020年7月5日下午8:52:38
* com.shegx.dao
*/
public class CategoryDAo extends BaseDao<Category>{
public List<Category> list(Category category,PageBean pageBean) throws Exception{
String sql="select * from t_easyui_category where true";
return super.executeQuery(sql, pageBean, Category.class);
}
}
ACtion处理层
package com.shegx.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.shegx.dao.CategoryDAo;
import com.shegx.entity.Category;
import com.shegx.util.ResponseUtil;
import com.zhuchenxi.framework.ActionSupport;
import com.zhuchenxi.framework.ModelDriven;
/**下拉列表
* @author SHE
*
* 2020年7月5日下午8:48:39
* com.shegx.web
*/
public class CategoryAction extends ActionSupport implements ModelDriven<Category>{
private CategoryDAo cd=new CategoryDAo();
private Category category=new Category();
/* (non-Javadoc)
* @see com.zhuchenxi.framework.ModelDriven#getModel()
*/
@Override
public Category getModel() {
// TODO Auto-generated method stub
return category;
}
public String combobox(HttpServletRequest req,HttpServletResponse resp) {
try {
List<Category> list = this.cd.list(null, null);
ResponseUtil.writeJson(resp, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
js方法
$(function(){
var ct=$("#ct").val();
$('#cid').combobox({
url:ct+'/category.action?methodName=combobox',
valueField:'id',
textField:'name'
});
})
配置
最后不要忘了配置文件!!!!
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--用户相关业务-->
<action path="/user" type="com.shegx.web.UserAction">
<forward name="login" path="/user.jsp" redirect="false" />
<forward name="register" path="/register.jsp" redirect="false"/>
<forward name="mainTemp" path="/index.jsp" redirect="false"/>
</action>
<!-- 下拉列表 -->
<action path="/category" type="com.shegx.web.CategoryAction"></action>