From e747135d1fb4d3f17a380660bc12f901f8a6d3f9 Mon Sep 17 00:00:00 2001 From: iamsongpeng Date: Fri, 6 Mar 2020 09:09:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E5=BD=A2=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +- .../sparchetype/common/enums/CommonEnum.java | 45 +++++ .../util/RandomVerificationCodeUtil.java | 152 ++++++++++++++++ .../system/config/shiro/ShiroConfig.java | 2 +- .../admin/SysDepartmentController.java | 8 +- .../controller/admin/SysDictController.java | 8 +- .../controller/admin/SysLoginController.java | 3 - .../controller/admin/SysMenuController.java | 6 +- .../controller/admin/SysRoleController.java | 57 +++--- .../controller/admin/SysUserController.java | 1 - .../controller/client/SysLoginController.java | 169 +++++++++++------- .../system/request/SysDepartmentPageReq.java | 15 ++ .../system/request/SysDictPageReq.java | 15 ++ .../system/service/ISysRoleService.java | 9 + .../system/service/ISysUserService.java | 2 - .../service/impl/SysDictServiceImpl.java | 4 +- .../service/impl/SysMenuServiceImpl.java | 166 ++++++++--------- .../service/impl/SysRoleMenuServiceImpl.java | 2 - .../service/impl/SysRoleServiceImpl.java | 37 +++- .../service/impl/SysUserRoleServiceImpl.java | 2 - .../service/impl/SysUserServiceImpl.java | 38 +--- .../src/main/resources/templates/login.ftl | 24 ++- 22 files changed, 527 insertions(+), 246 deletions(-) create mode 100644 sparchetype/src/main/java/com/songpeng/sparchetype/common/enums/CommonEnum.java create mode 100644 sparchetype/src/main/java/com/songpeng/sparchetype/common/util/RandomVerificationCodeUtil.java create mode 100644 sparchetype/src/main/java/com/songpeng/sparchetype/system/request/SysDepartmentPageReq.java create mode 100644 sparchetype/src/main/java/com/songpeng/sparchetype/system/request/SysDictPageReq.java diff --git a/README.md b/README.md index 3a67cc0..6add1ff 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,13 @@ * http://www.fontawesome.com.cn/faicons/ * 后端 * Hutool 是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。 - + * 枚举 + * 公共枚举类:com.songpeng.sparchetype.common.enums.CommonEnum + * 不同模块下的枚举:如 com.songpeng.sparchetype.system.enums + * 请求参数 + * 每张表的分页查询参数,严格按照一张表对应一个请求参数对象进行开发,如系统用户分页查询参数:com.songpeng.sparchetype.system.request.SysUserPageReq + * + #### 安装教程 1. xxxx diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/common/enums/CommonEnum.java b/sparchetype/src/main/java/com/songpeng/sparchetype/common/enums/CommonEnum.java new file mode 100644 index 0000000..1a9dbf1 --- /dev/null +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/common/enums/CommonEnum.java @@ -0,0 +1,45 @@ +package com.songpeng.sparchetype.common.enums; + +/** + * 系统用户枚举 + * + * @author SongPeng + * @date 2019/10/17 9:45 + */ +public enum CommonEnum { + + /** + * 类型:0 目录;1 菜单;2 按钮 + */ + FIELD_NAME_IS_DELETED("is_deleted", "数据库删除标记字段名"), + + FIELD_NAME_CREATE_TIME("create_time", "数据库创建时间字段名"), + + FIELD_NAME_CREATE_USERNAME("create_username", "数据库创建用户名字段名"), + + FIELD_NAME_UPDATE_TIME("update_time", "数据库更新时间字段名"), + + FIELD_NAME_UPDATE_USERNAME("update_username", "数据库更新用户名字段名"); + + /** + * The Code + */ + String code; + /** + * The Desc + */ + String desc; + + public String getCode() { + return code; + } + + public String getDesc() { + return desc; + } + + CommonEnum(String code, String desc) { + this.code = code; + this.desc = desc; + } +} diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/common/util/RandomVerificationCodeUtil.java b/sparchetype/src/main/java/com/songpeng/sparchetype/common/util/RandomVerificationCodeUtil.java new file mode 100644 index 0000000..02d4106 --- /dev/null +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/common/util/RandomVerificationCodeUtil.java @@ -0,0 +1,152 @@ +package com.songpeng.sparchetype.common.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.imageio.ImageIO; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +/** + * 随机验证码生成工具 + * + * @author SongPeng + * @date 2020/3/6 + */ +public class RandomVerificationCodeUtil { + + private static final Logger log = LoggerFactory.getLogger(RandomVerificationCodeUtil.class); + + /** + * 放到session中的key + */ + public static final String RANDOM_CODE_KEY = "RANDOMVALIDATECODEKEY"; + /** + * 字体名称 + */ + public static final String FONT_NAME = "Fixedsys"; + + /** + * 随机产生只有数字的字符串 + */ + private String randString = "0123456789"; + + /** + * 图片宽 + */ + private int width = 95; + + /** + * 图片高 + */ + private int height = 25; + + /** + * 干扰线数量 + */ + private int lineSize = 40; + + /** + * 随机产生字符数量 + */ + private int stringNum = 4; + + private Random random = new Random(); + + /** + * 获得字体 + */ + private Font getFont() { + return new Font(FONT_NAME, Font.CENTER_BASELINE, 18); + } + + /** + * 获得颜色 + */ + private Color getRandColor(int fc, int bc) { + if (fc > 255) { + fc = 255; + } + if (bc > 255) { + bc = 255; + } + int r = fc + random.nextInt(bc - fc - 16); + int g = fc + random.nextInt(bc - fc - 14); + int b = fc + random.nextInt(bc - fc - 18); + return new Color(r, g, b); + } + + /** + * 生成随机图片 + */ + public void getRandcode(HttpServletRequest request, HttpServletResponse response) { + HttpSession session = request.getSession(); + // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类 + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); + // 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作 + Graphics g = image.getGraphics(); + // 图片大小 + g.fillRect(0, 0, width, height); + // 字体大小 + g.setFont(new Font("Default", Font.ROMAN_BASELINE, 18)); + // 字体颜色 + g.setColor(getRandColor(110, 133)); + // 绘制干扰线 + for (int i = 0; i <= lineSize; i++) { + drowLine(g); + } + // 绘制随机字符 + String randomString = ""; + for (int i = 1; i <= stringNum; i++) { + randomString = drowString(g, randomString, i); + } + log.info(randomString); + // 将生成的随机字符串保存到session中 + session.removeAttribute(RANDOM_CODE_KEY); + session.setAttribute(RANDOM_CODE_KEY, randomString); + g.dispose(); + try { + // 将内存中的图片通过流动形式输出到客户端 + ImageIO.write(image, "JPEG", response.getOutputStream()); + } catch (Exception e) { + log.error("将内存中的图片通过流形式输出到客户端失败", e); + } + } + + /** + * 绘制字符串 + */ + private String drowString(Graphics g, String randomString, int i) { + g.setFont(getFont()); + g.setColor(new Color(random.nextInt(101), random.nextInt(111), random + .nextInt(121))); + String rand = String.valueOf(getRandomString(random.nextInt(randString + .length()))); + randomString += rand; + g.translate(random.nextInt(3), random.nextInt(3)); + g.drawString(rand, 13 * i, 16); + return randomString; + } + + /** + * 绘制干扰线 + */ + private void drowLine(Graphics g) { + int x = random.nextInt(width); + int y = random.nextInt(height); + int xl = random.nextInt(13); + int yl = random.nextInt(15); + g.drawLine(x, y, x + xl, y + yl); + } + + /** + * 获取随机的字符 + */ + public String getRandomString(int num) { + return String.valueOf(randString.charAt(num)); + } +} diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/config/shiro/ShiroConfig.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/config/shiro/ShiroConfig.java index 3ea7188..ade85c9 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/config/shiro/ShiroConfig.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/config/shiro/ShiroConfig.java @@ -80,7 +80,7 @@ public class ShiroConfig { shiroFilterFactoryBean.setUnauthorizedUrl("/403"); LinkedHashMap filterChainDefinitionMap = new LinkedHashMap<>(); filterChainDefinitionMap.put("/login", "anon"); - filterChainDefinitionMap.put("/getVerify", "anon"); + filterChainDefinitionMap.put("/verification/code", "anon"); filterChainDefinitionMap.put("/css/**", "anon"); filterChainDefinitionMap.put("/image/**", "anon"); filterChainDefinitionMap.put("/js/**", "anon"); diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysDepartmentController.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysDepartmentController.java index 93fce91..07e3cbd 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysDepartmentController.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysDepartmentController.java @@ -2,11 +2,11 @@ package com.songpeng.sparchetype.system.controller.admin; import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.songpeng.sparchetype.common.BaseController; import com.songpeng.sparchetype.common.Result; import com.songpeng.sparchetype.system.entity.SysDepartment; import com.songpeng.sparchetype.system.entity.SysDict; +import com.songpeng.sparchetype.system.request.SysDepartmentPageReq; import com.songpeng.sparchetype.system.service.ISysDepartmentService; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -46,10 +46,12 @@ public class SysDepartmentController extends BaseController { return "admin/system/department/list"; } + @ApiOperation("系统部门信息分页列表") + @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "模型", defaultValue = "模型")}) @PostMapping("/page") @ResponseBody - public Result page(Page page) { - IPage result = sysDepartmentService.page(page); + public Result page(SysDepartmentPageReq req) { + IPage result = sysDepartmentService.page(req); return Result.success(result); } diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysDictController.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysDictController.java index ad1516f..dfebe8a 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysDictController.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysDictController.java @@ -2,10 +2,10 @@ package com.songpeng.sparchetype.system.controller.admin; import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.songpeng.sparchetype.common.BaseController; import com.songpeng.sparchetype.common.Result; import com.songpeng.sparchetype.system.entity.SysDict; +import com.songpeng.sparchetype.system.request.SysDictPageReq; import com.songpeng.sparchetype.system.service.ISysDictService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -41,7 +41,7 @@ public class SysDictController extends BaseController { private ISysDictService sysDictService; @ApiOperation("系统字典信息列表UI") - @ApiImplicitParams({@ApiImplicitParam(name ="model", value = "模型", defaultValue = "模型")}) + @ApiImplicitParams({@ApiImplicitParam(name = "model", value = "模型", defaultValue = "模型")}) @GetMapping("/list-ui") public String listUI(Model model) { return "admin/system/dict/list"; @@ -49,8 +49,8 @@ public class SysDictController extends BaseController { @PostMapping("/page") @ResponseBody - public Result page(Page page) { - IPage result = sysDictService.page(page); + public Result page(SysDictPageReq req) { + IPage result = sysDictService.page(req); return Result.success(result); } diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysLoginController.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysLoginController.java index a9b090d..c5a211b 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysLoginController.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysLoginController.java @@ -1,9 +1,7 @@ package com.songpeng.sparchetype.system.controller.admin; import com.songpeng.sparchetype.common.Result; -import com.songpeng.sparchetype.system.entity.SysMenu; import com.songpeng.sparchetype.system.service.ISysMenuService; -import com.songpeng.sparchetype.system.vo.TreeVO; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +12,6 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; -import java.util.List; import java.util.Map; /** diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysMenuController.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysMenuController.java index c53d56a..bc86b8b 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysMenuController.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysMenuController.java @@ -2,10 +2,10 @@ package com.songpeng.sparchetype.system.controller.admin; import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.songpeng.sparchetype.common.BaseController; import com.songpeng.sparchetype.common.Result; import com.songpeng.sparchetype.system.entity.SysMenu; +import com.songpeng.sparchetype.system.request.SysMenuPageReq; import com.songpeng.sparchetype.system.service.ISysMenuService; import com.songpeng.sparchetype.system.vo.TreeVO; import io.swagger.annotations.ApiOperation; @@ -42,8 +42,8 @@ public class SysMenuController extends BaseController { @PostMapping("/page") @ResponseBody - public Result page(Page page) { - IPage result = sysMenuService.page(page); + public Result page(SysMenuPageReq req) { + IPage result = sysMenuService.page(req); return Result.success(result); } diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysRoleController.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysRoleController.java index 067ced5..0a04bff 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysRoleController.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysRoleController.java @@ -3,7 +3,6 @@ package com.songpeng.sparchetype.system.controller.admin; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.songpeng.sparchetype.common.BaseController; import com.songpeng.sparchetype.common.Result; import com.songpeng.sparchetype.system.entity.SysRole; @@ -30,36 +29,36 @@ import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping("/admin/sys/role") public class SysRoleController extends BaseController { - @Autowired - private ISysRoleService sysRoleService; + @Autowired + private ISysRoleService sysRoleService; - @GetMapping("/list-ui") - public String listUI(Model model) { - return "admin/system/role/list"; - } + @GetMapping("/list-ui") + public String listUI(Model model) { + return "admin/system/role/list"; + } - @PostMapping("/page") - @ResponseBody - public Result page(Page page, SysRolePageReq req) { - QueryWrapper qw = new QueryWrapper(); - qw.orderByDesc(req.getOrderBy()); - IPage result = sysRoleService.page(page, qw); - return Result.success(result); - } + @PostMapping("/page") + @ResponseBody + public Result page(SysRolePageReq req) { + QueryWrapper qw = new QueryWrapper(); + qw.orderByDesc(req.getOrderBy()); + IPage result = sysRoleService.page(req, qw); + return Result.success(result); + } - @GetMapping("/add-or-update-ui") - public String addOrUpdateUI(Model model, SysRole record) { - if (StringUtils.isNotEmpty(record.getId())) { - SysRole result = sysRoleService.getById(record.getId()); - model.addAttribute("result", result); - } - return "admin/system/role/addOrUpdate"; - } + @GetMapping("/add-or-update-ui") + public String addOrUpdateUI(Model model, SysRole record) { + if (StringUtils.isNotEmpty(record.getId())) { + SysRole result = sysRoleService.getById(record.getId()); + model.addAttribute("result", result); + } + return "admin/system/role/addOrUpdate"; + } - @PostMapping("/add-or-update") - @ResponseBody - public Result addOrUpdate(SysRole record) { - sysRoleService.saveOrUpdate(record); - return Result.success(record.getId()); - } + @PostMapping("/add-or-update") + @ResponseBody + public Result addOrUpdate(SysRole record) { + sysRoleService.saveOrUpdate(record); + return Result.success(record.getId()); + } } diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysUserController.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysUserController.java index f2afcba..838a1f7 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysUserController.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/admin/SysUserController.java @@ -1,6 +1,5 @@ package com.songpeng.sparchetype.system.controller.admin; - import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.songpeng.sparchetype.common.BaseController; diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/client/SysLoginController.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/client/SysLoginController.java index 63ca2e9..0d9bbcc 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/client/SysLoginController.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/controller/client/SysLoginController.java @@ -1,7 +1,9 @@ package com.songpeng.sparchetype.system.controller.client; import com.songpeng.sparchetype.common.Result; +import com.songpeng.sparchetype.common.util.RandomVerificationCodeUtil; import com.songpeng.sparchetype.system.config.shiro.SpUsernamePasswordToken; +import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.*; import org.apache.shiro.crypto.hash.Md5Hash; @@ -15,86 +17,115 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** - * - * * @author SongPeng * @date 2019/9/27 16:05 */ @Controller("clientSysLoginController") public class SysLoginController { - Logger log = LoggerFactory.getLogger(SysLoginController.class); + Logger log = LoggerFactory.getLogger(SysLoginController.class); - /** - * 首页默认跳转到博客主页 - * - * @param model - * @return - */ - @GetMapping({"/", ""}) - public String welcomeUI(Model model) { - return "redirect:/client/company"; - } + /** + * 首页默认跳转到博客主页 + * + * @param model + * @return + */ + @GetMapping({"/", ""}) + public String welcomeUI(Model model) { + return "redirect:/client/company"; + } - /** - * 登录页面 - * - * @param model - * @return - */ - @GetMapping("/login-ui") - public String loginUI(Model model) { - return "login"; - } + /** + * 生成验证码 + */ + @GetMapping(value = "/verification/code") + public void getVerify(HttpServletRequest request, HttpServletResponse response) { + try { + // 设置相应类型,告诉浏览器输出的内容为图片 + response.setContentType("image/jpeg"); + // 设置响应头信息,告诉浏览器不要缓存此内容 + response.setHeader("Pragma", "No-cache"); + response.setHeader("Cache-Control", "no-cache"); + response.setDateHeader("Expire", 0); + RandomVerificationCodeUtil randomValidateCode = new RandomVerificationCodeUtil(); + // 输出验证码图片方法 + randomValidateCode.getRandcode(request, response); + } catch (Exception e) { + log.error("获取验证码失败", e); + } + } - @PostMapping("/login") - @ResponseBody - public Result login(String username, String password, String captcha, String rememberMe, HttpServletRequest request) { - // TODO loginType 字段用于后期拓展用 - UsernamePasswordToken token = new SpUsernamePasswordToken(username, password, "UserLogin"); - Subject subject = SecurityUtils.getSubject(); - try { - subject.login(token); - return Result.success(); - } catch (LockedAccountException e) { - log.error("锁定的帐号", e); - return Result.failure("锁定的帐号"); - } catch (DisabledAccountException e) { - log.error("禁用的帐号", e); - return Result.failure("禁用的帐号"); - } catch (UnknownAccountException e) { - log.error("错误的帐号", e); - return Result.failure("错误的帐号"); - } catch (ExcessiveAttemptsException e) { - log.error("登录失败次数过多", e); - return Result.failure("登录失败次数过多"); - }catch (IncorrectCredentialsException e) { - log.error("错误的凭证", e); - return Result.failure("错误的凭证"); - }catch (ExpiredCredentialsException e) { - log.error("过期的凭证", e); - return Result.failure("过期的凭证"); - }catch (AuthenticationException e) { - log.error("用户或密码错误", e); - return Result.failure("用户或密码错误"); - } - } + /** + * 登录页面 + * + * @param model + * @return + */ + @GetMapping("/login-ui") + public String loginUI(Model model) { + return "login"; + } - /** - * 404页面 - * - * @param model - * @return - */ - @GetMapping("/404-ui") - public String error404UI(Model model) { - return "404"; - } + @PostMapping("/login") + @ResponseBody + public Result login(String username, String password, String captcha, String rememberMe, HttpServletRequest request) { + //从session中获取随机数 + String random = (String) request.getSession().getAttribute(RandomVerificationCodeUtil.RANDOM_CODE_KEY); + if (StringUtils.isBlank(captcha)) { + return Result.failure("请输入验证码"); + } - public static void main(String[] args) { - String pwd = new Md5Hash("123", "admin", 3).toString(); - System.out.println(pwd); - } + if (!random.equals(captcha)) { + return Result.failure("请输入正确的验证码"); + } + + // TODO loginType 字段用于后期拓展用 + UsernamePasswordToken token = new SpUsernamePasswordToken(username, password, "UserLogin"); + Subject subject = SecurityUtils.getSubject(); + try { + subject.login(token); + return Result.success(); + } catch (LockedAccountException e) { + log.error("锁定的帐号", e); + return Result.failure("锁定的帐号"); + } catch (DisabledAccountException e) { + log.error("禁用的帐号", e); + return Result.failure("禁用的帐号"); + } catch (UnknownAccountException e) { + log.error("错误的帐号", e); + return Result.failure("错误的帐号"); + } catch (ExcessiveAttemptsException e) { + log.error("登录失败次数过多", e); + return Result.failure("登录失败次数过多"); + } catch (IncorrectCredentialsException e) { + log.error("错误的凭证", e); + return Result.failure("错误的凭证"); + } catch (ExpiredCredentialsException e) { + log.error("过期的凭证", e); + return Result.failure("过期的凭证"); + } catch (AuthenticationException e) { + log.error("用户或密码错误", e); + return Result.failure("用户或密码错误"); + } + } + + /** + * 404页面 + * + * @param model + * @return + */ + @GetMapping("/404-ui") + public String error404UI(Model model) { + return "404"; + } + + public static void main(String[] args) { + String pwd = new Md5Hash("123", "admin", 3).toString(); + System.out.println(pwd); + } } diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/request/SysDepartmentPageReq.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/request/SysDepartmentPageReq.java new file mode 100644 index 0000000..7088560 --- /dev/null +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/request/SysDepartmentPageReq.java @@ -0,0 +1,15 @@ +package com.songpeng.sparchetype.system.request; + +import com.songpeng.sparchetype.common.BasePageReq; + +/** + *

