作者 朱兆平

增加前端提交的password MD5加密处理注解,同时返回的userlist 取消返回用户的password

@@ -14,9 +14,12 @@ @@ -14,9 +14,12 @@
14 * 自定义权限角色管理 14 * 自定义权限角色管理
15 * url角色权限识别 15 * url角色权限识别
16 * menu与权限关联 16 * menu与权限关联
  17 + * 参数校验
17 * 已集成mybatis、mybatisGenerator、pageHelper 18 * 已集成mybatis、mybatisGenerator、pageHelper
18 * 集成定时任务框架 19 * 集成定时任务框架
19 * 目前在IMF框架中使用,打开IMF_Task里面的定时任务注释就可以启动IMF客户端功能 20 * 目前在IMF框架中使用,打开IMF_Task里面的定时任务注释就可以启动IMF客户端功能
20 * 集成Spring Cloud 21 * 集成Spring Cloud
21 * 集成websocket 22 * 集成websocket
  23 +* 将会集成lombok,简化部分代码录入,比如实体类,使用方法见[lombok集成使用说明](https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html)
  24 +)
22 25
  1 +package com.tianbo.warehouse.annotation;
  2 +
  3 +
  4 +import com.tianbo.warehouse.model.USERS;
  5 +
  6 +import java.lang.annotation.*;
  7 +
  8 +/**
  9 + * 将前端注册、新增、编辑后提交的明文密码MD5加密
  10 + * 使用该注解不用再MD5转换了
  11 + *
  12 + * @author adonis
  13 + *
  14 + */
  15 +@Target({ElementType.PARAMETER,ElementType.METHOD})
  16 +@Retention(RetentionPolicy.RUNTIME)
  17 +@Documented
  18 +@Inherited
  19 +public @interface UserPasswordMd5 {
  20 + String value() default "password";
  21 +
  22 +}
  1 +package com.tianbo.warehouse.annotation;
  2 +
  3 +import com.tianbo.warehouse.model.USERS;
  4 +import org.apache.commons.codec.digest.DigestUtils;
  5 +import org.aspectj.lang.JoinPoint;
  6 +import org.aspectj.lang.ProceedingJoinPoint;
  7 +import org.aspectj.lang.annotation.*;
  8 +import org.aspectj.lang.reflect.MethodSignature;
  9 +import org.springframework.stereotype.Component;
  10 +
  11 +import java.lang.reflect.Method;
  12 +
  13 +@Aspect
  14 +@Component
  15 +public class UserPasswordMd5AOP {
  16 +
  17 + @Pointcut("@annotation(com.tianbo.warehouse.annotation.UserPasswordMd5)")
  18 + public void annotationPointCut(){
  19 +
  20 + }
  21 +
  22 + @Before("annotationPointCut()")
  23 + public void before(JoinPoint joinPoint){
  24 + MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  25 + Method method= signature.getMethod();
  26 + UserPasswordMd5 annotation = method.getAnnotation(UserPasswordMd5.class);
  27 + System.out.print("打印:"+annotation.value()+" 开始前");
  28 + }
  29 +
  30 + @Around("annotationPointCut()")
  31 + public Object advice(ProceedingJoinPoint joinPoint){
  32 + System.out.println("通知之开始");
  33 + Object[] args = joinPoint.getArgs();
  34 + if (args != null && args.length > 0 && args[0].getClass() == USERS.class) {
  35 + USERS user = (USERS)args[0];
  36 + user.setPassword(DigestUtils.md5Hex(user.getPassword()));
  37 + args[0] = user;
  38 +
  39 + }
  40 + Object retmsg=null;
  41 + try {
  42 + retmsg=joinPoint.proceed(args);
  43 + System.err.println("++++++++"+retmsg);
  44 + } catch (Throwable e) {
  45 + e.printStackTrace();
  46 + }
  47 + System.out.println("通知之结束");
  48 + return retmsg;
  49 + }
  50 +
  51 + @After("annotationPointCut()")
  52 + public void after(){
  53 + System.out.println("after方法执行后");
  54 + }
  55 +
  56 +}
