一、pom配置文件
<groupId>com.java</groupId>
<artifactId>zuul</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jjwt.version>0.9.1</jjwt.version>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!--基础包 start-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--基础包 end-->
<!--zuul start-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--fastjson start-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<!--fastjson end-->
<!--lombok start-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!--lombok end-->
<!--jwt start-->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.3.0</version>
</dependency>
<!--jwt end-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
二、配置文件设置
##端口号
server.port=8201
spring.application.name=zuul
eureka.client.service-url.defaultZone=http://localhost:8200/eureka/
ribbon.ReadTimeout= 60000
ribbon.ConnectTimeout= 60000
##测试项目
zuul.routes.apply1.path=/apply1/**
zuul.routes.apply1.service-id=apply1
##测试项目
三、token实现
3.1、token过滤器实现
package com.java.zuul.filters;
import com.alibaba.fastjson.JSONObject;
import com.java.zuul.jwt.JWTUtil;
import com.java.zuul.util.Cache;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.java.Log;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
@Component
@Log
public class TokenFilter extends ZuulFilter {
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();
//完整路径接口
String url = request.getRequestURI();
String local_url = "/login";
/**
* 如果是登录接口不进行token验证
*/
if (local_url.equals(url)){
return false;
}else{
return true;
}
}
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String token_header = request.getHeader("token") == null ? "" : request.getHeader("token");
if (token_header.equals("")){
try {
ctx.setSendZuulResponse(false);
ctx.getResponse().setContentType("text/html;charset=utf-8");
ctx.getResponse().getWriter().write("{\"code\": -1,\"message\": \"必须添加token验证\"}");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
JSONObject jsonObject = JSONObject.parseObject(Cache.getCache(token_header).toString());
if (jsonObject.getInteger("state") == 0){
try {
ctx.setSendZuulResponse(false);
ctx.getResponse().setContentType("text/html;charset=utf-8");
ctx.getResponse().getWriter().write("{\"code\": -1,\"message\": \"token值过期或不存在\"}");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
JSONObject accout = jsonObject.getJSONObject("account");
String username = accout.getString("username");
String secret = accout.getString("secret");
if(!JWTUtil.verify(token_header,username,secret)){
try {
ctx.setSendZuulResponse(false);
ctx.getResponse().setContentType("text/html;charset=utf-8");
ctx.getResponse().getWriter().write("{\"code\": -1,\"message\": \"token验证失败\"}");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 通过登录名称获取权限接口list,在跟当前的url路径对比,
* 如果相等说明有这个接口的权限
*/
//完整路径接口
String url = request.getRequestURI();
log.info("url1"+url);
return null;
}
}
3.2、hashmap缓存实现
package com.java.zuul.util;
import com.alibaba.fastjson.JSONObject;
import java.util.concurrent.ConcurrentHashMap;
public class Cache {
private static ConcurrentHashMap<String, Object> cacheMap = new ConcurrentHashMap<>();
/**
* 获取缓存的对象
*
* @param account
* @return
*/
public static Object getCache(String account) {
JSONObject jsonObject = new JSONObject();
// 如果缓冲中有该账号,则返回value
if (cacheMap.containsKey(account)) {
jsonObject.put("state",1);
jsonObject.put("account",cacheMap.get(account));
} else {
jsonObject.put("state",0);
}
return jsonObject;
}
/**
* 设置缓存
*
* @param key
*/
public static void setCache(String key,Object value) {
// 一般是进行数据库查询,将查询的结果进行缓存
cacheMap.put(key, value);
}
/**
* 移除缓存信息
*
* @param account
*/
public static void removeCache(String account) {
cacheMap.remove(account);
}
}
3.3、返回结果封装
package com.java.zuul.result;
public class Result {
private String msg;
private Integer status;
private Object data;
public static Result newAppResult(String msg, Integer status, Object data){
return new Result(msg, status, data);
}
public Result(String msg, Integer status, Object data) {
this.msg = msg;
this.status = status;
this.data = data;
}
public static Result OK(Object object){
return new Result("success",1,object);
}
public static Result OK(String message, Object object){
return new Result(message,1,object);
}
public static Result ERROR(String message){
return new Result(message,-1,null);
}
public static Result ERROR(String message, Integer status){
return new Result(message,status,null);
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
3.4、登录功能实现
package com.java.zuul.controller;
import com.alibaba.fastjson.JSONObject;
import com.java.zuul.jwt.JWTUtil;
import com.java.zuul.result.Result;
import com.java.zuul.util.Cache;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
/**
* 用户登录同时返回jwt校验码
* @param username
* @param secret
* @return
*/
@RequestMapping(value = "/login")
public Object login(String username,String secret){
if ("cdy".equals(username) && "111111".equals(secret)){
String jwt =JWTUtil.sign(username,secret);
JSONObject user = new JSONObject();
user.put("username",username);
user.put("secret",secret);
Cache.setCache(jwt,user);
return Result.OK(jwt);
} else {
return Result.ERROR("账号密码错误!");
}
}
/**
* 校验token是否失败
* @param token
* @param username
* @param secret
* @return
*/
@RequestMapping(value = "verify")
public Object verify(String token, String username, String secret){
if (JWTUtil.verify(token,username,secret)){
return Result.OK("校验成功!");
} else {
return Result.ERROR("校验失败");
}
}
}
四、限流实现
package com.java.zuul.filters;
import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.java.Log;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.*;
@Component
@Log
public class RateLimiterFilter extends ZuulFilter {
//测试接口令牌数量
private static RateLimiter RATE_LIMITER = RateLimiter.create(1);
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
// 执行顺序为1,值越小执行顺行越靠前
// return SEND_ERROR_FILTER_ORDER;
return 3;
}
@Override
public boolean shouldFilter() {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();
//完整路径接口
String url = request.getRequestURI();
log.info("url3:"+url);
//去掉最后一层路径接口(例如:/instance/{serviceId})后面的参数需要去掉才能判断,最好不要写这样的接口
String lastUrl = url.substring(0,url.lastIndexOf("/"));
//只对订单接口限流
if ("/apply1/test".equalsIgnoreCase(url)) {
return true;
}
return false;
}
@Override
public Object run() throws ZuulException {
RequestContext requestContext = RequestContext.getCurrentContext();
//就相当于每调用一次tryAcquire()方法,令牌数量减1,当1000个用完后,那么后面进来的用户无法访问上面接口
//当然这里只写类上面一个接口,可以这么写,实际可以在这里要加一层接口判断。
if (!RATE_LIMITER.tryAcquire()) {
HttpStatus httpStatus = HttpStatus.TOO_MANY_REQUESTS;
requestContext.setSendZuulResponse(false);
//HttpStatus.TOO_MANY_REQUESTS.value()里面有静态代码常量
requestContext.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
// throw new ZuulException(
// httpStatus.getReasonPhrase(),
// httpStatus.value(),
// httpStatus.getReasonPhrase()
// );
try {
requestContext.setSendZuulResponse(false);
requestContext.getResponse().setContentType("text/html;charset=utf-8");
requestContext.getResponse().getWriter().write("{\"code\": -1,\"message\": \"访问次数过多\"}");
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}