增加jwt令牌
This commit is contained in:
@@ -69,6 +69,12 @@
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zipkin</artifactId>
|
||||
</dependency>
|
||||
<!--jwt令牌解析-->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>0.7.0</version>
|
||||
</dependency>
|
||||
<!--测试-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user