1 package com.tianbo.warehouse.controller; 1 package com.tianbo.warehouse.controller;
2 2
3 import com.github.pagehelper.PageInfo; 3 import com.github.pagehelper.PageInfo;
  4 +import com.tianbo.warehouse.annotation.UserPasswordMd5;
4 import com.tianbo.warehouse.controller.response.ResultJson; 5 import com.tianbo.warehouse.controller.response.ResultJson;
5 import com.tianbo.warehouse.model.USERS; 6 import com.tianbo.warehouse.model.USERS;
6 import com.tianbo.warehouse.service.UserService; 7 import com.tianbo.warehouse.service.UserService;
7 -import org.bouncycastle.asn1.ASN1Sequence;  
8 -import org.bouncycastle.asn1.ocsp.ResponseData;  
9 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.security.core.context.SecurityContextHolder; 9 import org.springframework.security.core.context.SecurityContextHolder;
11 -import org.springframework.security.core.userdetails.User;  
12 import org.springframework.security.core.userdetails.UserDetails; 10 import org.springframework.security.core.userdetails.UserDetails;
  11 +import org.springframework.validation.BindingResult;
13 import org.springframework.web.bind.annotation.*; 12 import org.springframework.web.bind.annotation.*;
14 13
15 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse; 15 import javax.servlet.http.HttpServletResponse;
17 -import java.util.List; 16 +import javax.validation.Valid;
18 17
19 @RestController 18 @RestController
20 public class UserController { 19 public class UserController {
@@ -37,8 +36,9 @@ public class UserController { @@ -37,8 +36,9 @@ public class UserController {
37 return "欢迎回来:"+userDetails.getUsername(); 36 return "欢迎回来:"+userDetails.getUsername();
38 } 37 }
39 38
  39 + @UserPasswordMd5
40 @PutMapping("/user/edit") 40 @PutMapping("/user/edit")
41 - public ResultJson updateUserById(@RequestBody USERS user){ 41 + public ResultJson updateUserById(@RequestBody @Valid USERS user){
42 int i = userService.updateByPrimaryKeySelective(user); 42 int i = userService.updateByPrimaryKeySelective(user);
43 ResultJson resultJson = new ResultJson(); 43 ResultJson resultJson = new ResultJson();
44 if (1==i){ 44 if (1==i){
@@ -49,8 +49,14 @@ public class UserController { @@ -49,8 +49,14 @@ public class UserController {
49 return resultJson; 49 return resultJson;
50 } 50 }
51 51
  52 + @UserPasswordMd5
52 @PostMapping("/user/add") 53 @PostMapping("/user/add")
53 - public ResultJson addUser(@RequestBody USERS user,HttpServletRequest request,HttpServletResponse response){ 54 + public ResultJson addUser(@RequestBody @Valid USERS user, HttpServletRequest request, HttpServletResponse response, BindingResult bindingResult){
  55 +
  56 + if (bindingResult.hasErrors()){
  57 + String s = bindingResult.toString();
  58 + }
  59 +
54 int i = userService.insertSelective(user); 60 int i = userService.insertSelective(user);
55 ResultJson resultJson = new ResultJson(); 61 ResultJson resultJson = new ResultJson();
56 if (1==i){ 62 if (1==i){
1 package com.tianbo.warehouse.model; 1 package com.tianbo.warehouse.model;
2 2
  3 +import com.tianbo.warehouse.validate.CheckUserExist;
  4 +import org.hibernate.validator.constraints.Length;
3 import org.springframework.security.core.GrantedAuthority; 5 import org.springframework.security.core.GrantedAuthority;
4 import org.springframework.security.core.authority.SimpleGrantedAuthority; 6 import org.springframework.security.core.authority.SimpleGrantedAuthority;
5 import org.springframework.security.core.userdetails.UserDetails; 7 import org.springframework.security.core.userdetails.UserDetails;
6 8
  9 +import javax.validation.constraints.*;
7 import java.util.ArrayList; 10 import java.util.ArrayList;
8 import java.util.Collection; 11 import java.util.Collection;
9 import java.util.Date; 12 import java.util.Date;
@@ -13,10 +16,17 @@ public class USERS implements UserDetails { @@ -13,10 +16,17 @@ public class USERS implements UserDetails {
13 16
14 private static final long serialVersionUID = 1L; 17 private static final long serialVersionUID = 1L;
15 18
  19 + @DecimalMin("1")
16 private Integer userId; 20 private Integer userId;
17 21
  22 + @NotBlank(message="用户名不能为空")
  23 + @Length(min = 5, max = 11, message = "username 长度必须在 {min} - {max} 之间")
  24 + @CheckUserExist(message = "用户已存在")
18 private String username; 25 private String username;
19 26
  27 + @NotNull
  28 + @NotBlank(message="密码不能为空")
  29 + @Length(min = 6, max = 22, message = "密码 长度必须在 {min} - {max} 之间")
20 private String password; 30 private String password;
21 31
22 private Date birthday; 32 private Date birthday;
@@ -27,6 +37,7 @@ public class USERS implements UserDetails { @@ -27,6 +37,7 @@ public class USERS implements UserDetails {
27 37
28 private Boolean state; 38 private Boolean state;
29 39
  40 + @Length(min = 11, max = 11, message = "mobilephone 长度必须为11位")
30 private String mobilephone; 41 private String mobilephone;
31 42
32 private Date creattime; 43 private Date creattime;
@@ -36,7 +47,7 @@ public class USERS implements UserDetails { @@ -36,7 +47,7 @@ public class USERS implements UserDetails {
36 private String userface; 47 private String userface;
37 48
38 private String realname; 49 private String realname;
39 - 50 + @Pattern(regexp="[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\\.[a-za-z]{2,4}", message="邮件格式错误")
40 private String email; 51 private String email;
41 52
42 private Integer age; 53 private Integer age;
  1 +package com.tianbo.warehouse.validate;
  2 +
  3 +import javax.validation.Constraint;
  4 +import javax.validation.Payload;
  5 +import java.lang.annotation.*;
  6 +
  7 +@Target({ElementType.METHOD,ElementType.FIELD,ElementType.ANNOTATION_TYPE})
  8 +@Retention(RetentionPolicy.RUNTIME)
  9 +@Constraint(validatedBy = CheckUserExistValidator.class )
  10 +@Documented
  11 +/**
  12 + * 检查用户重复注解
  13 + * @author mrz
  14 + *
  15 + */
  16 +public @interface CheckUserExist {
  17 +
  18 + String message() default "";
  19 +
  20 + Class<?>[] groups() default {};
  21 +
  22 + Class<? extends Payload>[] payload() default {};
  23 +
  24 +
  25 +
  26 +}
  1 +package com.tianbo.warehouse.validate;
  2 +
  3 +import com.tianbo.warehouse.service.UserService;
  4 +import org.springframework.beans.factory.annotation.Autowired;
  5 +
  6 +import javax.validation.ConstraintValidator;
  7 +import javax.validation.ConstraintValidatorContext;
  8 +
  9 +public class CheckUserExistValidator implements ConstraintValidator<CheckUserExist,String> {
  10 +
  11 + private CheckUserExist checkUserExist;
  12 +
  13 + @Autowired
  14 + UserService userService;
  15 +
  16 + @Override
  17 + public void initialize(CheckUserExist checkUserExist) {
  18 + this.checkUserExist = checkUserExist;
  19 + }
  20 +
  21 + @Override
  22 + public boolean isValid(String username, ConstraintValidatorContext var2){
  23 + if (username == null){
  24 + return true;
  25 + }
  26 +
  27 + //根据用户名查询出来有数据,则返回失败存在用户
  28 + if (userService.loadByUsername(username)!=null){
  29 + return false;
  30 + }else {
  31 + return true;
  32 + }
  33 +
  34 +
  35 + }
  36 +}
@@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
18 <result column="age" property="age" jdbcType="INTEGER" /> 18 <result column="age" property="age" jdbcType="INTEGER" />
19 </resultMap> 19 </resultMap>
20 <sql id="Base_Column_List" > 20 <sql id="Base_Column_List" >
21 - user_id, username, password, birthday, sex, address, state, mobilePhone, creatTime, 21 + user_id, username, birthday, sex, address, state, mobilePhone, creatTime,
22 updateTime, userFace, realName, email, age 22 updateTime, userFace, realName, email, age
23 </sql> 23 </sql>
24 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > 24 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >