基于SSM框架权限系统的开发

有bug请在评论区留言


这是个十分简单的权限实现,后端采用SSM框架,前段展示层使用ztree(ztree官网),实现控制则使用jsp自定义的判断标签;

第一步:新建power数据库。

用户表user,权利表power,用户-权利关系表userPower,

/*
Navicat MySQL Data Transfer

Source Server         : 123
Source Server Version : 50610
Source Host           : 127.0.0.1:3306
Source Database       : power

Target Server Type    : MYSQL
Target Server Version : 50610
File Encoding         : 65001

Date: 2018-01-15 21:07:24
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for power
-- ----------------------------
DROP TABLE IF EXISTS `power`;
CREATE TABLE `power` (
  `pId` int(4) NOT NULL AUTO_INCREMENT,
  `pName` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`pId`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of power
-- ----------------------------
INSERT INTO `power` VALUES ('1', '吃');
INSERT INTO `power` VALUES ('2', '吃饭');
INSERT INTO `power` VALUES ('3', '吃面条');
INSERT INTO `power` VALUES ('4', '吃零食');
INSERT INTO `power` VALUES ('5', '喝');
INSERT INTO `power` VALUES ('6', '喝芬达');
INSERT INTO `power` VALUES ('7', '喝可乐');
INSERT INTO `power` VALUES ('8', '嘴');
INSERT INTO `power` VALUES ('9', '说话');
INSERT INTO `power` VALUES ('10', '喷人');

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `uId` int(4) NOT NULL AUTO_INCREMENT,
  `uName` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`uId`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'admin');
INSERT INTO `user` VALUES ('2', 'tom');
INSERT INTO `user` VALUES ('3', 'jack');

-- ----------------------------
-- Table structure for userpower
-- ----------------------------
DROP TABLE IF EXISTS `userpower`;
CREATE TABLE `userpower` (
  `uPId` int(4) NOT NULL AUTO_INCREMENT,
  `uPUId` int(11) DEFAULT NULL,
  `uPPId` int(11) DEFAULT NULL,
  PRIMARY KEY (`uPId`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of userpower
-- ----------------------------
INSERT INTO `userpower` VALUES ('1', '1', '1');
INSERT INTO `userpower` VALUES ('12', '2', '1');
INSERT INTO `userpower` VALUES ('13', '2', '2');
INSERT INTO `userpower` VALUES ('14', '2', '3');
INSERT INTO `userpower` VALUES ('15', '2', '4');
INSERT INTO `userpower` VALUES ('16', '2', '5');


数据库完成,开始web部分

第二步:新建maven项目

添加项目相关jar包,这是项目的架构,biaoqian包是存放jsp自定义标签的处理类

基于SSM框架权限系统的开发_第1张图片

代码贴出关键部分

controller层代码:

package cn.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import cn.pojo.Power;
import cn.pojo.User;
import cn.pojo.UserPower;
import cn.service.UserPowerService;
import cn.service.UserService;

@Controller
@RequestMapping("power")
public class PowerController {

	@Resource
	private UserService userService;

	@Resource
	private UserPowerService userPowerService;

	/**
	 * 登录
	 * 
	 * @param request
	 * @param u
	 * @param model
	 * @return
	 */
	@RequestMapping("login")
	public String login(HttpServletRequest request, User u, Model model) {
		User user = userService.login(u);
		if (user == null) {
			return "redirect:/login.jsp";
		}
		HttpSession session = request.getSession();
		session.setAttribute("USER_LOGIN", user);
		if (user.getuName().equals("admin")) {
			List list_user = userService.getAllUser();
			session.setAttribute("list_user", list_user);
			return "admin";
		}
		return "welcome";
	}

	/**
	 * 异步方法,根据uid获取相应权限list
	 * 
	 * @param uId
	 * @return
	 */
	@RequestMapping("getPowerByUId")
	@ResponseBody
	public String getPowerByUId(@RequestParam("uId") String uId) {
		List i = userPowerService.getPowerByUId(Integer.parseInt(uId));
		return JSONArray.toJSONString(i);
	}

	@RequestMapping("updatePower")
	public String updatePower(@RequestParam("uId") String uId, @RequestParam("pow") String pow) {
		userPowerService.deletePower(Integer.parseInt(uId));// 增加前先删除具有的权限
		String[] p = pow.split(",");
		UserPower up = new UserPower();
		for (String power : p) {
			if (power.length() > 0) {
				up.setuPUId(Integer.parseInt(uId));
				up.setuPPId(Integer.parseInt(power));
				userPowerService.addPower(up);// 增加权限
			}
		}
		return "admin";
	}
}
两个service的方法:

