From 6f7f8a6c6ab4db862df1b16cb50e45aedc90afb1 Mon Sep 17 00:00:00 2001 From: Zed Date: Mon, 4 Nov 2019 11:43:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=85=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=90=8D=E7=99=BB=E5=BD=95=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin.xml | 3 +- .../DemoGlobalRequestFilterBridge.java | 42 +++++++++++++++---- .../demo/request/DemoRequestFilterBridge.java | 3 +- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/plugin.xml b/plugin.xml index 4288b6b..3a9567c 100644 --- a/plugin.xml +++ b/plugin.xml @@ -5,10 +5,11 @@ yes 1.0 10.0 - 2018-11-20 + 2019-06-18 author [2018-11-05]初始化插件。
]]>
diff --git a/src/main/java/com/fr/plugin/demo/request/DemoGlobalRequestFilterBridge.java b/src/main/java/com/fr/plugin/demo/request/DemoGlobalRequestFilterBridge.java index 9d0114b..3a31242 100644 --- a/src/main/java/com/fr/plugin/demo/request/DemoGlobalRequestFilterBridge.java +++ b/src/main/java/com/fr/plugin/demo/request/DemoGlobalRequestFilterBridge.java @@ -8,7 +8,9 @@ import com.fr.decision.webservice.utils.DecisionServiceConstants; import com.fr.decision.webservice.v10.login.LoginService; import com.fr.decision.webservice.v10.login.TokenResource; import com.fr.decision.webservice.v10.user.UserService; +import com.fr.general.ComparatorUtils; import com.fr.log.FineLoggerFactory; +import com.fr.security.JwtUtils; import com.fr.stable.StringUtils; import javax.servlet.FilterChain; @@ -32,24 +34,46 @@ public class DemoGlobalRequestFilterBridge extends AbstractGlobalRequestFilterPr String username = NetworkHelper.getHTTPRequestParameter(req, "username"); try { if (StringUtils.isNotEmpty(username)) { - FineLoggerFactory.getLogger().info("current username:" + username); - User user = UserService.getInstance().getUserByUserName(username); + FineLoggerFactory.getLogger().info("username:" + username); + //根据用户名找到对应用户 + User user = UserService.getInstance().getUserByUserName(username); //第二步 + //用户不存在抛异常 if (user == null) { throw new UserNotExistException(); } - String oldToken = TokenResource.COOKIE.getToken(req); - if (oldToken == null) { + //尝试获取当前浏览器已有的token + String oldToken = TokenResource.COOKIE.getToken(req); //第三步 + //token不存在说明第一次登录 || token过期了 || 用户名切换了 这3种情况都要进行重新登录 + if (oldToken == null || !checkTokenValid(req, oldToken, username)) { //第四步 + //10.0后台登录方法,生成一份新的token String token = LoginService.getInstance().login(req, res, username); + //把token塞进请求内,这样请求放行之后,就可通过后台的登录校验 req.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, token); - filterChain.doFilter(req, res); - } else { - filterChain.doFilter(req, res); } - } else { - filterChain.doFilter(req, res); } + //放行请求 + filterChain.doFilter(req, res); } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); } } + + + /** + * 校验token是否有效 + */ + private boolean checkTokenValid(HttpServletRequest req, String token, String currentUserName) { + try { + //当前登录用户和token对应的用户名不同,需要重新生成token + if (!ComparatorUtils.equals(currentUserName, JwtUtils.parseJWT(token).getSubject())) { + FineLoggerFactory.getLogger().info("username changed:" + currentUserName); + return false; + } + + return LoginService.getInstance().isLogged(req); + } catch (Exception ignore) { + } + + return false; + } } diff --git a/src/main/java/com/fr/plugin/demo/request/DemoRequestFilterBridge.java b/src/main/java/com/fr/plugin/demo/request/DemoRequestFilterBridge.java index 3b80a99..bfa7bd4 100644 --- a/src/main/java/com/fr/plugin/demo/request/DemoRequestFilterBridge.java +++ b/src/main/java/com/fr/plugin/demo/request/DemoRequestFilterBridge.java @@ -6,7 +6,6 @@ import com.fr.intelli.record.Original; import com.fr.log.FineLoggerFactory; import com.fr.record.analyzer.EnableMetrics; -import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -16,7 +15,7 @@ import java.io.IOException; public class DemoRequestFilterBridge extends AbstractEmbedRequestFilterProvider { @Override - @Focus(id="com.fr.plugin.demo.request.filter", text = "", source = Original.PLUGIN) + @Focus(id = "com.fr.plugin.demo.request.filter", text = "", source = Original.PLUGIN) public void filter(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { FineLoggerFactory.getLogger().info("=========Test Filter========"); }