用户缓存存储优化,登录成功会根据用户名缓存用户token,根据token缓存用户具体信息.
新增用户锁定接口
正在显示
5 个修改的文件
包含
51 行增加
和
6 行删除
@@ -90,11 +90,27 @@ public class UserController { | @@ -90,11 +90,27 @@ public class UserController { | ||
90 | public ResultJson updateUserById(@Validated(UpdateUser.class) @RequestBody KakoUser user){ | 90 | public ResultJson updateUserById(@Validated(UpdateUser.class) @RequestBody KakoUser user){ |
91 | user.setPassword(null); | 91 | user.setPassword(null); |
92 | int i = userService.updateByPrimaryKeySelective(user); | 92 | int i = userService.updateByPrimaryKeySelective(user); |
93 | - ResultJson resultJson = new ResultJson(); | ||
94 | return i==1 ? new ResultJson("200","success") :new ResultJson("500","update faild"); | 93 | return i==1 ? new ResultJson("200","success") :new ResultJson("500","update faild"); |
95 | 94 | ||
96 | } | 95 | } |
97 | 96 | ||
97 | + @LogAnnotation(moduleName = "用户锁定",operate = "用户锁定") | ||
98 | + @PutMapping("/lock") | ||
99 | + public ResultJson lockUserById(@Validated(UpdateUser.class) @RequestBody KakoUser user){ | ||
100 | + KakoUser kakoUser = new KakoUser(); | ||
101 | + kakoUser.setId(user.getId()); | ||
102 | + kakoUser.setLoginFlag(user.getLoginFlag()); | ||
103 | + | ||
104 | + int i = userService.updateByPrimaryKeySelective(kakoUser); | ||
105 | + //删除用户token缓存 及时生效锁定账号 | ||
106 | + if (i>0){ | ||
107 | + String userTokenStr = redisUtils.get(KakoUser.TOKEN_KEY + user.getLoginName()); | ||
108 | + redisUtils.del(userTokenStr); | ||
109 | + } | ||
110 | + return i==1 ? new ResultJson("200","success") :new ResultJson("500","lock user faild"); | ||
111 | + | ||
112 | + } | ||
113 | + | ||
98 | @LogAnnotation(moduleName = "用户管理",operate = "用户密码修改") | 114 | @LogAnnotation(moduleName = "用户管理",operate = "用户密码修改") |
99 | @PutMapping("/password") | 115 | @PutMapping("/password") |
100 | public ResultJson updateUserPassById(@RequestBody KakoUser user){ | 116 | public ResultJson updateUserPassById(@RequestBody KakoUser user){ |
@@ -114,7 +130,6 @@ public class UserController { | @@ -114,7 +130,6 @@ public class UserController { | ||
114 | } | 130 | } |
115 | 131 | ||
116 | int i = userService.insertSelective(user); | 132 | int i = userService.insertSelective(user); |
117 | - ResultJson resultJson = new ResultJson(); | ||
118 | return i==1 ? new ResultJson("200","新建账户成功") :new ResultJson("500","insert faild"); | 133 | return i==1 ? new ResultJson("200","新建账户成功") :new ResultJson("500","insert faild"); |
119 | 134 | ||
120 | } | 135 | } |
@@ -124,7 +139,6 @@ public class UserController { | @@ -124,7 +139,6 @@ public class UserController { | ||
124 | public ResultJson delUser(@RequestBody KakoUser user,HttpServletRequest request,HttpServletResponse response){ | 139 | public ResultJson delUser(@RequestBody KakoUser user,HttpServletRequest request,HttpServletResponse response){ |
125 | //String username = getusername(); | 140 | //String username = getusername(); |
126 | int i = userService.deleteByPrimaryKey(user); | 141 | int i = userService.deleteByPrimaryKey(user); |
127 | - ResultJson resultJson = new ResultJson(); | ||
128 | return i==1 ? new ResultJson("200","删除账户成功") :new ResultJson("500","delete faild"); | 142 | return i==1 ? new ResultJson("200","删除账户成功") :new ResultJson("500","delete faild"); |
129 | } | 143 | } |
130 | 144 |
@@ -13,6 +13,7 @@ public interface KakoUserMapper { | @@ -13,6 +13,7 @@ public interface KakoUserMapper { | ||
13 | 13 | ||
14 | KakoUser selectByPrimaryKey(String id); | 14 | KakoUser selectByPrimaryKey(String id); |
15 | 15 | ||
16 | + | ||
16 | int updateByPrimaryKeySelective(KakoUser record); | 17 | int updateByPrimaryKeySelective(KakoUser record); |
17 | 18 | ||
18 | int updateByPrimaryKey(KakoUser record); | 19 | int updateByPrimaryKey(KakoUser record); |
@@ -21,6 +22,8 @@ public interface KakoUserMapper { | @@ -21,6 +22,8 @@ public interface KakoUserMapper { | ||
21 | 22 | ||
22 | List<KakoUser> selectAllUser(KakoUser record); | 23 | List<KakoUser> selectAllUser(KakoUser record); |
23 | 24 | ||
25 | + List<KakoUser> selectOnlineUser(); | ||
26 | + | ||
24 | int lockUser(KakoUser record); | 27 | int lockUser(KakoUser record); |
25 | 28 | ||
26 | 29 |
@@ -18,6 +18,8 @@ import java.util.Date; | @@ -18,6 +18,8 @@ import java.util.Date; | ||
18 | import java.util.List; | 18 | import java.util.List; |
19 | 19 | ||
20 | public class KakoUser implements UserDetails { | 20 | public class KakoUser implements UserDetails { |
21 | + public static String TOKEN_KEY = "user:"; | ||
22 | + | ||
21 | private String id; | 23 | private String id; |
22 | 24 | ||
23 | private String companyId; | 25 | private String companyId; |
@@ -67,6 +69,8 @@ public class KakoUser implements UserDetails { | @@ -67,6 +69,8 @@ public class KakoUser implements UserDetails { | ||
67 | 69 | ||
68 | private String token; | 70 | private String token; |
69 | 71 | ||
72 | + private Boolean online; | ||
73 | + | ||
70 | @JSONField(serialzeFeatures= {SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty}) | 74 | @JSONField(serialzeFeatures= {SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty}) |
71 | private List<ROLE> roles; | 75 | private List<ROLE> roles; |
72 | 76 | ||
@@ -269,6 +273,14 @@ public class KakoUser implements UserDetails { | @@ -269,6 +273,14 @@ public class KakoUser implements UserDetails { | ||
269 | this.token = token; | 273 | this.token = token; |
270 | } | 274 | } |
271 | 275 | ||
276 | + public Boolean getOnline() { | ||
277 | + return online; | ||
278 | + } | ||
279 | + | ||
280 | + public void setOnline(Boolean online) { | ||
281 | + this.online = online; | ||
282 | + } | ||
283 | + | ||
272 | /** | 284 | /** |
273 | * | 285 | * |
274 | * @return 账户未过期 | 286 | * @return 账户未过期 |
src/main/java/com/tianbo/warehouse/security/handel/kakologin/MyKakoAuthenticationSuccessHandler.java
@@ -53,9 +53,11 @@ public class MyKakoAuthenticationSuccessHandler extends SavedRequestAwareAuthent | @@ -53,9 +53,11 @@ public class MyKakoAuthenticationSuccessHandler extends SavedRequestAwareAuthent | ||
53 | 53 | ||
54 | @Autowired | 54 | @Autowired |
55 | RedisUtils redisUtils; | 55 | RedisUtils redisUtils; |
56 | + | ||
56 | @Override | 57 | @Override |
57 | public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { | 58 | public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { |
58 | logger.info("登录成功"); | 59 | logger.info("登录成功"); |
60 | + int expirationSeconds = 3600*24*7; | ||
59 | if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())){ | 61 | if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())){ |
60 | //将 authention 信息打包成json格式返回 | 62 | //将 authention 信息打包成json格式返回 |
61 | response.setContentType("application/json;charset=UTF-8"); | 63 | response.setContentType("application/json;charset=UTF-8"); |
@@ -77,7 +79,8 @@ public class MyKakoAuthenticationSuccessHandler extends SavedRequestAwareAuthent | @@ -77,7 +79,8 @@ public class MyKakoAuthenticationSuccessHandler extends SavedRequestAwareAuthent | ||
77 | loginedUser.setToken(jwtToken); | 79 | loginedUser.setToken(jwtToken); |
78 | //这里将登录成功的[user]对象数据写入redis缓存,KEY为token value为user的JSON对象 | 80 | //这里将登录成功的[user]对象数据写入redis缓存,KEY为token value为user的JSON对象 |
79 | String json = JSON.toJSONString(user); | 81 | String json = JSON.toJSONString(user); |
80 | - redisUtils.set(jwtToken, json,3600*24*7); | 82 | + redisUtils.set(jwtToken, json,expirationSeconds); |
83 | + redisUtils.set(KakoUser.TOKEN_KEY + user.getUsername(),jwtToken,expirationSeconds); | ||
81 | Map<String,Object> menuMap = permissionService.getUserMenusKako(user.getId()); | 84 | Map<String,Object> menuMap = permissionService.getUserMenusKako(user.getId()); |
82 | //返回用户信息和用户可访问的目录列表 | 85 | //返回用户信息和用户可访问的目录列表 |
83 | response.getWriter().write(objectMapper.writeValueAsString(new AuthSuccessResponse(loginedUser,menuMap))); | 86 | response.getWriter().write(objectMapper.writeValueAsString(new AuthSuccessResponse(loginedUser,menuMap))); |
@@ -23,11 +23,12 @@ | @@ -23,11 +23,12 @@ | ||
23 | <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" /> | 23 | <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" /> |
24 | <result column="remarks" property="remarks" jdbcType="VARCHAR" /> | 24 | <result column="remarks" property="remarks" jdbcType="VARCHAR" /> |
25 | <result column="del_flag" property="delFlag" jdbcType="CHAR" /> | 25 | <result column="del_flag" property="delFlag" jdbcType="CHAR" /> |
26 | + <result column="online" property="online" jdbcType="BOOLEAN" /> | ||
26 | </resultMap> | 27 | </resultMap> |
27 | <sql id="Base_Column_List" > | 28 | <sql id="Base_Column_List" > |
28 | id, company_id, office_id, login_name, password, no, name, email, phone, mobile, | 29 | id, company_id, office_id, login_name, password, no, name, email, phone, mobile, |
29 | user_type, photo, login_ip, login_date, login_flag, create_by, create_date, update_by, | 30 | user_type, photo, login_ip, login_date, login_flag, create_by, create_date, update_by, |
30 | - update_date, remarks, del_flag | 31 | + update_date, remarks, del_flag, online |
31 | </sql> | 32 | </sql> |
32 | <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > | 33 | <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > |
33 | select | 34 | select |
@@ -54,6 +55,14 @@ | @@ -54,6 +55,14 @@ | ||
54 | from sys_user | 55 | from sys_user |
55 | where login_name = #{login_name,jdbcType=VARCHAR} | 56 | where login_name = #{login_name,jdbcType=VARCHAR} |
56 | </select> | 57 | </select> |
58 | + | ||
59 | + <select id="selectOnlineUser" resultMap="BaseResultMap" > | ||
60 | + select | ||
61 | + <include refid="Base_Column_List" /> | ||
62 | + from sys_user | ||
63 | + where online > 0; | ||
64 | + </select> | ||
65 | + | ||
57 | <update id="deleteByPrimaryKey" parameterType="java.lang.String" > | 66 | <update id="deleteByPrimaryKey" parameterType="java.lang.String" > |
58 | update sys_user | 67 | update sys_user |
59 | set | 68 | set |
@@ -274,6 +283,9 @@ | @@ -274,6 +283,9 @@ | ||
274 | <if test="delFlag != null" > | 283 | <if test="delFlag != null" > |
275 | del_flag = #{delFlag,jdbcType=CHAR}, | 284 | del_flag = #{delFlag,jdbcType=CHAR}, |
276 | </if> | 285 | </if> |
286 | + <if test="online != null" > | ||
287 | + online = #{online,jdbcType=BOOLEAN}, | ||
288 | + </if> | ||
277 | </set> | 289 | </set> |
278 | where id = #{id,jdbcType=VARCHAR} | 290 | where id = #{id,jdbcType=VARCHAR} |
279 | </update> | 291 | </update> |
@@ -298,7 +310,8 @@ | @@ -298,7 +310,8 @@ | ||
298 | update_by = #{updateBy,jdbcType=VARCHAR}, | 310 | update_by = #{updateBy,jdbcType=VARCHAR}, |
299 | update_date = #{updateDate,jdbcType=TIMESTAMP}, | 311 | update_date = #{updateDate,jdbcType=TIMESTAMP}, |
300 | remarks = #{remarks,jdbcType=VARCHAR}, | 312 | remarks = #{remarks,jdbcType=VARCHAR}, |
301 | - del_flag = #{delFlag,jdbcType=CHAR} | 313 | + del_flag = #{delFlag,jdbcType=CHAR}, |
314 | + online = #{online,jdbcType=BOOLEAN} | ||
302 | where id = #{id,jdbcType=VARCHAR} | 315 | where id = #{id,jdbcType=VARCHAR} |
303 | </update> | 316 | </update> |
304 | 317 |
-
请 注册 或 登录 后发表评论