package cn.service;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import cn.pojo.UserPower;

public interface UserPowerService {
	// 根据uid查出具有的权限
	List getPowerByUId(Integer uId);

	// 增加权限
	int addPower(UserPower userPower);

	// 删除权限
	int deletePower(Integer uId);
}






package cn.service;

import java.util.List;

import cn.pojo.User;

public interface UserService {
	// 登录
	User login(User u);

	// 获得所有用户
	List getAllUser();
}

jsp页面有3个,login.jsp   welcome.jsp    admin.jsp,这里的login.jsp就不用说了吧,一个输入框输入姓名后,去数据库查是否存在这个人,很简单;

welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/WEB-INF/powertag.tld"  prefix="yu" %>


  
    
    
    My JSP 'welcome.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
  我是${USER_LOGIN.uName },
我具有:

吃,
吃饭,
吃面条,
吃零食,
喝,
喝芬达,
喝可乐,
嘴,
说话,
喷人,


功能的

admin.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>






My JSP 'admin.jsp' starting page











	

你想修改谁的权限:

${list_user.uName }
请注意welcome.jsp中 <%@ taglib uri="/WEB-INF/powertag.tld"  prefix="yu" %> 这段代码,这是自定义标签的引用。

下面开始自定义一个jsp判断标签,简单的哦

jsp标签创建分3步,先创建处理类,在编写tid文件,最后引用。

处理类:

package cn.biaoqian;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

import cn.pojo.Power;
import cn.pojo.User;


/**
 * @author Yu
 *
 */
public class Yu extends TagSupport {


	private PageContext pageContext;

	private String power;

	// 设置PageContext对象
	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
	}

	public void setPower(String power) {
		this.power = power;
	}

	/**
	 * 标签开始处理..调用的方法
	 * 
	 */
	@Override
	public int doStartTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			HttpSession session = request.getSession();
			User u = (User) session.getAttribute("USER_LOGIN");
			System.out.println(u);
			List power = u.getPower();
			for (int i = 0; i < power.size(); i++) {
				if (power.get(i).getpName().equals(this.power)) {
					// 允许访问标签body
					return BodyTagSupport.EVAL_BODY_INCLUDE;// 返回此则执行标签body中内容,SKIP_BODY则不执行
				}
			}
			return BodyTagSupport.SKIP_BODY;
		} catch (Exception e) {
			return BodyTagSupport.SKIP_BODY;
		}
	}

	/**
	 * 
	 * 标签处理结束的调用的方法
	 */
	@Override
	public int doEndTag() throws JspException {
		return super.doEndTag();
	}

	/**
	 * 
	 * 释放资源
	 */
	@Override
	public void release() {
		super.release();
	}

}
powertag.tid文件,注意这个文件位置在WEB-INF目录下,与web.xml同级别:







    1.1
    1.2
    
    
    yu
    
    http://www.mytaglib.com/taglib 
    MyDateTaglib
    My date taglib!  
      
    
        power
        cn.biaoqian.Yu
        
        
            power
            
            true
            
            true
        
        
    
    


最后在需要引用的界面引用

<%@ taglib uri="/WEB-INF/powertag.tld"  prefix="yu" %>

普通成员效果图:

基于SSM框架权限系统的开发_第2张图片

admin效果图:

基于SSM框架权限系统的开发_第3张图片

代码完成!

结束语:给与没有权限设计思路小伙伴的一个方法,仅作为案例(样式什么的随便啦)

转载请注明出处!


你可能感兴趣的:(权限设计)