From 536af498571ebdaea3738460f8985dc9902e4238 Mon Sep 17 00:00:00 2001
From: winLsy <18332763730@163.com>
Date: Fri, 2 Feb 2018 22:48:38 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0jwt=E4=BB=A4=E7=89=8C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ms/gateway/pom.xml | 6 ++++
.../fast/gateway/filter/PreRequestFilter.java | 27 +++++++++-------
.../fast/gateway/jwt/ValidateJwtToken.java | 31 +++++++++++++++++++
3 files changed, 53 insertions(+), 11 deletions(-)
create mode 100644 ms/gateway/src/main/java/com/micro/fast/gateway/jwt/ValidateJwtToken.java
diff --git a/ms/gateway/pom.xml b/ms/gateway/pom.xml
index 565174d..3db1336 100644
--- a/ms/gateway/pom.xml
+++ b/ms/gateway/pom.xml
@@ -69,6 +69,12 @@
org.springframework.cloud
spring-cloud-starter-zipkin
+
+
+ io.jsonwebtoken
+ jjwt
+ 0.7.0
+
org.springframework.boot
diff --git a/ms/gateway/src/main/java/com/micro/fast/gateway/filter/PreRequestFilter.java b/ms/gateway/src/main/java/com/micro/fast/gateway/filter/PreRequestFilter.java
index 8073f85..d654b83 100644
--- a/ms/gateway/src/main/java/com/micro/fast/gateway/filter/PreRequestFilter.java
+++ b/ms/gateway/src/main/java/com/micro/fast/gateway/filter/PreRequestFilter.java
@@ -1,20 +1,25 @@
package com.micro.fast.gateway.filter;
+import com.micro.fast.boot.starter.common.response.ServerResponse;
+import com.micro.fast.gateway.jwt.ValidateJwtToken;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Component;
+
import javax.servlet.http.HttpServletRequest;
+import javax.validation.Valid;
/**
- * 对options类型的跨域预请求进行快速处理,未启动
+ * 对accessToken进行校验
* @author lsy
*/
+@Component
public class PreRequestFilter extends ZuulFilter {
- /**
- * 对options类型的跨域预请求进行快速处理
- */
- private static final String OPTIONS = "OPTIONS";
+ @Value("${jwt.signingKey:msjwt}")
+ private String signingKey;
@Override
public String filterType() {
@@ -35,14 +40,14 @@ public class PreRequestFilter extends ZuulFilter {
public Object run() {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();
- String method = request.getMethod();
- if (OPTIONS.equals(method)){
- //不进行后续操作,直接过滤请求,返回200状态码
- requestContext.setSendZuulResponse(false);
- requestContext.setResponseStatusCode(HttpStatus.OK.value());
- }else{
+ ServerResponse validate = ValidateJwtToken.validate(request, signingKey);
+ if (validate.isSuccess()) {
//对请求进行后续处理
requestContext.setSendZuulResponse(true);
+ }else {
+ //不进行后续操作,返回401状态码
+ requestContext.setSendZuulResponse(false);
+ requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
}
return null;
}
diff --git a/ms/gateway/src/main/java/com/micro/fast/gateway/jwt/ValidateJwtToken.java b/ms/gateway/src/main/java/com/micro/fast/gateway/jwt/ValidateJwtToken.java
new file mode 100644
index 0000000..3c5f0a0
--- /dev/null
+++ b/ms/gateway/src/main/java/com/micro/fast/gateway/jwt/ValidateJwtToken.java
@@ -0,0 +1,31 @@
+package com.micro.fast.gateway.jwt;
+
+import com.micro.fast.boot.starter.common.response.ServerResponse;
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.Jwts;
+import org.apache.commons.lang.StringUtils;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * 校验后传入的jwtToken
+ * @author lsy
+ */
+public class ValidateJwtToken {
+ /**
+ * 校验jwtToken
+ */
+ public static ServerResponse validate(HttpServletRequest request,String signingKey){
+ String authorization = request.getHeader("Authorization");
+ String token = StringUtils.substringAfter(authorization, "bearer ");
+ //解析jwt
+ Claims body = null;
+ try {
+ body = Jwts.parser().setSigningKey(signingKey.getBytes("UTF-8")).parseClaimsJws(token).getBody();
+ } catch (UnsupportedEncodingException e) {
+ return ServerResponse.error();
+ }
+ return ServerResponse.successData(body);
+ }
+}