???: ?? ???????? ??? ??? ?? ?? ??? ?? ? ?? ??? ? ????
Aug 22, 2023 pm 03:57 PM????? ??? ?? Tian ?????
?? ??? ?? ?? ???? ?? ?, ????? ???? ??? ?????? ???? ?? ?? ??? ??? ??? ??? ? ? ? ??.
??? ?? ??? ?????? ??? ??? ?? ??? ? ??? Tian ??? ?? ? ?? ??????.
? ?? ? 9?? ?? ???? ???? ????:

Imdempotence? ???? ??? ??? ??? ?? ? ???? ????????. . ??? ? ?? ??? ? ??? ???. ?:
?? ?????, ??? ?? ? ??? ? ???? ?? ?????, ??? ??? ?? ?? ??? ? ?? ??? ? ????. -
Alipay ?? ?????, ?? ??? ?? ? ??? ?? ??? ???? ??? -
???? ?? ?? ?????? ???? ?? ?? ? ?? ??? ?? ??? ?? ?? ??? ? ??? ? ?? ??? ? ????. ?? ???--??? ?? ??? ??
??? ??--??? ?? ? ??(??? ?? ?? ? ??)
- ??? ??-??? ?? ?? ???? ??? ???? ? ???? ????? ??
- ?? ?? - redis(jedis, redisson) ?? Zookeeper ??
- ?? ?? - ?? ??, ??? ???? ? ?? ??
-
- ? ????? ? ?? ?? ??, ?
????? ?? ????? ??? ??? ???? ??? ?????. 4. ?? ????
???? ???? ?? ? ??? ?? ?? ???? ????
token
, 先獲取token
, 并將此token
存入redis, 請求接口時(shí), 將此token
放到header或者作為請求參數(shù)請求接口, 后端接口判斷redis中是否存在此token
:???? ?? ???? ??? ????? ???? redis ??
,??
? ???? ??? ??? ? ?????? ???? ???
Prompttoken
, 那么, 如果是重復(fù)請求, 由于token
已被刪除, 則不能通過校驗(yàn), 返回請勿重復(fù)操作
提示如果不存在, 說明參數(shù)不合法或者是重復(fù)請求, 返回提示即可
五、項(xiàng)目簡介
Spring Boot
Redis
@ApiIdempotent
注解 + 攔截器對請求進(jìn)行攔截@ControllerAdvice
全局異常處理壓測工具: Jmeter
5. ???? ??
????? ??
????????Redis
????????@ApiIdempotent
?? + ????? ??? ????? ????????@ControllerAdvice
?? ?? ?????????????? ??? ??:Jmeter
??????????:??????? ??? ??? ??? ???. ???? ?? ??, Spring Boot Redis, ServerResponse, ResponseCode ? ?? ?? ?? ??? ?? ?? ??? ? ??? ??? ?????.??六、代碼實(shí)現(xiàn)
1、
maven
依賴maven
依賴<!-- Redis-Jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!--lombok 本文用到@Slf4j注解, 也可不引用, 自定義log即可--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency>
2、
JedisUtil
@Component @Slf4j public class JedisUtil { @Autowired private JedisPool jedisPool; private Jedis getJedis() { return jedisPool.getResource(); } /** * 設(shè)值 * * @param key * @param value * @return */ public String set(String key, String value) { Jedis jedis = null; try { jedis = getJedis(); return jedis.set(key, value); } catch (Exception e) { log.error("set key:{} value:{} error", key, value, e); return null; } finally { close(jedis); } } /** * 設(shè)值 * * @param key * @param value * @param expireTime 過期時(shí)間, 單位: s * @return */ public String set(String key, String value, int expireTime) { Jedis jedis = null; try { jedis = getJedis(); return jedis.setex(key, expireTime, value); } catch (Exception e) { log.error("set key:{} value:{} expireTime:{} error", key, value, expireTime, e); return null; } finally { close(jedis); } } /** * 取值 * * @param key * @return */ public String get(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.get(key); } catch (Exception e) { log.error("get key:{} error", key, e); return null; } finally { close(jedis); } } /** * 刪除key * * @param key * @return */ public Long del(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.del(key.getBytes()); } catch (Exception e) { log.error("del key:{} error", key, e); return null; } finally { close(jedis); } } /** * 判斷key是否存在 * * @param key * @return */ public Boolean exists(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.exists(key.getBytes()); } catch (Exception e) { log.error("exists key:{} error", key, e); return null; } finally { close(jedis); } } /** * 設(shè)值key過期時(shí)間 * * @param key * @param expireTime 過期時(shí)間, 單位: s * @return */ public Long expire(String key, int expireTime) { Jedis jedis = null; try { jedis = getJedis(); return jedis.expire(key.getBytes(), expireTime); } catch (Exception e) { log.error("expire key:{} error", key, e); return null; } finally { close(jedis); } } /** * 獲取剩余時(shí)間 * * @param key * @return */ public Long ttl(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.ttl(key); } catch (Exception e) { log.error("ttl key:{} error", key, e); return null; } finally { close(jedis); } } private void close(Jedis jedis) { if (null != jedis) { jedis.close(); } } }
3、自定義注解
@ApiIdempotent
/** * 在需要保證 接口冪等性 的Controller的方法上使用此注解 */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface ApiIdempotent { }
4、
ApiIdempotentInterceptor
攔截器/** * 接口冪等性攔截器 */ public class ApiIdempotentInterceptor implements HandlerInterceptor { @Autowired private TokenService tokenService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { if (!(handler instanceof HandlerMethod)) { return true; } HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); ApiIdempotent methodAnnotation = method.getAnnotation(ApiIdempotent.class); if (methodAnnotation != null) { check(request);// 冪等性校驗(yàn), 校驗(yàn)通過則放行, 校驗(yàn)失敗則拋出異常, 并通過統(tǒng)一異常處理返回友好提示 } return true; } private void check(HttpServletRequest request) { tokenService.checkToken(request); } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }
5、
TokenServiceImpl
@Service public class TokenServiceImpl implements TokenService { private static final String TOKEN_NAME = "token"; @Autowired private JedisUtil jedisUtil; @Override public ServerResponse createToken() { String str = RandomUtil.UUID32(); StrBuilder token = new StrBuilder(); token.append(Constant.Redis.TOKEN_PREFIX).append(str); jedisUtil.set(token.toString(), token.toString(), Constant.Redis.EXPIRE_TIME_MINUTE); return ServerResponse.success(token.toString()); } @Override public void checkToken(HttpServletRequest request) { String token = request.getHeader(TOKEN_NAME); if (StringUtils.isBlank(token)) {// header中不存在token token = request.getParameter(TOKEN_NAME); if (StringUtils.isBlank(token)) {// parameter中也不存在token throw new ServiceException(ResponseCode.ILLEGAL_ARGUMENT.getMsg()); } } if (!jedisUtil.exists(token)) { throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg()); } Long del = jedisUtil.del(token); if (del <= 0) { throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg()); } } }
6、
TestApplication
@SpringBootApplication @MapperScan("com.wangzaiplus.test.mapper") public class TestApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } /** * 跨域 * @return */ @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); final CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration); return new CorsFilter(urlBasedCorsConfigurationSource); } @Override public void addInterceptors(InterceptorRegistry registry) { // 接口冪等性攔截器 registry.addInterceptor(apiIdempotentInterceptor()); super.addInterceptors(registry); } @Bean public ApiIdempotentInterceptor apiIdempotentInterceptor() { return new ApiIdempotentInterceptor(); } }
好了,以上便是代碼的實(shí)現(xiàn)部分,下面我們就來驗(yàn)證一下。
七、測試驗(yàn)證
獲取
token
的控制器TokenController
@RestController @RequestMapping("/token") public class TokenController { @Autowired private TokenService tokenService; @GetMapping public ServerResponse token() { return tokenService.createToken(); } }
2、JedisUtil
??@RestController @RequestMapping("/test") @Slf4j public class TestController { @Autowired private TestService testService; @ApiIdempotent @PostMapping("testIdempotence") public ServerResponse testIdempotence() { return testService.testIdempotence(); } }
??3、自定義注解@ApiIdempotent
??rrreee??4、ApiIdempotentInterceptor
?攔截器??rrreee??5、TokenServiceImpl< /code>??rrreee??6、<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px; background-color: rgba(27, 31, 35, 0.05);?? ??: 'Operator Mono', Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(239, 112, 96);">TestApplication
??rrreee??好了,以上便是代碼的實(shí)現(xiàn)分,下面我們就來驗(yàn)證一下。??????????七、測試驗(yàn)證???? ??????獲取??
? ?? ??TokenController
:??@RestController @RequestMapping("/token") public class TokenController { @Autowired private TokenService tokenService; @GetMapping public ServerResponse token() { return tokenService.createToken(); } }
TestController
, 注意@ApiIdempotent
注解, 在需要冪等性校驗(yàn)的方法上聲明此注解即可, 不需要校驗(yàn)的無影響:@RestController @RequestMapping("/test") @Slf4j public class TestController { @Autowired private TestService testService; @ApiIdempotent @PostMapping("testIdempotence") public ServerResponse testIdempotence() { return testService.testIdempotence(); } }
獲取
token
:查看
Redis
:測試接口安全性: 利用
Jmeter
測試工具模擬50個(gè)并發(fā)請求, 將上一步獲取到的token作為參數(shù)header或參數(shù)均不傳token, 或者token值為空, 或者token值亂填, 均無法通過校驗(yàn), 如token值為
abcd
。8. ???? (?? ??)
? ????? ??? ?? ??? ?? ??? ?? ?? ??? ???? ?? ??? ??? ?? ??? ?? ????. ????, ?? ???? ??? 46?? ??? ???? ?? ?????. ?? ??? ???? ????? ??? ?????.
jedisUtil.del(token)
? ?? ??? ???? ?? ?? ???? ???? ??? ????. ??? ?? ?? ??? ?? ???? ??? ?? ?????. ???? ??? ???.?? ?? ??:
?? ??
??? ?? ?????
?? ? ??? ?????? ?? ??? ???? ??? ??? ??? ?? ????. ??? ??? ??? ???? ???
9. ??
?? ????? ?? ?????. ?, ? ??? ???? ?????
保證冪等性
, 通過攔截器+注解
, 就不用每次請求都寫重復(fù)代碼, 其實(shí)也可以利用Spring AOP
?????.?, ??? ??? ???????.
? ??? ???: ?? ???????? ??? ??? ?? ?? ??? ?? ? ?? ??? ? ????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

Spring? ??? ??? Aop? ?? ?? ??? ?? ???? ?????. Spring Boot ?? Spring Boot 2? Aop? ?? ??? ?? ??? ???? AOP?? ??? ??? ?? ??????.

OOM? ??? JVM ???? ???? ?? ????? ???? ??? ?????. ? ????? Java ????? OOM? ???? ? ??? ???? ??? ?? ???? ?????.

?? ??? ?? ?? ??? ?????? ????. ??? ??? ??? ??? ?? ? ????. ?? ??? ?? ???? ??? ??? ??? ???? ???? ???? ?????? ????.

?? ?? ??? ? ??? Ping An Insurance? ???? ?? ????. ??? ?? ????, ???? ??? ????? ?? ??? ???? ??? ????. ?? ??? ??? ??? ??? ? ???, ??? ????!

?? ????? Java String ???? ?? 5?? ?? ??? ???????. ?? ??? ???? ? 5?? ?? ? ? ??? ?? ??????. ? ??? ??? ??? ?? ??? ? ??? ???? ? ??? ? ????.

?? ?? ????? ???? ?? ??? C A S(?? ? ??)? ??? ???? ?? ???? ??? ???? ??? ???? ???? ?? ??? ?? ? ??? ???.

Java? ??? ??? ???? ?????. Java ???? ??? ????? ??? ?? ?? ??? ??? ???. ????? ?? ??? ? ?, ??? "???? ??" ??? ?????? Java?? ????? ???? ??? ??? ?? ??? ????? ??? ????? ??? ????.

???? ????? ClassLoader? ?? ???? ???? ???? ???.
