校验apikey,但是异常未生效,头大!!!

master
kezhen0805 2 years ago
parent b882a41e5e
commit 7d5096823d

@ -23,20 +23,27 @@ public class CacheController {
@PostMapping(value = "/cache/hmset/{key}") @PostMapping(value = "/cache/hmset/{key}")
public void hmset(@PathVariable(value = "key") String key, @RequestBody Map<String, Object> map) { public void hmset(@PathVariable(value = "key") String key, @RequestBody Map<String, Object> map) {
log.info("【缓存模块】hmset方法存储key = {},存储value = {}", key, map); log.info("【缓存模块】hmset方法存储key = {},存储value = {}", key, map);
redisClient.putMap(key,map); redisClient.putMap(key, map);
} }
@PostMapping(value = "/cache/set/{key}") @PostMapping(value = "/cache/set/{key}")
public void set(@PathVariable(value = "key") String key, @RequestParam(value = "value") String value){ public void set(@PathVariable(value = "key") String key, @RequestParam(value = "value") Object value) {
log.info("【缓存模块】set方法存储key = {},存储value = {}", key, value); log.info("【缓存模块】set方法存储key = {},存储value = {}", key, value);
redisClient.set(key,value); redisClient.set(key, value);
} }
@PostMapping(value = "/cache/sadd/{key}") @PostMapping(value = "/cache/sadd/{key}")
public void sadd(@PathVariable(value = "key") String key, @RequestBody Map<String,Object>... value){ public void sadd(@PathVariable(value = "key") String key, @RequestBody Map<String, Object>... value) {
log.info("【缓存模块】sadd 方法存储key = {},存储value = {}", key, value); log.info("【缓存模块】sadd 方法存储key = {},存储value = {}", key, value);
redisClient.sAdd(key,value); redisClient.sAdd(key, value);
} }
@GetMapping("/cache/hgetall/{key}")
public Map hGetAll(@PathVariable(value = "key") String key) {
log.info("【缓存模块】hGetAll 方法获取key = {} 的数据", key);
Map<String, Object> value = redisClient.getMap(key);
log.info("【缓存模块】hGetAll 方法获取key = {} 的数据 value = {}", key, value);
return value;
}
} }

@ -0,0 +1,26 @@
package com.mashibing.constant;
/**
*
* @author kezhen
* @date 2022/12/29
* @description
*/
public interface CacheConstant {
/**
*
*/
String CLIENT_BUSINESS = "client_business:";
/**
*
*/
String CLIENT_SIGN = "client_sign:";
/**
*
*/
String CLIENT_TEMPLATE = "client_template:";
/**
*
*/
String CLIENT_BALANCE = "client_balance:";
}

