使用者權限設計(一),使用者權限設計(

來源:互聯網
上載者:User

使用者權限設計(一),使用者權限設計(

我是springmvc加mysql加hibernate的環境,基礎配置的東西就不寫出來的,只把重點的寫出來

1.資料表:一個三個表

  (1) 使用者表:



  (2)對象表



  (3)使用者關於對象對應的許可權表



2.model對象

UserAuth.java

public class UserAuth {private String userName;private Map<String, List<String>> userAuthMap;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public Map<String, List<String>> getUserAuthMap() {return userAuthMap;}public void setUserAuthMap(Map<String, List<String>> userAuthMap) {this.userAuthMap = userAuthMap;}}

AuthAcc.java

@Entity@Table(name = "auth_acc")public class AuthAcc {@Idprivate String id;@Columnprivate String userId;@Columnprivate String objId;@Columnprivate String authOperate;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public String getObjId() {return objId;}public void setObjId(String objId) {this.objId = objId;}public String getAuthOperate() {return authOperate;}public void setAuthOperate(String authOperate) {this.authOperate = authOperate;}}
AuthObj.java

@Entity@Table(name = "auth_obj")public class AuthObj {@Idprivate String id;@Columnprivate String name;@Columnprivate String operate;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getOperate() {return operate;}public void setOperate(String operate) {this.operate = operate;}}

3.Controller
@RequestMapping("authDemo")public String authDemo(Map<String, Object> map) {Map<String, Object> authObjMap = new HashMap<String, Object>();// 儲存所有對象的所有操作許可權Map<String, String> objNmMap = new HashMap<String, String>();// 儲存對象id對應的對象名,用於畫面顯示List<AuthObj> authObjLst = testService.getAuthObjLst();// 得到有許可權管理的所有對象String operateKey = null;// 操作許可權內容字母String operate = null;// 操作許可權內容for (AuthObj mAuthObj : authObjLst) {Map<String, String> operateMap = new HashMap<String, String>();// 操作許可權內容Mapoperate = mAuthObj.getOperate();// 操作許可權內容for (int i = 0; i < operate.length(); i++) {operateKey = operate.substring(i, i + 1);operateMap.put(operateKey, Constants.OPERATEMAP.get(operateKey));//把每個字母對應的名字儲存在map中}authObjMap.put(mAuthObj.getId(), operateMap);//以對象的id為key,操作許可權內容Map為valueobjNmMap.put(mAuthObj.getId(), mAuthObj.getName());// 儲存對象id對應的對象名,用於畫面顯示}map.put("mp", authObjMap);// 儲存所有對象的所有操作許可權map.put("nmMp", objNmMap);// 儲存對象id對應的對象名,用於畫面顯示Map<String, List<String>> authAccMap = new HashMap<String, List<String>>();String userId = "004";// 假設目前使用者id為004List<AuthAcc> authAccLst = testService.getAuthAccLstByUserid(userId);// 擷取目前使用者對應對象的許可權String authOperate = null;//目前使用者的許可權for (AuthAcc mAuthAcc : authAccLst) {List<String> authLst = new ArrayList<String>();//目前使用者的許可權,用一個list儲存authOperate = mAuthAcc.getAuthOperate();//目前使用者的許可權for (int i = 0; i < authOperate.length(); i++) {authLst.add(authOperate.substring(i, i + 1));//遍曆許可權,儲存在一個list裡}authAccMap.put(mAuthAcc.getObjId(), authLst);//以對象id為key,目前使用者的許可權為value,儲存在一個map裡}UserAuth mUserAuth = new UserAuth();mUserAuth.setUserAuthMap(authAccMap);map.put("vm", mUserAuth);return "authDemoVw";}}

4.Constants.java

public class Constants {/** * 定義每個字母對應的許可權名稱 */public final static Map<String, String> OPERATEMAP = new HashMap<String, String>();  static {  OPERATEMAP.put("c", "增加");  OPERATEMAP.put("r", "查詢");  OPERATEMAP.put("u", "更改"); OPERATEMAP.put("d", "刪除"); }  }

5.authDemoVw.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!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=ISO-8859-1"><title>Insert title here</title></head><body><form:form action="" method="post" commandName="vm"><table><c:forEach var="entry" items="${mp}"><tr><td>${nmMp[entry.key]}</td><td><form:checkboxes path="userAuthMap[${entry.key}]"items="${entry.value}" /></td></tr></c:forEach></table></form:form></body></html>

6.


這篇文章只是先把使用者所擁有的許可權列表顯示出來,許可權的更新和許可權的判斷在後面的文章裡會寫





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.