网关增加对关闭状态的系统禁止访问功能,使用redis对系统状态进行缓存

This commit is contained in:
winLsy
2018-03-25 22:23:02 +08:00
parent bddd819a6e
commit 1cd2afb65e
16 changed files with 210 additions and 20 deletions
+1
View File
@@ -0,0 +1 @@
FROM ubuntu:latest
@@ -2,7 +2,7 @@ package com.micro.fast.gateway.controller;
import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.gateway.filter.RecordInformationFilter;
import com.micro.fast.gateway.pojo.ServiceRank;
import com.micro.fast.gateway.pojo.vo.ServiceRank;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
@@ -0,0 +1,29 @@
package com.micro.fast.gateway.feign;
import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.gateway.feign.fallback.SystemFeignFallBack;
import feign.Param;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* <pre>
* 远程获取系统信息的类
* upms 权限管理系统
* </pre>
* @author lsy
*/
@FeignClient(fallback = SystemFeignFallBack.class,name = "upms")
public interface SystemFeign {
/**
* 根据系统的路由查询系统的状态
* @param route 用户的访问路由
* @return
*/
@GetMapping("/upmsSystem/getSystemStatusByRoute")
ServerResponse<Integer> getSystemStatusByPath(@RequestParam("route") String route);
}
@@ -0,0 +1,13 @@
package com.micro.fast.gateway.feign.fallback;
import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.gateway.feign.SystemFeign;
import org.springframework.stereotype.Component;
@Component
public class SystemFeignFallBack implements SystemFeign {
@Override
public ServerResponse getSystemStatusByPath(String path) {
return ServerResponse.errorMsg("系统繁忙,请稍后再试");
}
}
@@ -27,7 +27,7 @@ public class PreRequestFilter extends ZuulFilter {
@Override
public int filterOrder() {
return 1;
return 2;
}
@Override
@@ -39,6 +39,10 @@ public class PreRequestFilter extends ZuulFilter {
public Object run() {
//校验登录状态和访问权限
RequestContext requestContext = RequestContext.getCurrentContext();
// 如果上一个过滤器终止请求,则终止请求
if (requestContext.get(SystemIsOpenFilter.IS_SUCCESS).equals(false)){
return null;
}
HttpServletRequest request = requestContext.getRequest();
ServerResponse validate = ValidateJwtToken.validate(request, signingKey);
if (validate.isSuccess()) {
@@ -2,16 +2,12 @@ package com.micro.fast.gateway.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.Set;
import java.util.SortedMap;
/**
* 对服务访问者的信息进行记录
@@ -0,0 +1,71 @@
package com.micro.fast.gateway.filter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.gateway.service.SystemService;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/**
* 判断服务是否停止访问的过滤器
* @author lsy
*/
@Component
@Slf4j
public class SystemIsOpenFilter extends ZuulFilter {
public static final String IS_SUCCESS = "isSuccess";
@Autowired
private SystemService systemService;
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
// 获取当前请求对象
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
// 获取请求服务的路由
String requestURI = request.getRequestURI();
String[] split = requestURI.split("/");
// 根据系统路由查找系统的开放状态
boolean b = systemService.systemIsOpen("/" + split[1]);
if (b){
currentContext.setSendZuulResponse(true);
currentContext.set(IS_SUCCESS,true);
} else{
// 终止请求
currentContext.setSendZuulResponse(false);
currentContext.setResponseStatusCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
ObjectMapper objectMapper = new ObjectMapper();
try {
currentContext.setResponseBody(objectMapper.writeValueAsString(ServerResponse.errorMsg("system is closed")));
currentContext.set(IS_SUCCESS,false);
} catch (JsonProcessingException e) {
log.info("对象转换成json失败");
}
}
return null;
}
}
@@ -1,4 +1,4 @@
package com.micro.fast.gateway.pojo;
package com.micro.fast.gateway.pojo.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -0,0 +1,9 @@
package com.micro.fast.gateway.service;
/**
* 查询系统信息的服务类
* @author lsy
*/
public interface SystemService {
boolean systemIsOpen(String path);
}
@@ -0,0 +1,46 @@
package com.micro.fast.gateway.service.impl;
import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.gateway.feign.SystemFeign;
import com.micro.fast.gateway.service.SystemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class SystemServiceImpl implements SystemService{
public static final String GATEWAY_SYSTEM_STATUS = "gateway:systemStatus:";
public static final int OPEN = 1;
@Autowired
private SystemFeign systemFeign;
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Override
public boolean systemIsOpen(String path) {
// 尝试从缓存中获取系统的状态
String status = redisTemplate.opsForValue().get(GATEWAY_SYSTEM_STATUS + path);
if (StringUtils.isBlank(status)){
// 如果没有缓存,尝试从远程获取
ServerResponse<Integer> systemStatusByPath = systemFeign.getSystemStatusByPath(path);
if (systemStatusByPath.isSuccess()){
// 获取成功就进行缓存
Integer data = systemStatusByPath.getData();
redisTemplate.opsForValue().set(GATEWAY_SYSTEM_STATUS+path,String.valueOf(data),60, TimeUnit.SECONDS);
return OPEN == data;
} else {
return false;
}
} else {
return Integer.valueOf(status) == OPEN;
}
}
}
@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.PathVariable;
* 远程获取用户信息
* @author lsy
*/
@FeignClient(name = "upms",fallback = UpmsUserServiceFallback.class)
@FeignClient(fallback = UpmsUserServiceFallback.class,name= "upms")
public interface UpmsUserService {
/**
@@ -5,6 +5,7 @@ import com.micro.fast.oauth.dto.UpmsUser;
import com.micro.fast.oauth.feign.UpmsUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
@@ -312,7 +312,17 @@ CREATE TABLE IF NOT EXISTS upms_role_permission (
# 初始化管理员数据
INSERT INTO upms_user VALUE (1,'admin','$2a$10$Tqf5dVNDPBLwGSKrtK4zeuxMHY54qoJgqRQiLQZsLSsFmsWqY/gOa',NULL ,'micro-fast'
,NULL ,'18332751256','18332763730@163.com',0,0,unix_timestamp(now()),unix_timestamp(now()) ,0);
,NULL ,'18332751256','18332763133@163.com',0,0,unix_timestamp(now()),unix_timestamp(now()) ,0);
# 初始化组织数据
INSERT INTO upms_organization VALUE (1,0,'micro-fast项目部','负责micro-fast项目开源工作',unix_timestamp(now()),unix_timestamp(now()),0);
INSERT INTO upms_organization VALUE (1,0,'micro-fast项目部','负责micro-fast项目开源工作',unix_timestamp(now()),unix_timestamp(now()),0);
# 初始化系统数据
INSERT INTO upms_system (id,basepath,status,name,route,description,title,orders,ctime,version) VALUES
(1,'www.upms.com',1,'upms','/upms','权限管理系统,对后台的用户权限进行控制','权限管理系统',1,unix_timestamp(now()),1),
(2,'www.config.com',1,'config-server','/config-server','配置中心,负责配置管理配置管理','配置管理系统',2,unix_timestamp(now()),1),
(3,'www.zipkin.com',1,'monitor-zipkin','/monitor-zipkin','服务调用监控','服务调用监控系统',3,unix_timestamp(now()),1),
(4,'www.turbine.com',1,'monitor-turbine','/monitor-turbine','服务熔断监控,请求监控和','请求监控系统',4,unix_timestamp(now()),1),
(5,'www.oauth.com',1,'oauth','/oauth','认证中心','认证中心系统',5,unix_timestamp(now()),1),
(6,'www.register1.com',1,'eureka-server','/eureka-server','注册中心','高可用注册中心',6,unix_timestamp(now()),1),
(7,'www.ucenter.com',1,'ucenter','/ucenter','用户管理系统','用户管理系统',7,unix_timestamp(now()),1);
@@ -39,4 +39,7 @@ public interface UpmsSystemMapper extends SsmMapper<UpmsSystem,Integer> {
*/
@Select("select status from upms_system where id = #{id}")
int selectSystemStatus(Integer id);
@Select("select status from upms_system where route = #{route}")
int selectSystemStatusByRoute(String route);
}
@@ -7,10 +7,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
@@ -34,7 +31,7 @@ public class UpmsRolePermissionController {
return upmsRolePermissionService.batchAddRolePermission(roleId,permissionIds);
}
@ApiOperation("给角色批量删除权限")
@PostMapping("/deleteRolePermission")
@DeleteMapping("/deleteRolePermission")
public ServerResponse deleteRolePermission(@RequestParam("roleId") @NotNull(message = BaseConst.BASEMSG_PREFIX+"请传入角色id") Integer roleId,
@RequestParam("permissionIds") @NotEmpty ArrayList<Integer> permissionIds){
return upmsRolePermissionService.batchDeleteRolePermission(roleId,permissionIds);
@@ -2,6 +2,7 @@ package com.micro.fast.upms.controller;
import com.micro.fast.boot.starter.common.response.BaseConst;
import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.upms.dao.UpmsSystemMapper;
import com.micro.fast.upms.pojo.UpmsSystem;
import com.micro.fast.upms.service.UpmsSystemService;
import io.swagger.annotations.Api;
@@ -28,6 +29,9 @@ public class UpmsSystemController {
@Autowired
private UpmsSystemService<UpmsSystem,Integer> upmsSystemService;
@Autowired
private UpmsSystemMapper upmsSystemMapper;
@ApiOperation("添加信息")
@PostMapping
public ServerResponse addSystem(@Valid UpmsSystem upmsSystem) throws BindException {
@@ -56,9 +60,7 @@ public class UpmsSystemController {
@ApiOperation("修改信息")
@PutMapping
public ServerResponse updateSystem( UpmsSystem upmsSystem,
@RequestParam("id") @NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){
upmsSystem.setId(Integer.valueOf(id));
public ServerResponse updateSystem(UpmsSystem upmsSystem){
return upmsSystemService.update(upmsSystem);
}
@@ -70,9 +72,17 @@ public class UpmsSystemController {
}
@ApiOperation("根据id查询系统的状态")
@GetMapping("/getSystemIdBySystemId")
public ServerResponse getSystemIdBySystemId(
@GetMapping("/getSystemStatusBySystemId")
public ServerResponse getSystemStatusBySystemId(
@NotNull(message = BaseConst.BASEMSG_PREFIX + "请传入系统的id") @RequestParam("systemId") Integer systemId){
return upmsSystemService.getSystemStatus(systemId);
}
@ApiOperation("根据系统路由查找系统的状态")
@GetMapping("/getSystemStatusByRoute")
public ServerResponse getSystemStatusByRoute(
@NotEmpty(message = BaseConst.BASEMSG_PREFIX + "请传入系统的路由") @RequestParam("route") String route){
int i = upmsSystemMapper.selectSystemStatusByRoute(route);
return ServerResponse.successData(i);
}
}