+ * 系统部门分页查询参数 + *

+ * + * @author SongPeng + * @since 2019-10-15 + */ +public class SysDepartmentPageReq extends BasePageReq { + +} diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/request/SysDictPageReq.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/request/SysDictPageReq.java new file mode 100644 index 0000000..73ce072 --- /dev/null +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/request/SysDictPageReq.java @@ -0,0 +1,15 @@ +package com.songpeng.sparchetype.system.request; + +import com.songpeng.sparchetype.common.BasePageReq; + +/** + *

+ * 系统部门分页查询参数 + *

+ * + * @author SongPeng + * @since 2019-10-15 + */ +public class SysDictPageReq extends BasePageReq { + +} diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/ISysRoleService.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/ISysRoleService.java index c07a5e1..b2bfdab 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/ISysRoleService.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/ISysRoleService.java @@ -3,6 +3,7 @@ package com.songpeng.sparchetype.system.service; import com.baomidou.mybatisplus.extension.service.IService; import com.songpeng.sparchetype.system.dto.SysRoleDTO; +import com.songpeng.sparchetype.system.dto.SysUserDTO; import com.songpeng.sparchetype.system.entity.SysRole; import java.util.List; @@ -25,4 +26,12 @@ public interface ISysRoleService extends IService { * @throws Exception 异常 */ List listByUserId(String userId) throws Exception; + + /** + * 重新建立用户角色关系 + * + * @param sysUserDTO 系统用户DTO + * @throws Exception 异常 + */ + void rebuild(SysUserDTO sysUserDTO) throws Exception; } diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/ISysUserService.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/ISysUserService.java index b70be45..788fe2c 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/ISysUserService.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/ISysUserService.java @@ -1,10 +1,8 @@ package com.songpeng.sparchetype.system.service; -import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.songpeng.sparchetype.system.dto.SysUserDTO; import com.songpeng.sparchetype.system.entity.SysUser; -import com.songpeng.sparchetype.system.request.SysUserPageReq; /** *

diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysDictServiceImpl.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysDictServiceImpl.java index 54f5058..3b17d13 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysDictServiceImpl.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysDictServiceImpl.java @@ -18,6 +18,6 @@ import org.springframework.stereotype.Service; @Service public class SysDictServiceImpl extends ServiceImpl implements ISysDictService { - @Autowired - private SysDictMapper sysDictMapper; + @Autowired + private SysDictMapper sysDictMapper; } diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysMenuServiceImpl.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysMenuServiceImpl.java index 7d45a16..c632dc6 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysMenuServiceImpl.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysMenuServiceImpl.java @@ -23,98 +23,98 @@ import java.util.*; @Service public class SysMenuServiceImpl extends ServiceImpl implements ISysMenuService { - @Autowired - private SysMenuMapper sysMenuMapper; + @Autowired + private SysMenuMapper sysMenuMapper; - /** - * 根据角色id查询菜单列表 - * - * @param roleId - * @return - * @throws Exception - */ - @Override - public List listByRoleId(String roleId) throws Exception { - return sysMenuMapper.listByRoleId(roleId); - } + /** + * 根据角色id查询菜单列表 + * + * @param roleId + * @return + * @throws Exception + */ + @Override + public List listByRoleId(String roleId) throws Exception { + return sysMenuMapper.listByRoleId(roleId); + } - /** - * 系统首页初始化菜单树数据 - * - * @return 系统首页初始化菜单树数据 - * @throws Exception 异常 - */ - @Override - public Map listIndexMenuTree() throws Exception { - Map result = new LinkedHashMap<>(4); + /** + * 系统首页初始化菜单树数据 + * + * @return 系统首页初始化菜单树数据 + * @throws Exception 异常 + */ + @Override + public Map listIndexMenuTree() throws Exception { + Map result = new LinkedHashMap<>(4); - List sysMenus = sysMenuMapper.selectList(null); + List sysMenus = sysMenuMapper.selectList(null); - Map clearInfo = new HashMap<>(2); - clearInfo.put("clearUrl", "json/clear.json"); + Map clearInfo = new HashMap<>(2); + clearInfo.put("clearUrl", "json/clear.json"); - Map homeInfo = new HashMap<>(4); - homeInfo.put("name", "首页"); - homeInfo.put("icon", "fa fa-home"); - homeInfo.put("url", "admin/welcome-ui"); + Map homeInfo = new HashMap<>(4); + homeInfo.put("name", "首页"); + homeInfo.put("icon", "fa fa-home"); + homeInfo.put("url", "admin/welcome-ui"); - Map logoInfo = new HashMap<>(4); - logoInfo.put("name", "后台管理"); - logoInfo.put("image", "image/logo.png"); - logoInfo.put("url", ""); + Map logoInfo = new HashMap<>(4); + logoInfo.put("name", "后台管理"); + logoInfo.put("image", "image/logo.png"); + logoInfo.put("url", ""); - Map menuInfo = new LinkedHashMap<>(8); + Map menuInfo = new LinkedHashMap<>(8); - List> menus = new ArrayList<>(); - for (SysMenu m : sysMenus) { - TreeVO tree = new TreeVO<>(); - tree.setId(m.getId()); - tree.setPid(m.getParentId()); - tree.setCode(m.getCode()); - tree.setName(m.getName()); - tree.setUrl(m.getUrl()); - tree.setIcon(m.getIcon()); - tree.setType(m.getType()); - tree.setPermission(m.getPermission()); - // TODO 是否需要更改? - tree.setTarget("_self"); - menus.add(tree); - } - List> treeVOS = TreeUtil.buildList(menus, "0"); - for (TreeVO mTree : treeVOS) { - menuInfo.put(mTree.getCode(), mTree); - } + List> menus = new ArrayList<>(); + for (SysMenu m : sysMenus) { + TreeVO tree = new TreeVO<>(); + tree.setId(m.getId()); + tree.setPid(m.getParentId()); + tree.setCode(m.getCode()); + tree.setName(m.getName()); + tree.setUrl(m.getUrl()); + tree.setIcon(m.getIcon()); + tree.setType(m.getType()); + tree.setPermission(m.getPermission()); + // TODO 是否需要更改? + tree.setTarget("_self"); + menus.add(tree); + } + List> treeVOS = TreeUtil.buildList(menus, "0"); + for (TreeVO mTree : treeVOS) { + menuInfo.put(mTree.getCode(), mTree); + } - result.put("clearInfo", clearInfo); - result.put("homeInfo", homeInfo); - result.put("logoInfo", logoInfo); - result.put("menuInfo", menuInfo); + result.put("clearInfo", clearInfo); + result.put("homeInfo", homeInfo); + result.put("logoInfo", logoInfo); + result.put("menuInfo", menuInfo); - return result; - } + return result; + } - /** - * 获取系统菜单树 - * - * @return 系统菜单树 - * @throws Exception 异常 - */ - @Override - public List> listMenuTree() throws Exception { - List> menus = new ArrayList<>(); - List sysMenus = sysMenuMapper.selectList(null); - for (SysMenu m : sysMenus) { - TreeVO tree = new TreeVO<>(); - tree.setId(m.getId()); - tree.setPid(m.getParentId()); - tree.setCode(m.getCode()); - tree.setName(m.getName()); - tree.setUrl(m.getUrl()); - tree.setIcon(m.getIcon()); - tree.setType(m.getType()); - tree.setPermission(m.getPermission()); - menus.add(tree); - } - return TreeUtil.buildList(menus, "0"); - } + /** + * 获取系统菜单树 + * + * @return 系统菜单树 + * @throws Exception 异常 + */ + @Override + public List> listMenuTree() throws Exception { + List> menus = new ArrayList<>(); + List sysMenus = sysMenuMapper.selectList(null); + for (SysMenu m : sysMenus) { + TreeVO tree = new TreeVO<>(); + tree.setId(m.getId()); + tree.setPid(m.getParentId()); + tree.setCode(m.getCode()); + tree.setName(m.getName()); + tree.setUrl(m.getUrl()); + tree.setIcon(m.getIcon()); + tree.setType(m.getType()); + tree.setPermission(m.getPermission()); + menus.add(tree); + } + return TreeUtil.buildList(menus, "0"); + } } diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysRoleMenuServiceImpl.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysRoleMenuServiceImpl.java index 9cdbc3e..aacbe1e 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysRoleMenuServiceImpl.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysRoleMenuServiceImpl.java @@ -1,9 +1,7 @@ package com.songpeng.sparchetype.system.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.songpeng.sparchetype.system.entity.SysRole; import com.songpeng.sparchetype.system.entity.SysRoleMenu; -import com.songpeng.sparchetype.system.mapper.SysRoleMapper; import com.songpeng.sparchetype.system.mapper.SysRoleMenuMapper; import com.songpeng.sparchetype.system.service.ISysRoleMenuService; import org.springframework.stereotype.Service; diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysRoleServiceImpl.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysRoleServiceImpl.java index 6cb9342..84276b2 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysRoleServiceImpl.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysRoleServiceImpl.java @@ -2,11 +2,17 @@ package com.songpeng.sparchetype.system.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.songpeng.sparchetype.common.enums.CommonEnum; import com.songpeng.sparchetype.system.dto.SysRoleDTO; +import com.songpeng.sparchetype.system.dto.SysUserDTO; import com.songpeng.sparchetype.system.entity.SysRole; +import com.songpeng.sparchetype.system.entity.SysUserRole; import com.songpeng.sparchetype.system.enums.SysRoleEnum; import com.songpeng.sparchetype.system.mapper.SysRoleMapper; import com.songpeng.sparchetype.system.service.ISysRoleService; +import com.songpeng.sparchetype.system.service.ISysUserRoleService; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -28,6 +34,9 @@ public class SysRoleServiceImpl extends ServiceImpl impl @Autowired private SysRoleMapper sysRoleMapper; + @Autowired + private ISysUserRoleService sysUserRoleService; + /** * 根据用户ID获取角色列表信息 * @@ -42,7 +51,7 @@ public class SysRoleServiceImpl extends ServiceImpl impl List sysRoles = sysRoleMapper.listByUserId(userId); QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("delete", SysRoleEnum.DELETED_NORMAL.getCode()); + queryWrapper.eq(CommonEnum.FIELD_NAME_IS_DELETED.getCode(), SysRoleEnum.DELETED_NORMAL.getCode()); List sysRolesAll = sysRoleMapper.selectList(null); for (SysRole role : sysRolesAll) { @@ -57,4 +66,30 @@ public class SysRoleServiceImpl extends ServiceImpl impl } return result; } + + /** + * 重新建立用户角色关系 + * + * @param sysUserDTO 系统用户DTO + * @throws Exception 异常 + */ + @Override + public void rebuild(SysUserDTO sysUserDTO) throws Exception { + if (StringUtils.isNotEmpty(sysUserDTO.getId())) { + QueryWrapper deleteWrapper = new QueryWrapper<>(); + deleteWrapper.eq("user_id", sysUserDTO.getId()); + sysUserRoleService.remove(deleteWrapper); + } + if (ArrayUtils.isNotEmpty(sysUserDTO.getSysRoleIds())) { + for (String roleId : sysUserDTO.getSysRoleIds()) { + if (StringUtils.isEmpty(roleId)) { + continue; + } + SysUserRole sysUserRole = new SysUserRole(); + sysUserRole.setUserId(sysUserDTO.getId()); + sysUserRole.setRoleId(roleId); + sysUserRoleService.save(sysUserRole); + } + } + } } diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysUserRoleServiceImpl.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysUserRoleServiceImpl.java index c560286..81ce7a3 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysUserRoleServiceImpl.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysUserRoleServiceImpl.java @@ -1,9 +1,7 @@ package com.songpeng.sparchetype.system.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.songpeng.sparchetype.system.entity.SysUser; import com.songpeng.sparchetype.system.entity.SysUserRole; -import com.songpeng.sparchetype.system.mapper.SysUserMapper; import com.songpeng.sparchetype.system.mapper.SysUserRoleMapper; import com.songpeng.sparchetype.system.service.ISysUserRoleService; import org.springframework.stereotype.Service; diff --git a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysUserServiceImpl.java b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysUserServiceImpl.java index a7a110c..9be7128 100644 --- a/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysUserServiceImpl.java +++ b/sparchetype/src/main/java/com/songpeng/sparchetype/system/service/impl/SysUserServiceImpl.java @@ -1,21 +1,15 @@ package com.songpeng.sparchetype.system.service.impl; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.songpeng.sparchetype.system.dto.SysMenuDTO; import com.songpeng.sparchetype.system.dto.SysRoleDTO; import com.songpeng.sparchetype.system.dto.SysUserDTO; import com.songpeng.sparchetype.system.entity.SysUser; -import com.songpeng.sparchetype.system.entity.SysUserRole; import com.songpeng.sparchetype.system.mapper.SysUserMapper; -import com.songpeng.sparchetype.system.request.SysUserPageReq; import com.songpeng.sparchetype.system.service.ISysMenuService; -import com.songpeng.sparchetype.system.service.ISysUserRoleService; +import com.songpeng.sparchetype.system.service.ISysRoleService; import com.songpeng.sparchetype.system.service.ISysUserService; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.ArrayUtils; -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -40,7 +34,7 @@ public class SysUserServiceImpl extends ServiceImpl impl private ISysMenuService sysMenuService; @Autowired - private ISysUserRoleService sysUserRoleService; + private ISysRoleService sysRoleService; /** * 保存 @@ -52,18 +46,7 @@ public class SysUserServiceImpl extends ServiceImpl impl @Override public void save(SysUserDTO record) throws Exception { sysUserMapper.insert(record); -// rebuild - if (ArrayUtils.isNotEmpty(record.getSysRoleIds())) { - for (String roleId : record.getSysRoleIds()) { - if (StringUtils.isEmpty(roleId)) { - continue; - } - SysUserRole sysUserRole = new SysUserRole(); - sysUserRole.setUserId(record.getId()); - sysUserRole.setRoleId(roleId); - sysUserRoleService.save(sysUserRole); - } - } + sysRoleService.rebuild(record); } /** @@ -76,20 +59,7 @@ public class SysUserServiceImpl extends ServiceImpl impl @Override public void update(SysUserDTO record) throws Exception { sysUserMapper.updateById(record); - QueryWrapper deleteWrapper = new QueryWrapper<>(); - deleteWrapper.eq("user_id", record.getId()); - sysUserRoleService.remove(deleteWrapper); - if (ArrayUtils.isNotEmpty(record.getSysRoleIds())) { - for (String roleId : record.getSysRoleIds()) { - if (StringUtils.isEmpty(roleId)) { - continue; - } - SysUserRole sysUserRole = new SysUserRole(); - sysUserRole.setUserId(record.getId()); - sysUserRole.setRoleId(roleId); - sysUserRoleService.save(sysUserRole); - } - } + sysRoleService.rebuild(record); } /** diff --git a/sparchetype/src/main/resources/templates/login.ftl b/sparchetype/src/main/resources/templates/login.ftl index bade22f..0adce88 100644 --- a/sparchetype/src/main/resources/templates/login.ftl +++ b/sparchetype/src/main/resources/templates/login.ftl @@ -121,17 +121,19 @@

- +
- +
- +
- + + 更换验证码 +
@@ -151,7 +153,6 @@ // 登录过期的时候,跳出ifram框架 if (top.location != self.location) top.location = self.location; - console.log('------------^_^'); // 粒子线条背景 $(document).ready(function () { $('.layui-container').particleground({ @@ -171,12 +172,14 @@ if (result.code === 0) { location.href = '${request.contextPath}/admin' } else { + $("#captchaPic").click(); layer.alert(result.msg, { icon: 2 - }) + }); } }, error: function (e) { + $("#captchaPic").click(); layer.alert(e, { icon: 2 }) @@ -185,6 +188,15 @@ return false; }); + + /** + * 获取图形验证码 + */ + $('#captchaPic').click(function () { + this.src = "${request.contextPath}/verification/code?" + Math.random(); + }); + + $("#captchaPic").click(); });