美文网首页web后端
基于角色权限系统(第一节)-用户登录实现

基于角色权限系统(第一节)-用户登录实现

作者: xxiaowen | 来源:发表于2017-02-13 11:42 被阅读1243次

    一、表的设计

    二、需求分析

    1、用户登录

    2、用户管理:用户角色动态分析

    3、角色管理:角色权限动态分析

    4、菜单管理

    三、数据表的设计

    1、用户、角色、权限菜单关系

    2、用户和角色是一对多的关系

    3、角色和权限菜单是多对多的关系

    t_user用户表

    CREATE TABLE `t_user` (

    `userId` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id',

    `userName` varchar(20) DEFAULT NULL COMMENT '用户名',

    `password` varchar(20) DEFAULT NULL COMMENT '密码',

    `userType` tinyint(4) DEFAULT NULL COMMENT '用户类型',

    `roleId` int(11) DEFAULT NULL COMMENT '角色id(外键)',

    `userDescription` varchar(200) DEFAULT NULL COMMENT '用户备注',

    PRIMARY KEY (`userId`),

    KEY `FK_t_user` (`roleId`),

    CONSTRAINT `FK_t_user` FOREIGN KEY (`roleId`) REFERENCES `t_role` (`roleId`)

    ) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8

    t_role角色表

    CREATE TABLE `t_role` (

    `roleId` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色id',

    `roleName` varchar(20) DEFAULT NULL COMMENT '角色名称',

    `authIds` varchar(50) DEFAULT NULL COMMENT '菜单权限ID集合',

    `roleDescription` varchar(200) DEFAULT NULL COMMENT '角色备注',

    PRIMARY KEY (`roleId`)

    ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8

    t_ruth权限菜单表

    CREATE TABLE `t_auth` (

    `authId` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜单ID',

    `authName` varchar(20) DEFAULT NULL COMMENT '菜单名称',

    `authPath` varchar(100) DEFAULT NULL COMMENT '菜单路径',

    `parentId` int(11) DEFAULT NULL COMMENT '父级id',

    `authDescription` varchar(200) DEFAULT NULL COMMENT '资源备注',

    `state` varchar(20) DEFAULT NULL COMMENT '状态',

    `iconCls` varchar(20) DEFAULT NULL COMMENT '菜单图标',

    PRIMARY KEY (`authId`)

    ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8

    四、开发环境搭建

    4.1、使用到技术:jsp+servlet+mysql+easyui

    4.2、web环境搭建

    点击下一步

    点击下一步

    点击下一步

    点击finish

    4.3、创建好的web项目工程结构

    4.4、导入相关的jar包

    4.5、引入jquery-easyui框架

    4.6、编写JSP页面代码

    login.jsp

    <%@pagelanguage="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    "Content-Type"content="text/html; charset=UTF-8">

    "style/login.css"rel="stylesheet"type="text/css">

    用户登陆

    "text/javascript">

    functionloadimage(){

    document.getElementById("randImage").src="images/image.jsp?"+Math.random();

    }

    "form1"name="form1"action="user?action=login"method="post">

    "MAIN">

      "top">

      "top2">

      "topA">

      "topB">

      "http://www.baidu.com"target="_blank">"images/login/logo.gif"alt=""style=""/>

      "topC">

      "topD">

      "login">

    • "left">用户名:"userName"name="userName"type="text"class="txt"value="${userName }"/>
    • "left">密  码:"password"name="password"type="password"class="txt"value="${password }"onkeydown="if(event.keyCode==13)form1.submit()"/>
    • "left">验证码:"text"value="${imageCode }"name="imageCode"class="txtCode"id="imageCode"size="10"onkeydown="if(event.keyCode==13)form1.submit()"/> "换一张试试"name="randImage"id="randImage"src="images/image.jsp"width="60"height="20"border="1"align="absmiddle">
    • "topE">

      "middle_A">

      "middle_B">

      "middle_C">"btn">""src="images/login/btnlogin.gif"onclick="javascript:document.getElementById('form1').submit()"/>  "red">${error }

      "middle_D">

      "bottom_A">

      "bottom_B">

      login.css

      body

      {

      margin:0;

      padding:0;

      font-size:12px;

      background:#214D90url(../images/login/bg.gif)repeat-x;

      color:#999999;

      font-family:Tahoma,Verdana;

      }

      ul

      {

      list-style:none;

      margin:0;

      padding:0;

      }

      .Main

      {

      width:940px;

      margin:0auto;

      }

      .top

      {

      height:75px;

      background:url(../images/login/login_01.gif)no-repeat;

      }

      .top2

      {

      height:94px;

      background:url(../images/login/login_02.gif)no-repeat;

      }

      .topA

      {

      width:155px;

      height:140px;

      float:left;

      overflow:hidden;

      background:url(../images/login/login_03.gif)no-repeat;

      }

      .topB

      {

      width:282px;

      height:140px;

      float:left;

      overflow:hidden;

      background:url(../images/login/login_04.gif)no-repeat;

      }

      .topC

      {

      width:27px;

      height:140px;

      float:left;

      overflow:hidden;

      background:url(../images/login/login_05.gif)no-repeat;

      }

      .topD

      {

      width:318px;

      height:140px;

      float:left;

      overflow:hidden;

      background:url(../images/login/login_06.gif)no-repeat;

      }

      .topE

      {

      width:158px;

      height:140px;

      float:left;

      overflow:hidden;

      background:url(../images/login/login_07.gif)no-repeat;

      }

      .middle_A

      {

      width:155px;

      height:94px;

      float:left;

      overflow:hidden;

      background:url(../images/login/login_08.gif)no-repeat;

      }

      .middle_B

      {

      width:309px;

      height:94px;

      float:left;

      overflow:hidden;

      background:url(../images/login/login_09.gif)no-repeat;

      }

      .middle_C

      {

      width:318px;

      height:94px;

      float:left;

      overflow:hidden;

      background:url(../images/login/login_10.gif)no-repeat;

      }

      .middle_D

      {

      width:158px;

      height:94px;

      float:left;

      overflow:hidden;

      background:url(../images/login/login_11.gif)no-repeat;

      }

      .bottom_A

      {

      height:72px;

      clear:both;

      overflow:hidden;

      background:url(../images/login/login_12.gif)no-repeat;

      }

      .bottom_B

      {

      height:70px;

      overflow:hidden;

      text-align:center;

      background:url(../images/login/login_13.gif)no-repeat;

      }

      .login

      {

      }

      .loginli

      {

      line-height:35px;

      height:35px;

      overflow:hidden;

      }

      .left

      {line-height:35px;

      vertical-align:middle;

      width:50px;

      height:34px;

      text-align:right;

      display:-moz-inline-box;

      display:inline-block;

      padding-bottom:3px;

      }

      .txt

      {

      width:215px;

      height:30px;

      line-height:30px;

      overflow:hidden;

      border:0px;

      padding-left:3px;

      color:#999999;

      background:url(../images/login/txt.gif)no-repeatleftcenter;

      }

      .txtCode

      {

      line-height:30px;

      width:93px;

      height:30px;

      overflow:hidden;

      border:0px;

      padding-left:3px;

      color:#999999;

      background:url(../images/login/code.gif)no-repeatleftcenter;

      }

      .btn

      {

      margin-left:53px;

      }

      main.jsp注界面

      ${currentuser.userName},登陆成功

      4.7、编写相关后端代码

      实体类

      packagecom.xiaowen.model;

      importjava.io.Serializable;

      /**

      * 用户实体类

      *@authorxiaowen

      */

      publicclassUserimplementsSerializable {

      privatestaticfinallongserialVersionUID= 1L;

      privateIntegeruserId;//用户ID

      privateStringuserName;//用户名

      privateStringpassword;//密码

      privateIntegeruserType;//用户类型

      privateIntegerroleId=1;//角色ID

      privateStringuserDescription;//用户备注

      publicUser() {

      super();

      }

      publicUser(StringuserName, Stringpassword) {

      super();

      this.userName=userName;

      this.password=password;

      }

      publicInteger getUserId() {

      returnuserId;

      }

      publicvoidsetUserId(IntegeruserId) {

      this.userId=userId;

      }

      publicString getUserName() {

      returnuserName;

      }

      publicvoidsetUserName(StringuserName) {

      this.userName=userName;

      }

      publicString getPassword() {

      returnpassword;

      }

      publicvoidsetPassword(Stringpassword) {

      this.password=password;

      }

      publicInteger getUserType() {

      returnuserType;

      }

      publicvoidsetUserType(IntegeruserType) {

      this.userType=userType;

      }

      publicInteger getRoleId() {

      returnroleId;

      }

      publicvoidsetRoleId(IntegerroleId) {

      this.roleId=roleId;

      }

      publicString getUserDescription() {

      returnuserDescription;

      }

      publicvoidsetUserDescription(StringuserDescription) {

      this.userDescription=userDescription;

      }

      }

      dao类

      packagecom.xiaowen.dao;

      importjava.sql.Connection;

      importjava.sql.PreparedStatement;

      importjava.sql.ResultSet;

      importcom.xiaowen.model.User;

      /**

      * 用户的Dao数据层

      *@authorxiaowen

      */

      publicclassUserDao {

      /**

      * 用户登陆

      *@paramcon

      *@paramuser

      *@return

      *@throwsException

      */

      publicUserlogin(Connectioncon,Useruser)throwsException{

      UserresultUser=null;

      Stringsql="select * from t_user where userName=? and password=?";

      PreparedStatementpst=con.prepareStatement(sql);

      pst.setString(1,user.getUserName());

      pst.setString(2,user.getPassword());

      ResultSetrs=pst.executeQuery();

      if(rs.next()){

      resultUser=newUser();

      resultUser.setUserId(rs.getInt("userId"));

      resultUser.setUserName(rs.getString("userName"));

      resultUser.setPassword(rs.getString("password"));

      resultUser.setRoleId(rs.getInt("roleId"));

      }

      returnresultUser;

      }

      }

      工具类

      packagecom.xiaowen.util;

      importjava.sql.Connection;

      importjava.sql.DriverManager;

      /**

      * 数据库工具类

      *@authorxiaowen

      */

      publicclassDbUtil {

      privateStringdbUrl="jdbc:mysql://localhost:3306/db_rbps";

      privateStringdbUserName="root";

      privateStringdbPassword="123456";

      privateStringdbDriver="com.mysql.jdbc.Driver";

      /**

      * 数据库连接

      *@return

      *@throwsException

      */

      publicConnection getCon()throwsException{

      Class.forName(dbDriver);

      Connectioncon=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);

      returncon;

      }

      /**

      * 数据库关闭

      *@paramcon

      *@throwsException

      */

      publicvoidcloseCon(Connectioncon)throwsException{

      if(con!=null){

      con.close();

      }

      }

      publicstaticvoidmain(String[]args) {

      DbUtildbUtil=newDbUtil();

      try{

      dbUtil.getCon();

      System.out.println("数据库连接成功!");

      }catch(Exceptione) {

      e.printStackTrace();

      System.out.println("数据库连接失败!");

      }

      }

      }

      service层

      packagecom.xiaowen.server;

      importjava.io.IOException;

      importjava.sql.Connection;

      importjavax.servlet.ServletException;

      importjavax.servlet.http.HttpServlet;

      importjavax.servlet.http.HttpServletRequest;

      importjavax.servlet.http.HttpServletResponse;

      importjavax.servlet.http.HttpSession;

      importcom.xiaowen.dao.UserDao;

      importcom.xiaowen.model.User;

      importcom.xiaowen.util.DbUtil;

      importcom.xiaowen.util.StringUtil;

      /**

      * 用户Server处理层

      *@authorxiaowen

      */

      publicclassUserServerextendsHttpServlet{

      privatestaticfinallongserialVersionUID= 1L;

      DbUtildbUtil=newDbUtil();

      UserDaouserDao=newUserDao();

      Connectioncon=null;

      @Override

      publicvoidservice(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException, IOException {

      response.setCharacterEncoding("utf-8");

      Stringaction=request.getParameter("action");

      if("login".equals(action)){

      login(request,response);

      }

      }

      privatevoidlogin(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException, IOException {

      HttpSessionsession=request.getSession();

      StringuserName=request.getParameter("userName");

      Stringpassword=request.getParameter("password");

      StringimageCode=request.getParameter("imageCode");

      request.setAttribute("userName",userName);

      request.setAttribute("password",password);

      request.setAttribute("imageCode",imageCode);

      if(StringUtil.isEmpty(userName)||StringUtil.isEmpty(password)){

      request.setAttribute("error","用户名或密码错误!");

      request.getRequestDispatcher("login.jsp").forward(request,response);

      return;

      }

      if(StringUtil.isEmpty(imageCode)){

      request.setAttribute("error","验证码为空!");

      request.getRequestDispatcher("login.jsp").forward(request,response);

      return;

      }

      if(!imageCode.equals(session.getAttribute("sRand"))){

      request.setAttribute("error","验证码错误");

      request.getRequestDispatcher("login.jsp").forward(request,response);

      return;

      }

      Useruser=newUser(userName,password);

      try{

      con=dbUtil.getCon();

      Usercurrentuser=userDao.login(con,user);

      if(currentuser==null){

      request.setAttribute("error","用户名或密码错误!");

      request.getRequestDispatcher("login.jsp").forward(request,response);

      }else{

      session.setAttribute("currentuser",currentuser);

      response.sendRedirect("main.jsp");

      }

      }catch(Exceptione) {

      e.printStackTrace();

      }finally{

      try{

      dbUtil.closeCon(con);

      }catch(Exceptione) {

      e.printStackTrace();

      }

      }

      }

      }

      相关文章

        网友评论

          本文标题:基于角色权限系统(第一节)-用户登录实现

          本文链接:https://www.haomeiwen.com/subject/hroiuttx.html