作者 朱兆平

JWT-token生效时长写到配置文件中

@@ -118,4 +118,6 @@ logging: @@ -118,4 +118,6 @@ logging:
118 #com.tianbo.warehouse.dao: DEBUG 118 #com.tianbo.warehouse.dao: DEBUG
119 #org.springframework.security: trace 119 #org.springframework.security: trace
120 #日志配置,输出到文本, 120 #日志配置,输出到文本,
121 - 121 +#Java Web Token 时效时间,单位秒
  122 +jwt:
  123 + max-alive: 300
@@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
25 * 集成JWT JAVA Web Token框架 25 * 集成JWT JAVA Web Token框架
26 * 前后端完全分离 26 * 前后端完全分离
27 * 前端登录验证后,每次访问系统通过在头部携带带有JWT token的Authorization:Bearer "Tokens字符窜"访问系统 27 * 前端登录验证后,每次访问系统通过在头部携带带有JWT token的Authorization:Bearer "Tokens字符窜"访问系统
  28 + * 相关集成资料 [SpringBoot使用SpringSecurity搭建基于非对称加密的JWT及前后端分离的搭建](https://blog.csdn.net/lhc0512/article/details/80563160)
28 * 已集成mybatis、mybatisGenerator、pageHelper 29 * 已集成mybatis、mybatisGenerator、pageHelper
29 * 集成定时任务框架 30 * 集成定时任务框架
30 * 目前在IMF框架中使用,打开IMF_Task里面的定时任务注释就可以启动IMF客户端功能 31 * 目前在IMF框架中使用,打开IMF_Task里面的定时任务注释就可以启动IMF客户端功能
@@ -2,8 +2,6 @@ package com.tianbo.warehouse.security.handel; @@ -2,8 +2,6 @@ package com.tianbo.warehouse.security.handel;
2 2
3 import com.fasterxml.jackson.databind.ObjectMapper; 3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import com.tianbo.warehouse.bean.AuthSuccessResponse; 4 import com.tianbo.warehouse.bean.AuthSuccessResponse;
5 -import com.tianbo.warehouse.controller.PermssionController;  
6 -import com.tianbo.warehouse.model.PERMISSION;  
7 import com.tianbo.warehouse.model.USERS; 5 import com.tianbo.warehouse.model.USERS;
8 import com.tianbo.warehouse.security.filter.JwtTokenUtil; 6 import com.tianbo.warehouse.security.filter.JwtTokenUtil;
9 import com.tianbo.warehouse.security.model.LoginType; 7 import com.tianbo.warehouse.security.model.LoginType;
@@ -12,8 +10,8 @@ import org.apache.commons.logging.Log; @@ -12,8 +10,8 @@ import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory; 10 import org.apache.commons.logging.LogFactory;
13 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Autowired;
14 import com.tianbo.warehouse.security.config.SecurityProperties; 12 import com.tianbo.warehouse.security.config.SecurityProperties;
  13 +import org.springframework.beans.factory.annotation.Value;
15 import org.springframework.security.core.Authentication; 14 import org.springframework.security.core.Authentication;
16 -import org.springframework.security.web.authentication.AuthenticationSuccessHandler;  
17 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; 15 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
18 import org.springframework.security.web.savedrequest.HttpSessionRequestCache; 16 import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
19 import org.springframework.security.web.savedrequest.RequestCache; 17 import org.springframework.security.web.savedrequest.RequestCache;
@@ -34,6 +32,9 @@ import java.util.Map; @@ -34,6 +32,9 @@ import java.util.Map;
34 public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler{ 32 public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler{
35 protected final Log logger = LogFactory.getLog(this.getClass()); 33 protected final Log logger = LogFactory.getLog(this.getClass());
36 34
  35 + @Value("${jwt.max-alive}")
  36 + protected Integer jwtMaxAlive;
  37 +
37 @Autowired 38 @Autowired
38 private ObjectMapper objectMapper; 39 private ObjectMapper objectMapper;
39 40
@@ -58,7 +59,7 @@ public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticat @@ -58,7 +59,7 @@ public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticat
58 loginedUser.setPassword(null); 59 loginedUser.setPassword(null);
59 60
60 //设置用户的TOKEN的有效时间,下面是300秒=5分钟 61 //设置用户的TOKEN的有效时间,下面是300秒=5分钟
61 - String jwtToken = JwtTokenUtil.generateToken(loginedUser.getUsername(), 300); 62 + String jwtToken = JwtTokenUtil.generateToken(loginedUser.getUsername(), jwtMaxAlive);
62 response.setHeader("Authorization",jwtToken); 63 response.setHeader("Authorization",jwtToken);
63 64
64 Map<String,Object> menuMap = permissionService.getUserMenus(loginedUser.getUserId()); 65 Map<String,Object> menuMap = permissionService.getUserMenus(loginedUser.getUserId());