From c1adf0e28a609075ea89cffa9c53c34486a2ce9f Mon Sep 17 00:00:00 2001 From: "DESKTOP-4U0TDEF\\20371" <2037158277@qq.com> Date: Thu, 15 Jul 2021 14:31:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/common/account/JwtLoginFilter.java | 5 +-- .../account/TokenAuthenticationService.java | 6 ++-- src/main/java/com/xkrs/dao/SysUserDao.java | 11 ++++++- .../java/com/xkrs/model/vo/SysUserVo.java | 32 ++++++++++++------- .../java/com/xkrs/service/SysUserService.java | 7 ++++ .../xkrs/service/impl/SysUserServiceImpl.java | 5 +++ 6 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/xkrs/common/account/JwtLoginFilter.java b/src/main/java/com/xkrs/common/account/JwtLoginFilter.java index c28d3ad..e30581e 100644 --- a/src/main/java/com/xkrs/common/account/JwtLoginFilter.java +++ b/src/main/java/com/xkrs/common/account/JwtLoginFilter.java @@ -3,6 +3,7 @@ package com.xkrs.common.account; import com.xkrs.common.encapsulation.OutputEncapsulation; import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.model.entity.SysUserEntity; +import com.xkrs.model.vo.SysUserVo; import com.xkrs.service.SysUserService; import com.xkrs.utils.IpUtil; import org.slf4j.Logger; @@ -100,12 +101,12 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter { } //更新用户登录信息 sysUserService.updateSysUserLogin(auth.getName(), IpUtil.getIpAddr(req)); - SysUserEntity sysUserByUserName = sysUserService.getSysUserByUserName(auth.getName()); + SysUserVo userByUserName = sysUserService.getUserByUserName(auth.getName()); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Credentials", "false"); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); - TokenAuthenticationService.addAuthentication(response, auth.getName(),auth.getAuthorities(),sysUserByUserName); + TokenAuthenticationService.addAuthentication(response, auth.getName(),auth.getAuthorities(),userByUserName); } /** diff --git a/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java b/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java index 6794190..6b8daae 100644 --- a/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java +++ b/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java @@ -3,6 +3,7 @@ package com.xkrs.common.account; import com.xkrs.common.encapsulation.OutputEncapsulation; import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.model.entity.SysUserEntity; +import com.xkrs.model.vo.SysUserVo; import com.xkrs.utils.DateTimeUtil; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; @@ -55,7 +56,7 @@ public class TokenAuthenticationService { * @param authorities */ static void addAuthentication(HttpServletResponse response, String userName, - Collection authorities,SysUserEntity sysUserEntity) { + Collection authorities, SysUserVo sysUserEntity) { Locale locale = new Locale("zh", "CN"); Map map = new HashMap(3); @@ -75,10 +76,11 @@ public class TokenAuthenticationService { LocalDateTime overTime = DateTimeUtil.stringToDateTime(sysUserEntity.getOverTime()); // 计算距离结束时间的天数作为token Duration duration = Duration.between(LocalDateTime.now(), overTime); + System.out.println("-------"+duration.toDays()); /** * 动态设置过期时间 */ - final long EXPIRATIONTIME = 60 * 60 * 24L * duration.toDays(); + final long EXPIRATIONTIME = 60 * 60 * 24 * duration.toDays(); // 生成JWT String jwt = Jwts.builder() diff --git a/src/main/java/com/xkrs/dao/SysUserDao.java b/src/main/java/com/xkrs/dao/SysUserDao.java index cb7be0e..7615138 100644 --- a/src/main/java/com/xkrs/dao/SysUserDao.java +++ b/src/main/java/com/xkrs/dao/SysUserDao.java @@ -82,10 +82,19 @@ public interface SysUserDao extends JpaRepository { * 查询用户信息 * @return */ - @Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,countyCode,activeFlag,addTime) " + + @Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,countyCode,activeFlag,addTime,dayNum,overTime) " + "from SysUserEntity") List selectAll(); + /** + * 根据用户名查询实体信息 + * @param userName + * @return + */ + @Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,countyCode,activeFlag,addTime,dayNum,overTime) " + + "from SysUserEntity where userName = :userName") + SysUserVo selectUserByUserName(String userName); + /** * 启用 * @param userId diff --git a/src/main/java/com/xkrs/model/vo/SysUserVo.java b/src/main/java/com/xkrs/model/vo/SysUserVo.java index 4524269..cfabeec 100644 --- a/src/main/java/com/xkrs/model/vo/SysUserVo.java +++ b/src/main/java/com/xkrs/model/vo/SysUserVo.java @@ -20,13 +20,19 @@ public class SysUserVo implements Serializable { private String addTime; - public SysUserVo(Integer id, String reallyName, String telephone, String countyCode, Integer activeFlag, String addTime) { + private Integer dayNum; + + private String overTime; + + public SysUserVo(Integer id, String reallyName, String telephone, String countyCode, Integer activeFlag, String addTime, Integer dayNum, String overTime) { this.id = id; this.reallyName = reallyName; this.telephone = telephone; this.countyCode = countyCode; this.activeFlag = activeFlag; this.addTime = addTime; + this.dayNum = dayNum; + this.overTime = overTime; } public Integer getId() { @@ -77,16 +83,20 @@ public class SysUserVo implements Serializable { this.addTime = addTime; } - @Override - public String toString() { - return "SysUserVo{" + - "id=" + id + - ", reallyName='" + reallyName + '\'' + - ", telephone='" + telephone + '\'' + - ", countyCode='" + countyCode + '\'' + - ", activeFlag=" + activeFlag + - ", addTime='" + addTime + '\'' + - '}'; + public Integer getDayNum() { + return dayNum; + } + + public void setDayNum(Integer dayNum) { + this.dayNum = dayNum; + } + + public String getOverTime() { + return overTime; + } + + public void setOverTime(String overTime) { + this.overTime = overTime; } } diff --git a/src/main/java/com/xkrs/service/SysUserService.java b/src/main/java/com/xkrs/service/SysUserService.java index 44b57da..c88d920 100644 --- a/src/main/java/com/xkrs/service/SysUserService.java +++ b/src/main/java/com/xkrs/service/SysUserService.java @@ -96,4 +96,11 @@ public interface SysUserService { * @return */ String adminUpdatePassword(Integer userId,String newPassword,String confirmPassword); + + /** + * 获取系统用户实体根据用户名 + * @param userName + * @return + */ + SysUserVo getUserByUserName(String userName); } diff --git a/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java b/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java index a50cf39..ae19eae 100644 --- a/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java @@ -232,4 +232,9 @@ public class SysUserServiceImpl implements SysUserService { return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"密码修改成功",locale); } + @Override + public SysUserVo getUserByUserName(String userName) { + return sysUserDao.selectUserByUserName(userName); + } + }