Commit 3a1292d8151473596777299f6c6dd454e062a0bd
1 parent
ab2a24db4e
Exists in
master
添加用户、角色、权限接口;
添加实体对象验证
Showing 12 changed files with 423 additions and 23 deletions
- talkonlineweb/pom.xml
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/config/MyBatisConfigs.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/config/MyWebInterceptor.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/PermissController.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/RoleController.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/TestController.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsUser.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsUserMapper.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsUserService.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsUserServiceImpl.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java
talkonlineweb/pom.xml
View file @
3a1292d
| ... | ... | @@ -26,6 +26,11 @@ |
| 26 | 26 | </exclusion> |
| 27 | 27 | </exclusions> |
| 28 | 28 | </dependency> |
| 29 | + <!-- 验证框架 --> | |
| 30 | + <dependency> | |
| 31 | + <groupId>org.springframework.boot</groupId> | |
| 32 | + <artifactId>spring-boot-starter-validation</artifactId> | |
| 33 | + </dependency> | |
| 29 | 34 | <!-- 引入log4j2依赖 --> |
| 30 | 35 | <dependency> |
| 31 | 36 | <groupId>org.springframework.boot</groupId> |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/config/MyBatisConfigs.java
View file @
3a1292d
| ... | ... | @@ -9,11 +9,11 @@ |
| 9 | 9 | @Configuration |
| 10 | 10 | public class MyBatisConfigs { |
| 11 | 11 | |
| 12 | -// @Bean | |
| 13 | -// public MybatisPlusInterceptor mybatisPlusInterceptor() { | |
| 14 | -// MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); | |
| 15 | -// interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); | |
| 16 | -// return interceptor; | |
| 17 | -// } | |
| 12 | + @Bean | |
| 13 | + public MybatisPlusInterceptor mybatisPlusInterceptor() { | |
| 14 | + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); | |
| 15 | + interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); | |
| 16 | + return interceptor; | |
| 17 | + } | |
| 18 | 18 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/config/MyWebInterceptor.java
View file @
3a1292d
| ... | ... | @@ -10,6 +10,7 @@ |
| 10 | 10 | import org.springframework.beans.factory.annotation.Value; |
| 11 | 11 | import org.springframework.web.method.HandlerMethod; |
| 12 | 12 | import org.springframework.web.servlet.HandlerInterceptor; |
| 13 | +import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; | |
| 13 | 14 | |
| 14 | 15 | import javax.servlet.http.HttpServletRequest; |
| 15 | 16 | import javax.servlet.http.HttpServletResponse; |
| ... | ... | @@ -34,7 +35,13 @@ |
| 34 | 35 | System.out.println(handler); |
| 35 | 36 | |
| 36 | 37 | |
| 37 | - TokenRequired clientRequired = findAnnotation((HandlerMethod) handler, TokenRequired.class); | |
| 38 | + TokenRequired clientRequired = null; | |
| 39 | + if(handler instanceof HandlerMethod){ | |
| 40 | + clientRequired=findAnnotation((HandlerMethod) handler, TokenRequired.class); | |
| 41 | + } | |
| 42 | +// if(handler instanceof ResourceHttpRequestHandler){ | |
| 43 | +// clientRequired=findAnnotation((ResourceHttpRequestHandler) handler, TokenRequired.class); | |
| 44 | +// } | |
| 38 | 45 | if (null == clientRequired) |
| 39 | 46 | return true; |
| 40 | 47 |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/PermissController.java
View file @
3a1292d
| 1 | +package com.lyms.talkonlineweb.controller; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |
| 4 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |
| 5 | +import com.lyms.talkonlineweb.domain.LymsPermission; | |
| 6 | +import com.lyms.talkonlineweb.domain.LymsUser; | |
| 7 | +import com.lyms.talkonlineweb.result.BaseResponse; | |
| 8 | +import com.lyms.talkonlineweb.service.LymsPermissionService; | |
| 9 | +import lombok.extern.log4j.Log4j2; | |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | +import org.springframework.web.bind.annotation.PostMapping; | |
| 12 | +import org.springframework.web.bind.annotation.RestController; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * 权限管理 | |
| 16 | + */ | |
| 17 | + | |
| 18 | +@RestController | |
| 19 | +@Log4j2 | |
| 20 | +public class PermissController { | |
| 21 | + @Autowired | |
| 22 | + private LymsPermissionService lymsPermissionService; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * 添加或修改权限 | |
| 26 | + * @param perm | |
| 27 | + * @return | |
| 28 | + */ | |
| 29 | + @PostMapping("savePerm") | |
| 30 | + public BaseResponse savePerm(LymsPermission perm){ | |
| 31 | + BaseResponse baseResponse=new BaseResponse(); | |
| 32 | + boolean f=lymsPermissionService.saveOrUpdate(perm); | |
| 33 | + baseResponse.setErrorcode(f==true?0:1); | |
| 34 | + return baseResponse; | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * 删除权限 | |
| 39 | + * @param perm | |
| 40 | + * @return | |
| 41 | + */ | |
| 42 | + @PostMapping("delPerm") | |
| 43 | + public BaseResponse delPerm(LymsPermission perm){ | |
| 44 | + BaseResponse baseResponse=new BaseResponse(); | |
| 45 | + boolean f=lymsPermissionService.remove(Wrappers.query(perm)); | |
| 46 | + baseResponse.setErrorcode(f==true?0:1); | |
| 47 | + return baseResponse; | |
| 48 | + } | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * 获取权限列表 | |
| 52 | + * @param perm | |
| 53 | + * @param current | |
| 54 | + * @param size | |
| 55 | + * @return | |
| 56 | + */ | |
| 57 | + @PostMapping("sltPerm") | |
| 58 | + public BaseResponse sltPerm(LymsPermission perm,int current,int size){ | |
| 59 | + BaseResponse baseResponse=new BaseResponse(); | |
| 60 | + Page<LymsPermission> page=new Page<>(current,size); | |
| 61 | + | |
| 62 | + Page<LymsPermission> perIPage=lymsPermissionService.page(page,Wrappers.query(perm).orderByDesc("updated_time","createdtime")); | |
| 63 | + baseResponse.setObject(perIPage); | |
| 64 | + | |
| 65 | + return baseResponse; | |
| 66 | + } | |
| 67 | + | |
| 68 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/RoleController.java
View file @
3a1292d
| 1 | +package com.lyms.talkonlineweb.controller; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |
| 4 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |
| 5 | +import com.lyms.talkonlineweb.domain.LymsRole; | |
| 6 | +import com.lyms.talkonlineweb.domain.LymsUser; | |
| 7 | +import com.lyms.talkonlineweb.result.BaseResponse; | |
| 8 | +import com.lyms.talkonlineweb.service.LymsRoleService; | |
| 9 | +import com.lyms.talkonlineweb.service.LymsUserService; | |
| 10 | +import lombok.extern.log4j.Log4j2; | |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | +import org.springframework.util.StringUtils; | |
| 13 | +import org.springframework.web.bind.annotation.GetMapping; | |
| 14 | +import org.springframework.web.bind.annotation.PostMapping; | |
| 15 | +import org.springframework.web.bind.annotation.RequestHeader; | |
| 16 | +import org.springframework.web.bind.annotation.RestController; | |
| 17 | + | |
| 18 | +import java.util.Date; | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * 角色管理 | |
| 23 | + */ | |
| 24 | +@RestController | |
| 25 | +@Log4j2 | |
| 26 | +public class RoleController { | |
| 27 | + @Autowired | |
| 28 | + private LymsUserService lymsUserService; | |
| 29 | + | |
| 30 | + @Autowired | |
| 31 | + private LymsRoleService lymsRoleService; | |
| 32 | + /** | |
| 33 | + * 保存角色 | |
| 34 | + * @param role | |
| 35 | + * @return | |
| 36 | + */ | |
| 37 | + @PostMapping("saveRole") | |
| 38 | + public BaseResponse saveRole(LymsRole role){ | |
| 39 | + BaseResponse baseResponse=new BaseResponse(); | |
| 40 | + | |
| 41 | +// LymsUser user=lymsUserService.getUserByToken(Authorization); | |
| 42 | +// role.setCreatedby(user.getUid()); | |
| 43 | + | |
| 44 | + if(role.getRid()==null){ | |
| 45 | + role.setCreatedtime(new Date()); | |
| 46 | + }else { | |
| 47 | +// role.setUpdatedby(user.getUid()); | |
| 48 | + role.setUpdatedTime(new Date()); | |
| 49 | + } | |
| 50 | + boolean f=false; | |
| 51 | + | |
| 52 | + if(!StringUtils.isEmpty(role.getRname())){ | |
| 53 | + f=lymsRoleService.saveOrUpdate(role); | |
| 54 | + } | |
| 55 | + | |
| 56 | + baseResponse.setErrorcode(f==true?0:1); | |
| 57 | + return baseResponse; | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * 查询角色列表 | |
| 62 | + * @param role | |
| 63 | + * @param current | |
| 64 | + * @param size | |
| 65 | + * @return | |
| 66 | + */ | |
| 67 | + @GetMapping("sltRole") | |
| 68 | + public BaseResponse sltRole(LymsRole role,int current,int size){ | |
| 69 | + BaseResponse baseResponse=new BaseResponse(); | |
| 70 | + Page<LymsRole> page=new Page<>(current,size); | |
| 71 | + Page<LymsRole> rolePage=lymsRoleService.page(page, Wrappers.query(role).orderByDesc("updated_time","createdtime")); | |
| 72 | + baseResponse.setObject(rolePage); | |
| 73 | + return baseResponse; | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * 删除角色 | |
| 78 | + * @param rid | |
| 79 | + * @return | |
| 80 | + */ | |
| 81 | + @GetMapping("delRole") | |
| 82 | + public BaseResponse delRole(int rid){ | |
| 83 | + BaseResponse baseResponse=new BaseResponse(); | |
| 84 | + boolean f=lymsRoleService.removeById(rid); | |
| 85 | + baseResponse.setErrorcode(f==true?0:1); | |
| 86 | + return baseResponse; | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * 根据用户获取角色 | |
| 91 | + * @param uid | |
| 92 | + * @return | |
| 93 | + */ | |
| 94 | + @GetMapping("getRolesByUid") | |
| 95 | + public BaseResponse getRolesByUid(int uid){ | |
| 96 | + BaseResponse baseResponse=new BaseResponse(); | |
| 97 | + List<LymsRole> rLst=lymsRoleService.getRolesByUid(uid); | |
| 98 | + baseResponse.setObject(rLst); | |
| 99 | + return baseResponse; | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * 添加角色包含的权限 | |
| 104 | + * @param rid | |
| 105 | + * @param perms 如果是多个权限需要用","分割 | |
| 106 | + * @return | |
| 107 | + */ | |
| 108 | + @PostMapping("addRoleByPerms") | |
| 109 | + public BaseResponse addRoleByPerms(int rid,String perms){ | |
| 110 | + BaseResponse baseResponse=new BaseResponse(); | |
| 111 | + String[] pArr=perms.split(","); | |
| 112 | + int cnt=0; | |
| 113 | + | |
| 114 | + cnt=lymsRoleService.delPersByRole(rid);//删除旧的关系 | |
| 115 | + for (int i = 0; i < pArr.length; i++) { | |
| 116 | + cnt=lymsRoleService.addRoleByPerms(rid,Integer.parseInt(pArr[i])); | |
| 117 | + } | |
| 118 | +// baseResponse.setObject(rLst); | |
| 119 | + return baseResponse; | |
| 120 | + } | |
| 121 | + | |
| 122 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/TestController.java
View file @
3a1292d
| ... | ... | @@ -16,14 +16,14 @@ |
| 16 | 16 | public class TestController { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - @Autowired | |
| 20 | - private LymsUserService lymsUserService; | |
| 21 | - | |
| 22 | - @GetMapping("lstUser") | |
| 23 | - @TokenRequired | |
| 24 | - public List<LymsUser> listUser(){ | |
| 25 | - log.info("List user!!"); | |
| 26 | - return lymsUserService.list(); | |
| 27 | - } | |
| 19 | +// @Autowired | |
| 20 | +// private LymsUserService lymsUserService; | |
| 21 | +// | |
| 22 | +// @GetMapping("lstUser") | |
| 23 | +// @TokenRequired | |
| 24 | +// public List<LymsUser> listUser(){ | |
| 25 | +// log.info("List user!!"); | |
| 26 | +// return lymsUserService.list(); | |
| 27 | +// } | |
| 28 | 28 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java
View file @
3a1292d
| ... | ... | @@ -2,17 +2,25 @@ |
| 2 | 2 | |
| 3 | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 4 | 4 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| 5 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |
| 6 | +import com.lyms.talkonlineweb.annotation.TokenRequired; | |
| 5 | 7 | import com.lyms.talkonlineweb.domain.LymsUser; |
| 6 | 8 | import com.lyms.talkonlineweb.mapper.LymsUserMapper; |
| 7 | 9 | import com.lyms.talkonlineweb.result.BaseResponse; |
| 10 | +import com.lyms.talkonlineweb.result.CheckResult; | |
| 8 | 11 | import com.lyms.talkonlineweb.service.LymsUserService; |
| 9 | 12 | import com.lyms.talkonlineweb.util.Constant; |
| 10 | 13 | import com.lyms.talkonlineweb.util.JwtUtils; |
| 14 | +import io.jsonwebtoken.Claims; | |
| 11 | 15 | import lombok.extern.log4j.Log4j2; |
| 12 | 16 | import org.springframework.beans.factory.annotation.Autowired; |
| 13 | -import org.springframework.web.bind.annotation.GetMapping; | |
| 14 | -import org.springframework.web.bind.annotation.RestController; | |
| 17 | +import org.springframework.util.DigestUtils; | |
| 18 | +import org.springframework.validation.BindingResult; | |
| 19 | +import org.springframework.validation.annotation.Validated; | |
| 20 | +import org.springframework.web.bind.annotation.*; | |
| 15 | 21 | |
| 22 | +import javax.servlet.http.HttpSession; | |
| 23 | +import java.util.Date; | |
| 16 | 24 | import java.util.HashMap; |
| 17 | 25 | import java.util.List; |
| 18 | 26 | import java.util.Map; |
| 19 | 27 | |
| 20 | 28 | |
| 21 | 29 | |
| 22 | 30 | |
| ... | ... | @@ -31,22 +39,122 @@ |
| 31 | 39 | private LymsUserMapper lymsUserMapper; |
| 32 | 40 | |
| 33 | 41 | @GetMapping("login") |
| 34 | - public BaseResponse login(String username,String passwd){ | |
| 42 | + public BaseResponse login(String username, String passwd){ | |
| 35 | 43 | // QueryWrapper queryWrapper= Wrappers.query(LymsUser.class); |
| 36 | 44 | log.info(">>>>>"); |
| 37 | 45 | BaseResponse baseResponse=new BaseResponse(); |
| 38 | 46 | Map<String,Object> tMap=new HashMap<>(); |
| 39 | 47 | tMap.put("token",""); |
| 40 | 48 | |
| 41 | - List<LymsUser> uLst=lymsUserMapper.sltUser(username,passwd); | |
| 49 | + List<LymsUser> uLst=lymsUserMapper.sltUser(username,DigestUtils.md5DigestAsHex(passwd.getBytes())); | |
| 42 | 50 | |
| 43 | 51 | if(uLst.size()>0){ |
| 44 | - String JWT = JwtUtils.createJWT("1", username, Constant.JWT_TTL); | |
| 52 | + LymsUser user=uLst.get(0); | |
| 53 | + String JWT = JwtUtils.createJWT("1", user.getLogin(), Constant.JWT_TTL); | |
| 45 | 54 | tMap.put("token",JWT); |
| 55 | + user.setPasswd(null); | |
| 56 | + tMap.put("user",user); | |
| 46 | 57 | } |
| 47 | 58 | baseResponse.setObject(tMap); |
| 48 | 59 | baseResponse.setErrorcode(0); |
| 49 | 60 | return baseResponse; |
| 50 | 61 | } |
| 62 | + | |
| 63 | + /** | |
| 64 | + * 得到用户权限 | |
| 65 | + * @return | |
| 66 | + */ | |
| 67 | + @PostMapping("getPermission") | |
| 68 | + public BaseResponse getPermission(@RequestHeader String authorization){ | |
| 69 | + | |
| 70 | + BaseResponse baseResponse=new BaseResponse(); | |
| 71 | + try { | |
| 72 | + Claims claims =JwtUtils.parseJWT(authorization); | |
| 73 | + String username=claims.getSubject(); | |
| 74 | + | |
| 75 | + List<Map<String,Object>> permiss=lymsUserService.getPermissionByUsername(username); | |
| 76 | + baseResponse.setObject(permiss); | |
| 77 | + } catch (Exception e) { | |
| 78 | + log.error(e.getMessage()); | |
| 79 | + e.printStackTrace(); | |
| 80 | + baseResponse.setErrorcode(1); | |
| 81 | + baseResponse.setErrormsg(e.getMessage()); | |
| 82 | + } | |
| 83 | + | |
| 84 | + return baseResponse; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * 添加用户 | |
| 89 | + * @param user | |
| 90 | + * @param authorization | |
| 91 | + * @return | |
| 92 | + */ | |
| 93 | + @RequestMapping("saveUser") | |
| 94 | + public BaseResponse saveUser(@Validated LymsUser user, BindingResult result, @RequestHeader String authorization){ | |
| 95 | + BaseResponse baseResponse=new BaseResponse(); | |
| 96 | + baseResponse.setErrormsg(""); | |
| 97 | + try { | |
| 98 | + LymsUser curUser=lymsUserService.getUserByToken(authorization); | |
| 99 | + user.setCreatedby(curUser.getUid()); | |
| 100 | + user.setCreatedtime(new Date()); | |
| 101 | + if(user.getUid()==null){ | |
| 102 | + user.setPasswd(DigestUtils.md5DigestAsHex("123456".getBytes())); | |
| 103 | + } | |
| 104 | + | |
| 105 | + if(result.hasErrors()){ | |
| 106 | + result.getAllErrors().forEach(e->{ | |
| 107 | + baseResponse.setErrormsg(baseResponse.getErrormsg()+e.getDefaultMessage()+";"); | |
| 108 | + }); | |
| 109 | + baseResponse.setErrorcode(1); | |
| 110 | + return baseResponse; | |
| 111 | + } | |
| 112 | + | |
| 113 | + boolean f=lymsUserService.saveOrUpdate(user); | |
| 114 | + baseResponse.setErrorcode(f==true?0:1); | |
| 115 | + } catch (Exception e) { | |
| 116 | + log.error(e.getMessage()); | |
| 117 | + e.printStackTrace(); | |
| 118 | + baseResponse.setErrorcode(1); | |
| 119 | + baseResponse.setErrormsg(e.getMessage()); | |
| 120 | + } | |
| 121 | + | |
| 122 | + return baseResponse; | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * 删除用户 | |
| 127 | + * @param uid | |
| 128 | + * @return | |
| 129 | + */ | |
| 130 | + @PostMapping("delUser") | |
| 131 | + public BaseResponse delUser(int uid){ | |
| 132 | + BaseResponse baseResponse=new BaseResponse(); | |
| 133 | + boolean f=lymsUserService.removeById(uid); | |
| 134 | + baseResponse.setErrorcode(f==true?0:1); | |
| 135 | + if(!f){ | |
| 136 | + baseResponse.setErrormsg("不存在改用户!"); | |
| 137 | + } | |
| 138 | + return baseResponse; | |
| 139 | + } | |
| 140 | + | |
| 141 | + /** | |
| 142 | + * 查询用户列表 | |
| 143 | + * @param user | |
| 144 | + * @param current | |
| 145 | + * @param size | |
| 146 | + * @return | |
| 147 | + */ | |
| 148 | + @GetMapping("sltUser") | |
| 149 | + public BaseResponse sltUser(LymsUser user,int current,int size){ | |
| 150 | + BaseResponse baseResponse=new BaseResponse(); | |
| 151 | + Page<LymsUser> page=new Page<>(current,size); | |
| 152 | + Page<LymsUser> userIPage=lymsUserService.page(page,Wrappers.query(user).orderByDesc("updated_time","createdtime")); | |
| 153 | + baseResponse.setObject(userIPage); | |
| 154 | + | |
| 155 | + return baseResponse; | |
| 156 | + } | |
| 157 | + | |
| 158 | + | |
| 51 | 159 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsUser.java
View file @
3a1292d
| ... | ... | @@ -6,8 +6,11 @@ |
| 6 | 6 | import com.baomidou.mybatisplus.annotation.TableName; |
| 7 | 7 | import java.io.Serializable; |
| 8 | 8 | import java.util.Date; |
| 9 | + | |
| 9 | 10 | import lombok.Data; |
| 10 | 11 | |
| 12 | +import javax.validation.constraints.NotNull; | |
| 13 | + | |
| 11 | 14 | /** |
| 12 | 15 | * 用户 |
| 13 | 16 | * @TableName lyms_user |
| 14 | 17 | |
| ... | ... | @@ -24,12 +27,14 @@ |
| 24 | 27 | /** |
| 25 | 28 | * 姓名 |
| 26 | 29 | */ |
| 30 | + @NotNull(message = "姓名不能为空") | |
| 27 | 31 | @TableField(value = "uname") |
| 28 | 32 | private String uname; |
| 29 | 33 | |
| 30 | 34 | /** |
| 31 | 35 | * 登录账号 |
| 32 | 36 | */ |
| 37 | + @NotNull(message = "登录账号不能为空") | |
| 33 | 38 | @TableField(value = "login") |
| 34 | 39 | private String login; |
| 35 | 40 | |
| ... | ... | @@ -66,7 +71,7 @@ |
| 66 | 71 | /** |
| 67 | 72 | * 更新时间 |
| 68 | 73 | */ |
| 69 | - @TableField(value = "updated_time") | |
| 74 | + @TableField(value = "updated_time",update = "now()") | |
| 70 | 75 | private Date updatedTime; |
| 71 | 76 | |
| 72 | 77 | @TableField(exist = false) |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsUserMapper.java
View file @
3a1292d
| ... | ... | @@ -12,7 +12,7 @@ |
| 12 | 12 | */ |
| 13 | 13 | public interface LymsUserMapper extends BaseMapper<LymsUser> { |
| 14 | 14 | |
| 15 | - @Select("select * from lyms_user where login=#{login} and passwd=password(#{passwd}) ") | |
| 15 | + @Select("select * from lyms_user where login=#{login} and passwd=#{passwd} ") | |
| 16 | 16 | public List<LymsUser> sltUser(@Param("login") String login, @Param("passwd") String passwd); |
| 17 | 17 | |
| 18 | 18 |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsUserService.java
View file @
3a1292d
| ... | ... | @@ -3,10 +3,16 @@ |
| 3 | 3 | import com.lyms.talkonlineweb.domain.LymsUser; |
| 4 | 4 | import com.baomidou.mybatisplus.extension.service.IService; |
| 5 | 5 | |
| 6 | +import java.util.List; | |
| 7 | +import java.util.Map; | |
| 8 | + | |
| 6 | 9 | /** |
| 7 | 10 | * |
| 8 | 11 | */ |
| 9 | 12 | public interface LymsUserService extends IService<LymsUser> { |
| 10 | 13 | |
| 14 | + List<Map<String, Object>> getPermissionByUsername(String username) throws Exception; | |
| 15 | + | |
| 16 | + LymsUser getUserByToken(String token); | |
| 11 | 17 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsUserServiceImpl.java
View file @
3a1292d
| 1 | 1 | package com.lyms.talkonlineweb.service.impl; |
| 2 | 2 | |
| 3 | 3 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| 4 | +import com.lyms.talkonlineweb.domain.LymsPermission; | |
| 5 | +import com.lyms.talkonlineweb.domain.LymsRole; | |
| 4 | 6 | import com.lyms.talkonlineweb.domain.LymsUser; |
| 7 | +import com.lyms.talkonlineweb.mapper.LymsPermissionMapper; | |
| 8 | +import com.lyms.talkonlineweb.mapper.LymsRoleMapper; | |
| 5 | 9 | import com.lyms.talkonlineweb.service.LymsUserService; |
| 6 | 10 | import com.lyms.talkonlineweb.mapper.LymsUserMapper; |
| 11 | +import com.lyms.talkonlineweb.util.Constant; | |
| 12 | +import com.lyms.talkonlineweb.util.JwtUtils; | |
| 13 | +import io.jsonwebtoken.Claims; | |
| 14 | +import lombok.extern.log4j.Log4j2; | |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | 16 | import org.springframework.stereotype.Service; |
| 8 | 17 | |
| 18 | +import java.util.ArrayList; | |
| 19 | +import java.util.HashMap; | |
| 20 | +import java.util.List; | |
| 21 | +import java.util.Map; | |
| 22 | + | |
| 9 | 23 | /** |
| 10 | 24 | * |
| 11 | 25 | */ |
| 12 | 26 | @Service |
| 27 | +@Log4j2 | |
| 13 | 28 | public class LymsUserServiceImpl extends ServiceImpl<LymsUserMapper, LymsUser> |
| 14 | 29 | implements LymsUserService{ |
| 15 | 30 | |
| 31 | + @Autowired | |
| 32 | + private LymsUserMapper lymsUserMapper; | |
| 33 | + | |
| 34 | + @Autowired | |
| 35 | + private LymsRoleMapper lymsRoleMapper; | |
| 36 | + | |
| 37 | + @Autowired | |
| 38 | + private LymsPermissionMapper lymsPermissionMapper; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * 根据登录用户获取权限 | |
| 42 | + * @param username | |
| 43 | + * @return | |
| 44 | + */ | |
| 45 | + @Override | |
| 46 | + public List<Map<String, Object>> getPermissionByUsername(String username) throws Exception { | |
| 47 | + List<Map<String, Object>> rs=new ArrayList<>(); | |
| 48 | + | |
| 49 | + Map<String,Object> param=new HashMap<>(); | |
| 50 | + param.put("login",username); | |
| 51 | + List<LymsUser> uLst= lymsUserMapper.selectByMap(param); | |
| 52 | + | |
| 53 | + if(uLst.size()<1){ | |
| 54 | + throw new Exception("系统错误"); | |
| 55 | + } | |
| 56 | + List<LymsRole> rLst=lymsRoleMapper.sltRoleByUser(uLst.get(0).getUid()); | |
| 57 | + | |
| 58 | + int rid=rLst.get(0).getRid(); | |
| 59 | +// 首次取一次菜单 | |
| 60 | + List<LymsPermission> pLst=lymsPermissionMapper.sltPermissByRole(rid, Constant.ROOT_MENU); | |
| 61 | + | |
| 62 | + for (LymsPermission p:pLst) { | |
| 63 | + //获取二级菜单 | |
| 64 | + List<LymsPermission> sLst=lymsPermissionMapper.sltPermissByRole(rid, p.getId()); | |
| 65 | + Map<String, Object> oneMap=new HashMap<>(); | |
| 66 | + oneMap.put("menu",p); | |
| 67 | + oneMap.put("children",sLst); | |
| 68 | + rs.add(oneMap); | |
| 69 | + } | |
| 70 | + | |
| 71 | + return rs; | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * 根据token获取用户 | |
| 76 | + * @param token | |
| 77 | + * @return | |
| 78 | + */ | |
| 79 | + @Override | |
| 80 | + public LymsUser getUserByToken(String token) { | |
| 81 | + Claims claims = null; | |
| 82 | + try { | |
| 83 | + claims = JwtUtils.parseJWT(token); | |
| 84 | + } catch (Exception e) { | |
| 85 | + e.printStackTrace(); | |
| 86 | + } | |
| 87 | + String username=claims.getSubject(); | |
| 88 | + Map<String,Object> param=new HashMap<>(); | |
| 89 | + param.put("login",username); | |
| 90 | + List<LymsUser> uLst= lymsUserMapper.selectByMap(param); | |
| 91 | + return uLst.size()>0?uLst.get(0):null; | |
| 92 | + } | |
| 16 | 93 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java
View file @
3a1292d
| ... | ... | @@ -5,5 +5,7 @@ |
| 5 | 5 | public static final int JWT_ERRCODE_EXPIRE = 500; |
| 6 | 6 | public static final int JWT_ERRCODE_FAIL = 501; |
| 7 | 7 | public static final long JWT_TTL=24*60*60*1000;//1天 |
| 8 | + public static final int ROOT_MENU = 0;//一级菜单 | |
| 9 | + public static final String CUR_USER = "cur_user";//当前用户 | |
| 8 | 10 | } |