Java实战角色权限后台脚手架系统的实现流程
程序员文章站
2022-06-24 17:59:51
springboot框架+mybaits-plus+mysql实现的角色权限后台管理脚手架系统实战项目,实现的是所有系统最基础的后台管理功能,有了这个脚手架,以后开发别的项目就在这上面快速增加功能即可...
springboot框架+mybaits-plus+mysql实现的角色权限后台管理脚手架系统实战项目,实现的是所有系统最基础的后台管理功能,有了这个脚手架,以后开发别的项目就在这上面快速增加功能即可。本系统主要实现的功能有:菜单管理、角色管理、用户管理、登录、登出,不同的角色分配相应的权限,用户分配不同的角色登录后显示相应被分配的功能。
本系统采用前后端分离的思想设计,后端采用springboot微服务架构,主要包含一个网关服务和一个权限管理服务,开发其他功能可以再创建其他的微服务,前端采用vue+element-ui实现。
1.本系统开发所需的基础环境及版本:
软件 | 版本 |
jdk | 1.8 |
mysql | 5.7 |
maven | 3.6.1 |
nacos | 1.1.4 |
redis | 5.0.10 |
node | 14.17.3 |
系统控制器:
/** * 系统控制器 * @author yy * */ @requestmapping("/system") @controller public class systemcontroller { @autowired private operaterlogservice operaterlogservice; @autowired private userservice userservice; @autowired private databasebakservice databasebakservice; @autowired private staffservice staffservice; @autowired private orderauthservice orderauthservice; private logger log = loggerfactory.getlogger(systemcontroller.class); /** * 登录页面 * @param model * @return */ @requestmapping(value="/login",method=requestmethod.get) public string login(model model){ model.addattribute("logintypes", logintype.values()); return "admin/system/login"; } /** * 用户登录提交表单处理方法 * @param request * @param cpacha * @return */ @requestmapping(value="/login",method=requestmethod.post) @responsebody public result<boolean> login(httpservletrequest request,string username,string password,string cpacha,integer type){ if(stringutils.isempty(username)){ return result.error(codemsg.admin_username_empty); } if(stringutils.isempty(password)){ return result.error(codemsg.admin_password_empty); } //表示实体信息合法,开始验证验证码是否为空 if(stringutils.isempty(cpacha)){ return result.error(codemsg.cpacha_empty); } //说明验证码不为空,从session里获取验证码 object attribute = request.getsession().getattribute("admin_login"); if(attribute == null){ return result.error(codemsg.session_expired); } //表示session未失效,进一步判断用户填写的验证码是否正确 if(!cpacha.equalsignorecase(attribute.tostring())){ return result.error(codemsg.cpacha_error); } if(type == logintype.administrator.getcode()){ //表示验证码正确,开始查询数据库,检验密码是否正确 user findbyusername = userservice.findbyusername(username); //判断是否为空 if(findbyusername == null){ return result.error(codemsg.admin_username_no_exist); } //表示用户存在,进一步对比密码是否正确 if(!findbyusername.getpassword().equals(password)){ return result.error(codemsg.admin_password_error); } //表示密码正确,接下来判断用户状态是否可用 if(findbyusername.getstatus() == user.admin_user_status_unable){ return result.error(codemsg.admin_user_unable); } //检查用户所属角色状态是否可用 if(findbyusername.getrole() == null || findbyusername.getrole().getstatus() == role.admin_role_status_unable){ return result.error(codemsg.admin_user_role_unable); } //检查用户所属角色的权限是否存在 if(findbyusername.getrole().getauthorities() == null || findbyusername.getrole().getauthorities().size() == 0){ return result.error(codemsg.admin_user_role_authorites_empty); } //检查一切符合,可以登录,将用户信息存放至session request.getsession().setattribute(sessionconstant.session_user_login_key, findbyusername); request.getsession().setattribute("logintype",type); //销毁session中的验证码 request.getsession().setattribute("admin_login", null); //将登陆记录写入日志库 operaterlogservice.add("用户【"+username+"】于【" + stringutil.getformatterdate(new date(), "yyyy-mm-dd hh:mm:ss") + "】登录系统!"); log.info("用户成功登录,user = " + findbyusername); }else{ staff byjobnumber = staffservice.findbynameandisstatus(username); //判断是否为空 if(byjobnumber == null){ return result.error(codemsg.admin_username_no_exist); } //表示用户存在,进一步对比密码是否正确 if(!byjobnumber.getpassword().equals(password)){ return result.error(codemsg.admin_password_error); } //检查用户所属角色状态是否可用 if(byjobnumber.getrole() == null || byjobnumber.getrole().getstatus() == role.admin_role_status_unable){ return result.error(codemsg.admin_user_role_unable); } //检查用户所属角色的权限是否存在 if(byjobnumber.getrole().getauthorities() == null || byjobnumber.getrole().getauthorities().size() == 0){ return result.error(codemsg.admin_user_role_authorites_empty); } //检查一切符合,可以登录,将用户信息存放至session request.getsession().setattribute(sessionconstant.session_staff_login_key, byjobnumber); request.getsession().setattribute("logintype",type); //销毁session中的验证码 request.getsession().setattribute("admin_login", null); //将登陆记录写入日志库 operaterlogservice.add("用户【"+username+"】于【" + stringutil.getformatterdate(new date(), "yyyy-mm-dd hh:mm:ss") + "】登录系统!"); log.info("员工成功登录,user = " + byjobnumber); } return result.success(true); } /** * 登录成功后的系统主页 * @param model * @return */ @requestmapping(value="/index") public string index(model model){ model.addattribute("operatorlogs", operaterlogservice.findlastestlog(10)); model.addattribute("usertotal", userservice.total()); model.addattribute("operatorlogtotal", operaterlogservice.total()); model.addattribute("databasebackuptotal", databasebakservice.total()); model.addattribute("onlineusertotal", sessionlistener.onlineusercount); return "admin/system/index"; } /** * 注销登录 * @return */ @requestmapping(value="/logout") public string logout(){ integer logintype = (integer) sessionutil.get("logintype"); if(logintype == logintype.administrator.getcode()){ user logineduser = sessionutil.getlogineduser(); if(logineduser != null){ sessionutil.set(sessionconstant.session_user_login_key, null); } }else if(logintype == logintype.staff.getcode()){ staff loginedstaff = sessionutil.getloginedstaff(); if(loginedstaff != null){ sessionutil.set(sessionconstant.session_staff_login_key,null); } } return "redirect:login"; } /** * 无权限提示页面 * @return */ @requestmapping(value="/no_right") public string noright(){ return "admin/system/no_right"; } /** * 修改用户个人信息 * @return */ @requestmapping(value="/update_userinfo",method=requestmethod.get) public string updateuserinfo(){ return "admin/system/update_userinfo"; } /** * 修改个人信息保存 * @param user * @return */ @requestmapping(value="/update_userinfo",method=requestmethod.post) public string updateuserinfo(user user){ user logineduser = sessionutil.getlogineduser(); logineduser.setemail(user.getemail()); logineduser.setmobile(user.getmobile()); logineduser.setheadpic(user.getheadpic()); //首先保存到数据库 userservice.save(logineduser); //更新session里的值 sessionutil.set(sessionconstant.session_user_login_key, logineduser); return "redirect:update_userinfo"; } /** * 修改密码页面 * @return */ @requestmapping(value="/update_pwd",method=requestmethod.get) public string updatepwd(){ return "admin/system/update_pwd"; } /** * 修改密码表单提交 * @param oldpwd * @param newpwd * @return */ @requestmapping(value="/update_pwd",method=requestmethod.post) @responsebody public result<boolean> updatepwd(@requestparam(name="oldpwd",required=true)string oldpwd, @requestparam(name="newpwd",required=true)string newpwd ){ integer logintype = (integer) sessionutil.get("logintype"); if(logintype == logintype.administrator.getcode()){ user logineduser = sessionutil.getlogineduser(); if(!logineduser.getpassword().equals(oldpwd)){ return result.error(codemsg.admin_user_update_pwd_error); } if(stringutils.isempty(newpwd)){ return result.error(codemsg.admin_user_update_pwd_empty); } if(newpwd.length()<4 || newpwd.length()>32){ return result.error(codemsg.admin_user_pwd_length_error); } logineduser.setpassword(newpwd); //保存数据库 userservice.save(logineduser); //更新session sessionutil.set(sessionconstant.session_user_login_key, logineduser); }else{ staff loginedstaff = sessionutil.getloginedstaff(); staff staff = staffservice.find(loginedstaff.getid()); if(!staff.getpassword().equals(oldpwd)){ return result.error(codemsg.admin_user_update_pwd_error); } if(stringutils.isempty(newpwd)){ return result.error(codemsg.admin_user_update_pwd_empty); } staff.setpassword(newpwd); codemsg codemsg = validateentityutil.validate(staff); if (codemsg.getcode() != codemsg.success.getcode()){ return result.error(codemsg); } loginedstaff.setpassword(newpwd); //保存数据库 staffservice.save(loginedstaff); //更新session sessionutil.set(sessionconstant.session_staff_login_key, loginedstaff); } return result.success(true); } /** * 日志管理列表 * @param model * @param operaterlog * @param pagebean * @return */ @requestmapping(value="/operator_log_list") public string operatorloglist(model model,operaterlog operaterlog,pagebean<operaterlog> pagebean){ model.addattribute("pagebean", operaterlogservice.findlist(operaterlog, pagebean)); model.addattribute("operator", operaterlog.getoperator()); model.addattribute("title", "日志列表"); return "admin/system/operator_log_list"; } /** * 验证订单 * @param ordersn * @param phone * @return */ @requestmapping(value="/auth_order",method=requestmethod.post) @responsebody public result<boolean> authorder(@requestparam(name="ordersn",required=true)string ordersn,@requestparam(name="phone",required=true)string phone){ orderauth orderauth = new orderauth(); orderauth.setmac(stringutil.getmac()); orderauth.setordersn(ordersn); orderauth.setphone(phone); orderauthservice.save(orderauth); appconfig.order_auth = 1; return result.success(true); } /** * 删除操作日志,可删除多个 * @param ids * @return */ @requestmapping(value="/delete_operator_log",method=requestmethod.post) @responsebody public result<boolean> delete(string ids){ if(!stringutils.isempty(ids)){ string[] splitids = ids.split(","); for(string id : splitids){ operaterlogservice.delete(long.valueof(id)); } } return result.success(true); } /** * 清空整个日志 * @return */ @requestmapping(value="/delete_all_operator_log",method=requestmethod.post) @responsebody public result<boolean> deleteall(){ operaterlogservice.deleteall(); return result.success(true); } }
后台用户管理控制器:
/** * 后台用户管理控制器 * @author yy * */ @requestmapping("/admin/user") @controller public class usercontroller { @autowired private userservice userservice; @autowired private roleservice roleservice; @autowired private operaterlogservice operaterlogservice; /** * 用户列表页面 * @param model * @param user * @param pagebean * @return */ @requestmapping(value="/list") public string list(model model,user user,pagebean<user> pagebean){ model.addattribute("title", "用户列表"); model.addattribute("username", user.getusername()); model.addattribute("pagebean", userservice.findlist(user, pagebean)); return "admin/user/list"; } /** * 新增用户页面 * @param model * @return */ @requestmapping(value="/add",method=requestmethod.get) public string add(model model){ model.addattribute("roles", roleservice.findall()); return "admin/user/add"; } /** * 用户添加表单提交处理 * @param user * @return */ @requestmapping(value="/add",method=requestmethod.post) @responsebody public result<boolean> add(user user){ //用统一验证实体方法验证是否合法 codemsg validate = validateentityutil.validate(user); if(validate.getcode() != codemsg.success.getcode()){ return result.error(validate); } if(user.getrole() == null || user.getrole().getid() == null){ return result.error(codemsg.admin_user_role_empty); } //判断用户名是否存在 if(userservice.isexistusername(user.getusername(), 0l)){ return result.error(codemsg.admin_username_exist); } //到这说明一切符合条件,进行数据库新增 if(userservice.save(user) == null){ return result.error(codemsg.admin_use_add_error); } operaterlogservice.add("添加用户,用户名:" + user.getusername()); return result.success(true); } /** * 用户编辑页面 * @param model * @return */ @requestmapping(value="/edit",method=requestmethod.get) public string edit(model model,@requestparam(name="id",required=true)long id){ model.addattribute("roles", roleservice.findall()); model.addattribute("user", userservice.find(id)); return "admin/user/edit"; } /** * 编辑用户信息表单提交处理 * @param user * @return */ @requestmapping(value="/edit",method=requestmethod.post) @responsebody public result<boolean> edit(user user){ //用统一验证实体方法验证是否合法 codemsg validate = validateentityutil.validate(user); if(validate.getcode() != codemsg.success.getcode()){ return result.error(validate); } if(user.getrole() == null || user.getrole().getid() == null){ return result.error(codemsg.admin_user_role_empty); } if(user.getid() == null || user.getid().longvalue() <= 0){ return result.error(codemsg.admin_use_no_exist); } if(userservice.isexistusername(user.getusername(), user.getid())){ return result.error(codemsg.admin_username_exist); } //到这说明一切符合条件,进行数据库保存 user findbyid = userservice.find(user.getid()); //讲提交的用户信息指定字段复制到已存在的user对象中,该方法会覆盖新字段内容 beanutils.copyproperties(user, findbyid, "id","createtime","updatetime"); if(userservice.save(findbyid) == null){ return result.error(codemsg.admin_use_edit_error); } operaterlogservice.add("编辑用户,用户名:" + user.getusername()); return result.success(true); } /** * 删除用户 * @param id * @return */ @requestmapping(value="/delete",method=requestmethod.post) @responsebody public result<boolean> delete(@requestparam(name="id",required=true)long id){ try { userservice.delete(id); } catch (exception e) { return result.error(codemsg.admin_use_delete_error); } operaterlogservice.add("删除用户,用户id:" + id); return result.success(true); } }
后台角色管理控制器:
/** * 后台角色管理控制器 * @author yy * */ @requestmapping("/admin/role") @controller public class rolecontroller { private logger log = loggerfactory.getlogger(rolecontroller.class); @autowired private menuservice menuservice; @autowired private operaterlogservice operaterlogservice; @autowired private roleservice roleservice; /** * 分页搜索角色列表 * @param model * @param role * @param pagebean * @return */ @requestmapping(value="/list") public string list(model model,role role,pagebean<role> pagebean){ model.addattribute("title", "角色列表"); model.addattribute("name", role.getname()); model.addattribute("pagebean", roleservice.findbyname(role, pagebean)); return "admin/role/list"; } /** * 角色添加页面 * @param model * @return */ @requestmapping(value="/add",method=requestmethod.get) public string add(model model){ list<menu> findall = menuservice.findall(); model.addattribute("topmenus",menuutil.gettopmenus(findall)); model.addattribute("secondmenus",menuutil.getsecondmenus(findall)); model.addattribute("thirdmenus",menuutil.getthirdmenus(findall)); return "admin/role/add"; } /** * 角色添加表单提交处理 * @param role * @return */ @requestmapping(value="/add",method=requestmethod.post) @responsebody public result<boolean> add(role role){ //用统一验证实体方法验证是否合法 codemsg validate = validateentityutil.validate(role); if(validate.getcode() != codemsg.success.getcode()){ return result.error(validate); } if(roleservice.save(role) == null){ return result.error(codemsg.admin_role_add_error); } log.info("添加角色【"+role+"】"); operaterlogservice.add("添加角色【"+role.getname()+"】"); return result.success(true); } /** * 角色编辑页面 * @param id * @param model * @return */ @requestmapping(value="/edit",method=requestmethod.get) public string edit(@requestparam(name="id",required=true)long id,model model){ list<menu> findall = menuservice.findall(); model.addattribute("topmenus",menuutil.gettopmenus(findall)); model.addattribute("secondmenus",menuutil.getsecondmenus(findall)); model.addattribute("thirdmenus",menuutil.getthirdmenus(findall)); role role = roleservice.find(id); model.addattribute("role", role); model.addattribute("authorities",jsonarray.tojson(role.getauthorities()).tostring()); return "admin/role/edit"; } /** * 角色修改表单提交处理 * @param request * @param role * @return */ @requestmapping(value="/edit",method=requestmethod.post) @responsebody public result<boolean> edit(role role){ //用统一验证实体方法验证是否合法 codemsg validate = validateentityutil.validate(role); if(validate.getcode() != codemsg.success.getcode()){ return result.error(validate); } role existrole = roleservice.find(role.getid()); if(existrole == null){ return result.error(codemsg.admin_role_no_exist); } existrole.setname(role.getname()); existrole.setremark(role.getremark()); existrole.setstatus(role.getstatus()); existrole.setauthorities(role.getauthorities()); if(roleservice.save(existrole) == null){ return result.error(codemsg.admin_role_edit_error); } log.info("编辑角色【"+role+"】"); operaterlogservice.add("编辑角色【"+role.getname()+"】"); return result.success(true); } /** * 删除角色 * @param request * @param id * @return */ @requestmapping(value="delete",method=requestmethod.post) @responsebody public result<boolean> delete(@requestparam(name="id",required=true)long id){ try { roleservice.delete(id); } catch (exception e) { // todo: handle exception return result.error(codemsg.admin_role_delete_error); } log.info("编辑角色id【"+id+"】"); operaterlogservice.add("删除角色id【"+id+"】"); return result.success(true); } }
到此这篇关于java实战角色权限后台脚手架系统的实现流程的文章就介绍到这了,更多相关java 角色权限后台脚手架系统内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!