| | |
| | | comment on column lf.sys_login.type is '类别:1-登录,2-校验,3-登出'; |
| | | comment on column lf.sys_login.status is '状态:1-成功,0-失败'; |
| | | comment on column lf.sys_login.descr is '描述'; |
| | | comment on column lf.sys_login.userid is '登录人ID'; |
| | | comment on column lf.sys_login.optime is '登录时间'; |
| | | comment on column lf.sys_login.userid is '登记人ID'; |
| | | comment on column lf.sys_login.optime is '登记时间'; |
| | | |
| | | /*insert into lf.sys_login (appid,ip,userid) values (1,'192.168.20.106',1); |
| | | insert into lf.sys_login (appid,ip,userid,optime) values (1,'192.168.20.106',1,'2024-01-02');*/ |
| | |
| | | loginService.insertLogin(le); |
| | | return fail("密码不正确!", null); |
| | | } |
| | | le.setStatus(1); |
| | | |
| | | Integer rows = loginService.insertLogin(le); |
| | | if (rows == 0) { |
| | |
| | | * 登出 |
| | | * |
| | | * @param req |
| | | * @param res |
| | | * @return |
| | | */ |
| | | @GetMapping(value = "/logout") |
| | | public ResponseMsg<Boolean> logout(HttpServletRequest req, HttpServletResponse res) { |
| | | public ResponseMsg<Boolean> logout(HttpServletRequest req) { |
| | | try { |
| | | String token = WebHelper.getToken(req); |
| | | if (StringHelper.isEmpty(token)) { |
| | | return fail("没有检测到令牌", false); |
| | | } |
| | | |
| | | //... |
| | | Boolean flag = tokenService.isLogin(req, res); |
| | | Boolean flag = tokenService.logout(token, req); |
| | | |
| | | return success(flag ? "用户已登录" : "用户未登录", flag); |
| | | return success(flag ? "登出成功" : "登出失败", flag); |
| | | } catch (Exception ex) { |
| | | return fail(ex.getMessage(), false); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 是/否登录 |
| | | * 检查是/否登录 |
| | | * |
| | | * @return ResponseMsg<String> |
| | | */ |
| | | @GetMapping("/isLogin") |
| | | public ResponseMsg<Boolean> isLogin(HttpServletRequest req, HttpServletResponse res) { |
| | | @GetMapping("/check") |
| | | public ResponseMsg<Boolean> check(HttpServletRequest req, HttpServletResponse res) { |
| | | try { |
| | | Boolean flag = tokenService.isLogin(req, res); |
| | | |
| | |
| | | * 获取当前用户 |
| | | * |
| | | * @param req |
| | | * @param res |
| | | * @return |
| | | */ |
| | | @GetMapping("/getCurrentUser") |
| | | public ResponseMsg<UsersEntity> getCurrentUser(HttpServletRequest req, HttpServletResponse res) { |
| | | public ResponseMsg<UsersEntity> getCurrentUser(HttpServletRequest req) { |
| | | try { |
| | | UsersEntity ue = tokenService.getCurrentUser(req); |
| | | if (ue == null) { |
| | |
| | | public List<TokenEntity> selectByPage(String token, Integer limit, Integer offset); |
| | | |
| | | /** |
| | | * 查询单条数据 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | public TokenEntity selectToken(int id); |
| | | |
| | | /** |
| | | * 根据token值查询一条记录 |
| | | * |
| | | * @param token |
| | | * @return |
| | | */ |
| | | public TokenEntity selectOneByToken(String token); |
| | | |
| | | /** |
| | | * 查询全部数据 |
| | | * |
| | | * @return |
| | | */ |
| | | public List<TokenEntity> selectTokenAll(); |
| | | |
| | | /** |
| | | * 添加数据 |
| | | * |
| | | * @param tokenEntity |
| | |
| | | public Integer updateToken(TokenEntity tokenEntity); |
| | | |
| | | /** |
| | | * 查询单条数据 |
| | | * 更新令牌为失效 |
| | | * |
| | | * @param id |
| | | * @param tokenEntity |
| | | * @return |
| | | */ |
| | | public TokenEntity selectToken(int id); |
| | | |
| | | /** |
| | | * 根据token值查询一条记录 |
| | | * @param token |
| | | * @return |
| | | */ |
| | | public TokenEntity selectOneByToken(String token); |
| | | |
| | | /** |
| | | * 查询全部数据 |
| | | * |
| | | * @return |
| | | */ |
| | | public List<TokenEntity> selectTokenAll(); |
| | | public Integer updateTokenExpire(TokenEntity tokenEntity); |
| | | } |
| | |
| | | le.setAppid(1); |
| | | le.setIp(WebHelper.getIpAddress(req)); |
| | | le.setType(1); |
| | | le.setStatus(1); |
| | | le.setUserid(userid); |
| | | le.setOptime(WebHelper.getCurrentTimestamp()); |
| | | |
| | |
| | | package com.lf.server.service.data; |
| | | |
| | | import com.lf.server.entity.all.StaticData; |
| | | import com.lf.server.entity.data.LoginEntity; |
| | | import com.lf.server.entity.data.TokenEntity; |
| | | import com.lf.server.entity.data.UsersEntity; |
| | | import com.lf.server.entity.sys.RedisCacheKey; |
| | |
| | | @Autowired |
| | | UsersService usersService; |
| | | |
| | | @Autowired |
| | | LoginService loginService; |
| | | |
| | | @Override |
| | | public Integer selectCount(String token) { |
| | | return tokenMapper.selectCount(token); |
| | |
| | | @Override |
| | | public List<TokenEntity> selectByPage(String token, Integer limit, Integer offset) { |
| | | return tokenMapper.selectByPage(token, limit, offset); |
| | | } |
| | | |
| | | @Override |
| | | public TokenEntity selectToken(int id) { |
| | | return tokenMapper.selectToken(id); |
| | | } |
| | | |
| | | @Override |
| | | public TokenEntity selectOneByToken(String token) { |
| | | return tokenMapper.selectOneByToken(token); |
| | | } |
| | | |
| | | @Override |
| | | public List<TokenEntity> selectTokenAll() { |
| | | return tokenMapper.selectTokenAll(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public TokenEntity selectToken(int id) { |
| | | return tokenMapper.selectToken(id); |
| | | } |
| | | |
| | | @Override |
| | | public TokenEntity selectOneByToken(String token) { |
| | | return tokenMapper.selectOneByToken(token); |
| | | } |
| | | |
| | | @Override |
| | | public List<TokenEntity> selectTokenAll() { |
| | | return tokenMapper.selectTokenAll(); |
| | | public Integer updateTokenExpire(TokenEntity tokenEntity) { |
| | | return tokenMapper.updateTokenExpire(tokenEntity); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 登出 |
| | | * |
| | | * @param token |
| | | * @param req |
| | | * @return |
| | | */ |
| | | public Boolean logout(String token, HttpServletRequest req) { |
| | | TokenEntity te = getEntityByToken(token); |
| | | if (te == null) { |
| | | return false; |
| | | } |
| | | |
| | | // 清除Cookie |
| | | WebHelper.deleteCookie(StaticData.TOKEN_COOKIE_KEY, req); |
| | | |
| | | // 清除缓存 |
| | | String tokenKey = RedisCacheKey.signTokenKey(token); |
| | | redisService.delete(tokenKey); |
| | | |
| | | // 获取当前用户 |
| | | UsersEntity ue = getCurrentUser(req); |
| | | if (te == null) { |
| | | return false; |
| | | } |
| | | |
| | | // db,设置令牌过期 |
| | | te.setUpdateUser(ue.getId()); |
| | | Integer rows = updateTokenExpire(te); |
| | | if (rows == 0) { |
| | | return false; |
| | | } |
| | | |
| | | // 写日志 |
| | | LoginEntity le = loginService.getNewLogin(ue.getId(), req); |
| | | le.setType(3); |
| | | |
| | | rows = loginService.insertLogin(le); |
| | | |
| | | return rows > 0; |
| | | } |
| | | |
| | | /** |
| | | * 根据令牌获取实体 |
| | | * |
| | | * @param token |
| | |
| | | <update id="updateToken"> |
| | | update lf.sys_token set token=#{token},duration=#{duration},expire=#{expire},type=#{type},ip=#{ip},update_user=#{updateUser},update_time=now() where id=#{id} |
| | | </update> |
| | | |
| | | <update id="updateTokenExpire"> |
| | | update lf.sys_token set expire=now(),update_time=now(),update_user=#{updateUser} where where id=#{id} |
| | | </update> |
| | | </mapper> |