@ -0,0 +1,27 @@
package com.mashibing.enums;
import lombok.Getter;
/**
* @author kezhen
* @date 2022/12/29
* @description
*/
@Getter
public enum ExceptionEnums {
ERROR_APIKEY(-1, "非法的apikey"),
IP_NOT_WHITE(-2, "请求的ip不在白名单内"),
ERROR_SIGN(-3, "无可用签名"),
ERROR_TEMPLATE(-4, "无可用签名"),
ERROR_MOBILE(-5, "手机号格式不正确"),
BALANCE_NOT_ENOUGH(-6, "余额不足"),
PARAMETER_ERROR(-10,"参数不合法"),
;
private Integer code;
private String msg;
ExceptionEnums(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}

@ -0,0 +1,25 @@
package com.mashibing.exceptiion;
import com.mashibing.enums.ExceptionEnums;
import lombok.Getter;
/**
* @author kezhen
* @date 2022/12/29
* @description
*/
@Getter
public class ApiException extends RuntimeException {
private Integer code;
public ApiException(String message, Integer code) {
super(message);
this.code = code;
}
public ApiException(ExceptionEnums enums) {
super(enums.getMsg());
this.code = enums.getCode();
}
}

@ -20,7 +20,7 @@ public interface CacheClient {
void hmset(@PathVariable(value = "key") String key, @RequestBody Map<String,Object> map); void hmset(@PathVariable(value = "key") String key, @RequestBody Map<String,Object> map);
@PostMapping(value = "/cache/set/{key}") @PostMapping(value = "/cache/set/{key}")
void set(@PathVariable(value = "key") String key, @RequestParam(value = "value") String value); void set(@PathVariable(value = "key") String key, @RequestParam(value = "value") Object value);
@PostMapping(value = "/cache/sadd/{key}") @PostMapping(value = "/cache/sadd/{key}")
void sadd(@PathVariable(value = "key") String key, @RequestBody Map<String,Object>... maps); void sadd(@PathVariable(value = "key") String key, @RequestBody Map<String,Object>... maps);

@ -8,6 +8,25 @@ public class ClientBalance {
private long balance; private long balance;
private java.sql.Timestamp created; private java.sql.Timestamp created;
private long createId; private long createId;
@Override
public String toString() {
return "ClientBalance{" +
"id=" + id +
", clientId=" + clientId +
", balance=" + balance +
", created=" + created +
", createId=" + createId +
", updated=" + updated +
", updateId=" + updateId +
", isDelete=" + isDelete +
", extend1='" + extend1 + '\'' +
", extend2='" + extend2 + '\'' +
", extend3='" + extend3 + '\'' +
", extend4='" + extend4 + '\'' +
'}';
}
private java.sql.Timestamp updated; private java.sql.Timestamp updated;
private long updateId; private long updateId;
private long isDelete; private long isDelete;

@ -0,0 +1,15 @@
package com.mashibing.test.mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* @author kezhen
* @date 2022/12/28
* @description
*/
public interface ClientBalanceMapper {
@Select("select balance from client_balance where client_id = #{clientId}")
Long findByClientId(@Param("clientId") Integer clientId);
}

@ -0,0 +1,33 @@
package com.mashibing.test.mapper;
import com.mashibing.test.client.CacheClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
/**
* @author kezhen
* @date 2022/12/28
* @description
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ClientBalanceMapperTest {
@Autowired
private ClientBalanceMapper clientBalanceMapper;
@Autowired
private CacheClient cacheClient;
@Test
public void findByClientId() {
Long balance = clientBalanceMapper.findByClientId(1);
System.out.println("balance = " + balance);
cacheClient.set("client_balance:1", balance);
}
}

@ -0,0 +1,22 @@
package com.mashibing.advice;
import com.mashibing.api.util.R;
import com.mashibing.api.vo.ResultVO;
import com.mashibing.exceptiion.ApiException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author kezhen
* @date 2022/12/29
* @description
*/
@RestControllerAdvice
public class ApiExceptionHander {
@ExceptionHandler(ApiException.class)
public ResultVO apiException(ApiException ex){
System.out.println("exception.getCode() = " + ex.getCode());
return R.error(ex);
}
}

@ -1,6 +1,10 @@
package com.mashibing.api.client; package com.mashibing.api.client;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.Map;
/** /**
* @author kezhen * @author kezhen
@ -8,5 +12,8 @@ import org.springframework.cloud.openfeign.FeignClient;
* @description * @description
*/ */
@FeignClient(value = "beacon-cache") @FeignClient(value = "beacon-cache")
public class BeaconCacheClient { public interface BeaconCacheClient {
@GetMapping("/cache/hgetall/{key}")
Map hGetAll(@PathVariable(value = "key") String key);
} }

@ -1,11 +1,13 @@
package com.mashibing.api.controller; package com.mashibing.api.controller;
import com.mashibing.api.enums.SmsCodeEnum; import com.mashibing.api.filter.CheckFilterContext;
import com.mashibing.api.from.SingleSendForm; import com.mashibing.api.from.SingleSendForm;
import com.mashibing.api.util.R; import com.mashibing.api.util.R;
import com.mashibing.api.vo.ResultVO; import com.mashibing.api.vo.ResultVO;
import com.mashibing.common.model.StandardSubmit; import com.mashibing.common.model.StandardSubmit;
import com.mashibing.enums.ExceptionEnums;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -43,6 +45,9 @@ public class SmsController {
*/ */
private final String X_FORWARDED_FOR = "x-forwarded-for"; private final String X_FORWARDED_FOR = "x-forwarded-for";
@Autowired
private CheckFilterContext checkFilterContext;
/** /**
* @param singleSendForm * @param singleSendForm
* @return * @return
@ -54,7 +59,7 @@ public class SmsController {
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
String message = bindingResult.getFieldError().getDefaultMessage(); String message = bindingResult.getFieldError().getDefaultMessage();
log.info("【接口模块-单条短信Contorller】 参数不合法msg = {}", message); log.info("【接口模块-单条短信Contorller】 参数不合法msg = {}", message);
return R.error(SmsCodeEnum.PARAMETER_ERROR.getCode(), message); return R.error(ExceptionEnums.PARAMETER_ERROR.getCode(), message);
} }
//===============获取真实的ip================== //===============获取真实的ip==================
String ip = this.getRealIP(request); String ip = this.getRealIP(request);
@ -69,6 +74,9 @@ public class SmsController {
standardSubmit.setState(singleSendForm.getState()); standardSubmit.setState(singleSendForm.getState());
standardSubmit.setUid(singleSendForm.getUid()); standardSubmit.setUid(singleSendForm.getUid());
//===============调用策略模式的校验链==================
checkFilterContext.check(standardSubmit);
//===============发送到MQ交给策略模块处理================== //===============发送到MQ交给策略模块处理==================
return R.ok(); return R.ok();

@ -1,6 +1,7 @@
package com.mashibing.api.controller; package com.mashibing.api.controller;
import com.mashibing.api.filter.CheckFilterContext; import com.mashibing.api.filter.CheckFilterContext;
import com.mashibing.common.model.StandardSubmit;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -16,6 +17,7 @@ public class TestController {
@GetMapping("/api/test") @GetMapping("/api/test")
public void test(){ public void test(){
System.out.println("=============================="); System.out.println("==============================");
checkFilterContext.check(new Object()); StandardSubmit standardSubmit = new StandardSubmit();
checkFilterContext.check(standardSubmit);
} }
} }

@ -1,20 +0,0 @@
package com.mashibing.api.enums;
import lombok.Getter;
/**
* @author kezhen
* @date 2022/12/24
* @description codemessage
*/
@Getter
public enum SmsCodeEnum {
PARAMETER_ERROR(-10,"参数不合法");
private Integer code;
private String meg;
SmsCodeEnum(Integer code, String meg) {
this.code = code;
this.meg = meg;
}
}

@ -1,5 +1,7 @@
package com.mashibing.api.filter; package com.mashibing.api.filter;
import com.mashibing.common.model.StandardSubmit;
/** /**
* @author kezhen * @author kezhen
* @description * @description
@ -8,7 +10,7 @@ public interface CheckFilter {
/** /**
* *
* @param obj * @param standardSubmit
*/ */
void check(Object obj); void check(StandardSubmit submit);
} }

@ -1,5 +1,6 @@
package com.mashibing.api.filter; package com.mashibing.api.filter;
import com.mashibing.common.model.StandardSubmit;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.config.annotation.RefreshScope;
@ -24,11 +25,11 @@ public class CheckFilterContext {
/** /**
* check * check
*/ */
public void check(Object obj){ public void check(StandardSubmit standardSubmit){
String[] filterArray = filters.split(","); String[] filterArray = filters.split(",");
for (String filter : filterArray) { for (String filter : filterArray) {
CheckFilter checkFilter = checkFilterMap.get(filter); CheckFilter checkFilter = checkFilterMap.get(filter);
checkFilter.check(obj); checkFilter.check(standardSubmit);
} }
} }
} }

@ -1,9 +1,17 @@
package com.mashibing.api.filter.impl; package com.mashibing.api.filter.impl;
import com.mashibing.api.client.BeaconCacheClient;
import com.mashibing.api.filter.CheckFilter; import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.model.StandardSubmit;
import com.mashibing.constant.CacheConstant;
import com.mashibing.enums.ExceptionEnums;
import com.mashibing.exceptiion.ApiException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Map;
/** /**
* @author kezhen * @author kezhen
* @description * @description
@ -11,8 +19,19 @@ import org.springframework.stereotype.Service;
@Service(value = "apikey") @Service(value = "apikey")
@Slf4j @Slf4j
public class ApiKeyCheckFilter implements CheckFilter { public class ApiKeyCheckFilter implements CheckFilter {
@Autowired
private BeaconCacheClient beaconCacheClient;
@Override @Override
public void check(Object obj) { public void check(StandardSubmit submit) {
log.info("【接口模块-校验apikey】 校验ing…………"); log.info("【接口模块-校验apikey】 校验ing…………");
Map clientBusiness = beaconCacheClient.hGetAll(CacheConstant.CLIENT_BUSINESS + submit.getApikey());
if (clientBusiness == null || clientBusiness.size() == 0) {
log.info("【接口模块-校验apikey】 非法的apikey = {}", submit.getApikey());
throw new ApiException(ExceptionEnums.ERROR_APIKEY);
}
submit.setCliendId((Long) clientBusiness.get("id"));
log.info("【接口模块-校验apikey】 查询到客户信息 clientBusiness= {}", clientBusiness);
} }
} }

@ -1,6 +1,7 @@
package com.mashibing.api.filter.impl; package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter; import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -12,7 +13,7 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
public class FeeCheckFilter implements CheckFilter { public class FeeCheckFilter implements CheckFilter {
@Override @Override
public void check(Object obj) { public void check(StandardSubmit submit) {
log.info("【接口模块-校验客户余额】 校验ing…………"); log.info("【接口模块-校验客户余额】 校验ing…………");
} }
} }

@ -1,6 +1,7 @@
package com.mashibing.api.filter.impl; package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter; import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -12,7 +13,7 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
public class IPCheckFilter implements CheckFilter { public class IPCheckFilter implements CheckFilter {
@Override @Override
public void check(Object obj) { public void check(StandardSubmit submit) {
log.info("【接口模块-校验IP】 校验ing…………"); log.info("【接口模块-校验IP】 校验ing…………");
} }
} }

@ -1,6 +1,7 @@
package com.mashibing.api.filter.impl; package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter; import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -12,7 +13,7 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
public class MobileCheckFilter implements CheckFilter { public class MobileCheckFilter implements CheckFilter {
@Override @Override
public void check(Object obj) { public void check(StandardSubmit submit) {
log.info("【接口模块-校验手机号】 校验ing…………"); log.info("【接口模块-校验手机号】 校验ing…………");
} }
} }

@ -1,6 +1,7 @@
package com.mashibing.api.filter.impl; package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter; import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -12,7 +13,7 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
public class SignCheckFilter implements CheckFilter { public class SignCheckFilter implements CheckFilter {
@Override @Override
public void check(Object obj) { public void check(StandardSubmit submit) {
log.info("【接口模块-校验签名】 校验ing…………"); log.info("【接口模块-校验签名】 校验ing…………");
} }
} }

@ -1,6 +1,7 @@
package com.mashibing.api.filter.impl; package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter; import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -12,7 +13,7 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
public class TemplateCheckFilter implements CheckFilter { public class TemplateCheckFilter implements CheckFilter {
@Override @Override
public void check(Object obj) { public void check(StandardSubmit submit) {
log.info("【接口模块-校验模版】 校验ing…………"); log.info("【接口模块-校验模版】 校验ing…………");
} }
} }

@ -1,6 +1,7 @@
package com.mashibing.api.util; package com.mashibing.api.util;
import com.mashibing.api.vo.ResultVO; import com.mashibing.api.vo.ResultVO;
import com.mashibing.exceptiion.ApiException;
/** /**
* @author kezhen * @author kezhen
@ -9,16 +10,23 @@ import com.mashibing.api.vo.ResultVO;
*/ */
public class R { public class R {
public static ResultVO ok() { public static ResultVO ok() {
ResultVO r = new ResultVO(); ResultVO resultVO = new ResultVO();
r.setCode(0); resultVO.setCode(0);
r.setMsg("接收成功"); resultVO.setMsg("接收成功");
return r; return resultVO;
} }
public static ResultVO error(Integer code, String message) { public static ResultVO error(Integer code, String msg) {
ResultVO r = new ResultVO(); ResultVO resultVO = new ResultVO();
r.setCode(code); resultVO.setCode(code);
r.setMsg(message); resultVO.setMsg(msg);
return r; return resultVO;
}
public static ResultVO error(ApiException exception) {
ResultVO resultVO = new ResultVO();
resultVO.setCode(exception.getCode());
resultVO.setMsg(exception.getMessage());
return resultVO;
} }
} }

@ -1,5 +1,6 @@
package com.mashibing.api.filter; package com.mashibing.api.filter;
import com.mashibing.common.model.StandardSubmit;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -20,6 +21,6 @@ public class CheckFilterContextTest {
@org.junit.Test @org.junit.Test
public void check() { public void check() {
Object obj = new Object(); Object obj = new Object();
checkFilterContext.check(obj); checkFilterContext.check(new StandardSubmit());
} }
} }
Loading…
Cancel
Save