From 28c643cc4d3142503dfa5234777399586e8364e9 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期六, 30 三月 2019 11:41:36 +0800
Subject: [PATCH] 添加员工服务端bug修复

---
 java110-service/src/main/java/com/java110/service/filter/JwtFilter.java |   71 +++++++++++++++++++++++------------
 1 files changed, 46 insertions(+), 25 deletions(-)

diff --git a/java110-service/src/main/java/com/java110/service/filter/JwtFilter.java b/java110-service/src/main/java/com/java110/service/filter/JwtFilter.java
index 7c7cdb0..6e94885 100644
--- a/java110-service/src/main/java/com/java110/service/filter/JwtFilter.java
+++ b/java110-service/src/main/java/com/java110/service/filter/JwtFilter.java
@@ -1,56 +1,63 @@
 package com.java110.service.filter;
 
-import com.auth0.jwt.JWT;
-import com.auth0.jwt.JWTVerifier;
-import com.auth0.jwt.algorithms.Algorithm;
-import com.auth0.jwt.exceptions.JWTVerificationException;
-import com.auth0.jwt.interfaces.Claim;
-import com.auth0.jwt.interfaces.DecodedJWT;
 import com.java110.common.constant.CommonConstant;
 import com.java110.common.constant.ResponseConstant;
 import com.java110.common.exception.FilterException;
-import com.java110.common.factory.DataTransactionFactory;
+import com.java110.core.factory.AuthenticationFactory;
+import com.java110.core.factory.DataTransactionFactory;
 import com.java110.common.util.StringUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.web.filter.GenericFilterBean;
 
-import javax.servlet.FilterChain;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
+import javax.servlet.*;
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.io.UnsupportedEncodingException;
 import java.util.Map;
 
 /**
  * Created by wuxw on 2018/5/2.
  */
-public class JwtFilter extends GenericFilterBean {
+public class JwtFilter implements Filter {
+
+    private final static Logger logger = LoggerFactory.getLogger(JwtFilter.class);
+
+    private  String[] excludedUris;
+
+    @Override
+    public void destroy() {
+        // TODO Auto-generated method stub
+    }
+
+
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+        excludedUris = filterConfig.getInitParameter("excludedUri").split(",");
+    }
 
     public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain)
             throws IOException, ServletException {
 
         final HttpServletRequest request = (HttpServletRequest) req;
         final HttpServletResponse response = (HttpServletResponse) res;
+        String uri = request.getServletPath();
+        //濡傛灉鏄� 涓嶈兘杩囨护鐨勫湴鍧�閫夋嫨璺宠繃
+        if(isExcludedUri(uri)){
+            chain.doFilter(request, response);
+            return ;
+        }
         String token = "";
         try {
             //鑾峰彇token
             token = this.getToken(request);
             try {
-                Algorithm algorithm = Algorithm.HMAC256("secret");
-                JWTVerifier verifier = JWT.require(algorithm).withIssuer("auth0").build();
-                DecodedJWT jwt = verifier.verify(token);
-
-                Map<String, Claim> claims = jwt.getClaims();
-                // Add the claim to request header
+                Map<String, String> claims = AuthenticationFactory.verifyToken(token);
                 request.setAttribute("claims", claims);
-            } catch (UnsupportedEncodingException e) {
-                logger.error("瑙f瀽token 澶辫触 锛�", e);
-                throw new FilterException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "鎮ㄨ繕娌℃湁鐧诲綍锛岃鍏堢櫥褰�");
-            } catch (JWTVerificationException e) {
+
+            } catch (Exception e) {
                 //Invalid signature/claims
                 logger.error("瑙f瀽token 澶辫触 锛�", e);
                 throw new FilterException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "鎮ㄨ繕娌℃湁鐧诲綍锛岃鍏堢櫥褰�");
@@ -63,11 +70,11 @@
                         DataTransactionFactory.pageResponseJson(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,e.getMessage(),null),
                         "UTF-8");
             }else{
-                response.sendRedirect("/error?code="+e.getResult().getCode()+"&msg="+e.getResult().getMsg());
+                response.sendRedirect("/flow/login");
             }
 
         }catch (Exception e){
-            response.sendRedirect("/error?code="+ResponseConstant.RESULT_CODE_INNER_ERROR+"&msg=閴存潈澶辫触");
+            response.sendRedirect("/flow/login");
         }
     }
 
@@ -105,4 +112,18 @@
         }
     }
 
+    private boolean isExcludedUri(String uri) {
+        if (excludedUris == null || excludedUris.length <= 0) {
+            return false;
+        }
+        for (String ex : excludedUris) {
+            uri = uri.trim();
+            ex = ex.trim();
+            if (uri.toLowerCase().matches(ex.toLowerCase().replace("*",".*")))
+                return true;
+        }
+        return false;
+    }
+
+
 }

--
Gitblit v1.8.0