最近在網(wǎng)上學(xué)習(xí)了一些權(quán)限管理系統(tǒng),發(fā)現(xiàn)都差不多,只是復(fù)雜一些的權(quán)限管理系統(tǒng)分的更加細(xì)致唱歧,考慮的因素也越多。權(quán)限管理模塊就是為了管理用戶是否有權(quán)利訪問(wèn)某個(gè)權(quán)限,如果不能則拒絕訪問(wèn)颅崩。而我自己也做利用了jfinal做了一個(gè)簡(jiǎn)單的權(quán)限管理系統(tǒng)几于。小弟不精,可能還有些錯(cuò)誤沿后,請(qǐng)大家指明.
還有就是這里數(shù)據(jù)的表不對(duì)應(yīng)貼出來(lái)的代碼的表
數(shù)據(jù)庫(kù):
表1:super administrator (對(duì)應(yīng)代碼的teacher) 超級(jí)管理員的賬號(hào)密碼
表2:administrator 存儲(chǔ)普通的管理員的賬號(hào)密碼
表3:administrator_message (對(duì)應(yīng)代碼的groups) 普通管理員的基本信息
表4:students
表5:student_message (對(duì)應(yīng)代碼的message)
表創(chuàng)建對(duì)應(yīng)的id,name,age,sex,remark屬性等
超級(jí)管理員inori:可以對(duì)學(xué)生進(jìn)行增刪改查,對(duì)其他管理員進(jìn)行權(quán)限分配
普通的管理員miku:只能查看學(xué)生信息和增加學(xué)生
普通的管理員Alan:只能查看學(xué)生信息和修改學(xué)生信息
普通的管理員shiro:只能查看學(xué)生信息和刪除學(xué)生
學(xué)生:只能查看自己的基本信息
我是在上兩篇jfinal的基礎(chǔ)上進(jìn)行修改,只貼權(quán)限管理的部分代碼
登錄時(shí)的驗(yàn)證
package com.login;
import java.util.List;
import com.Interceptor.LogInterceptor;
import com.Permissions.Groups;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.render.CaptchaRender;
import com.students.students;
import com.teachers.teachers;
import net.sf.json.JSONObject;
public class loginControler extends Controller
{
public void index()
{
render("login.jsp");
}
public void login()
{
String username=getPara("username");
String password=getPara("password");
String sql="select * from teachers where username='"+username+"' and password='"+password+"'";
List<teachers> list=teachers.dao.find(sql);
//驗(yàn)證超級(jí)管理員身份
if(list.size()!=0 )
{
redirect("/choose");
}
else
{
sql = "select * from Administrator where username='"+username+"' and password='"+password+"'";
list=teachers.dao.find(sql);
if(list.size()!=0)
{
sql="select * from groups where name=(select name from Administrator where username='"+username+"' and password='"+password+"')";
List<Groups> groupslist=Groups.groups.find(sql);
setSessionAttr("groupslist", groupslist);
redirect("/students");
}
}
String str="select * from message where school_id='"+username+"' and password='"+password+"'";
List<students> student=students.students.find(str);
System.out.println(student.toString());
//驗(yàn)證學(xué)生登錄身份
if(student.size()!=0 )
{
str="select * from students where name=(select name from message where school_id='"+username+"' and PASSWORD='"+password+"')";
List<students> stu=students.students.find(str);
setAttr("student", stu);
render("/message/message.jsp");
}
}
}
這是普通管理員的模塊
package com.Permissions;
import com.jfinal.plugin.activerecord.Model;
//管理員模塊
public class Groups extends Model<Groups>
{
public static final Groups groups=new Groups();
}
這是超級(jí)管理員登錄的時(shí)能夠看到的全部普通管理員基本信息模塊
package com.Permissions;
import java.util.ArrayList;
import java.util.List;
import com.jfinal.core.Controller;
public class PermissionsGroups extends Controller
{
public void index()
{
List<Groups> groupslist=Groups.groups.find("select * from groups");
setAttr("groups", groupslist);
render("list.jsp");
}
public void form()
{
Groups group=Groups.groups.findById(getParaToInt(0));
if(group==null)
{
render("form.jsp");
}
else
{
setAttr("group", group);
render("form.jsp");
}
}
public void add()
{
Groups group=getModel(Groups.class,"group");
group.save();
redirect("/PermissionsGroups");
}
public void edit()
{
form();
}
public void delete()
{
Groups.groups.deleteById(getParaToInt(0));
index();
}
/**
* 注銷(xiāo)賬號(hào)
*/
public void logout()
{
redirect("/login");
}
}
下面是超級(jí)管理員對(duì)普通管理員團(tuán)隊(duì)進(jìn)行增刪改查
接下來(lái)是前端顯示的頁(yè)面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<script type="text/javascript">
function add()
{
window.location.href="/students/form";
}
function edit(id)
{
window.location.href="/students/edit/"+id;
}
function del(id)
{
window.location.href="/students/delete/"+id;
}
function logout()
{
window.location.href="/students/logout/";
}
</script>
<body>
<c:forEach items="${groupslist}" var="groupslist">
<div><h1>歡迎${groupslist.name}</h1></div>
<button <c:if test="${groupslist.permissions!='1'}">disabled="true"</c:if> onclick="add()">增加學(xué)生</button>
</c:forEach>
<button onclick="logout()">注銷(xiāo)賬號(hào)</button>
<table border="2" width="80%">
<caption><h1>學(xué)生信息</h1></caption>
<thead>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>備注</th>
<th rowspan="2">操作</th>
</thead>
<tbody>
<c:forEach items="${student}" var="student">
<tr>
<th>${student.name}</th>
<th>${student.age}</th>
<th>${student.sex}</th>
<th>${student.remark}</th>
<th>
<c:forEach items="${groupslist}" var="groupslist">
<button <c:if test="${groupslist.permissions!='2'}">disabled="true"</c:if> onclick="edit(${student.id})">修改</button>
<button <c:if test="${groupslist.permissions!='3'}">disabled="true"</c:if> onclick="del(${student.id})">刪除</button>
</c:forEach>
</th>
</tr>
</c:forEach>
</tbody>
<tr>
<th colspan="5">總頁(yè)數(shù) ${pagination}/${page}
<a href="/students">首頁(yè)</a>
<a href="/students/Previouspage/${pageNumber}-${pagination}">上一頁(yè)</a>
<a href="/students/nextpage/${pageNumber}-${pagination}">下一頁(yè)</a>
<a href="/students/trailerpage/">尾頁(yè)</a>
<form action="/students/jumpPage/">
<label>跳轉(zhuǎn)到</label>
<input type="text" name="number">
<button type="submit">確定</button>
</form>
</th>
</tr>
</table>
</body>
</html>
這是form.jsp表單
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/PermissionsGroups/add" method="post">
<label>姓名</label>
<input type="text" name="group.name" value="${group.name}">
<label>年齡</label>
<input type="text" name="group.age" value="${group.age}">
<div >
<label>性別</label>
<div>
<input type="radio" name="group.sex" <c:if test="${group.sex=='男'}">checked="checked"</c:if>value="男"> 男
</div>
<div>
<input type="radio" name="group.sex" <c:if test="${group.sex=='女'}">checked="checked"</c:if>value="女"> 女
</div>
</div>
<div>
<label>職務(wù)</label>
<select name="group.duty">
<option>選擇職務(wù)</option>
<option value="人力部部長(zhǎng)" ${group.duty=='人力部部長(zhǎng)'?'selected':''}>人力部部長(zhǎng)</option>
<option value="資源部部長(zhǎng)" ${group.duty=='資源部部長(zhǎng)'?'selected':''}>資源部部長(zhǎng)</option>
<option value="信息部部長(zhǎng)" ${group.duty=='信息部部長(zhǎng)'?'selected':''}>信息部部長(zhǎng)</option>
</select>
</div>
<div>
<label>權(quán)限</label>
<select name="group.permissions" ">
<option>選擇權(quán)限</option>
<option value="1" ${group.permissions=='1'?'selected':''}>查看學(xué)生信息和增加學(xué)生</option>
<option value="2" ${group.permissions=='2'?'selected':''}>查看學(xué)生信息和修改學(xué)生信息</option>
<option value="3" ${group.permissions=='3'?'selected':''}>查看學(xué)生信息和刪除學(xué)生</option>
</select>
</div>
<label>備注</label>
<textarea rows="10" cols="20" name="group.remark">${group.remark}</textarea>
<button type="submit">提交</button>
</form>
</body>
</html>
這是超級(jí)管理員登錄界面
接下面是各個(gè)管理員的權(quán)限
接下面是源碼鏈接(附上數(shù)據(jù)庫(kù)): https://pan.baidu.com/s/1nuCZwWp 密碼: bpxa
總結(jié):其實(shí)剛開(kāi)始做的時(shí)候沒(méi)怎么想清楚要怎么做沿彭,就先做的先,后面慢慢摸索到技巧了尖滚,前面的東西要想規(guī)范化喉刘,就要全部修改。這個(gè)壞習(xí)慣一定會(huì)改F崤D郎选!撼唾。這個(gè)簡(jiǎn)單權(quán)限管理系統(tǒng)思路就是