spring boot 閲嶅鎻愪氦
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 閲嶅鎻愪氦相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/cut' title='cut'>cut npoi www target exp sdn https cache end
package com.future.interceptor; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.baoqilai.scp.web.RestResultGenerator; import com.future.shiro.ShiroUtils; import com.google.common.cache.Cache; @Aspect @Component public class NoRepeatSubmitAop { private static final Logger logger = LoggerFactory.getLogger(NoRepeatSubmitAop.class); @Autowired private Cache<String, Integer> cache; @Around("execution(* com.future.controller.*.*.*(..))&& @annotation(nos)") public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nos) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); String sessionId = ShiroUtils.getSession().getId().toString(); HttpServletRequest request = attributes.getRequest(); String key = sessionId + "-" + request.getServletPath(); if (cache.getIfPresent(key) != null) { logger.error("閲嶅鎻愪氦"); return RestResultGenerator.genErrorResult("閲嶅璇锋眰锛岃绋嶅悗鍐嶈瘯"); } // 濡傛灉鏄涓€娆¤姹?灏卞皢 key 褰撳墠瀵硅薄鍘嬪叆缂撳瓨涓?/span> cache.put(key, 0); try { return pjp.proceed(); } catch (Throwable throwable) { logger.error("楠岃瘉閲嶅鎻愪氦鏃跺嚭鐜版湭鐭ュ紓甯?"); return RestResultGenerator.genErrorResult("楠岃瘉閲嶅鎻愪氦鏃跺嚭鐜版湭鐭ュ紓甯?"); } finally { cache.invalidate(key); } } }
package com.future.interceptor; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @鍔熻兘鎻忚堪 闃叉閲嶅鎻愪氦鏍囪娉ㄨВ * @author Administrator * */ @Target(ElementType.METHOD) // 浣滅敤鍒版柟娉曚笂 @Retention(RetentionPolicy.RUNTIME) // 杩愯鏃舵湁鏁?/span> public @interface NoRepeatSubmit { }
package com.future.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; @Configuration public class UrlCache { @Bean public Cache<String, Integer> getCache() { // return CacheBuilder.newBuilder().expireAfterWrite(2L, TimeUnit.SECONDS).build();// 缂撳瓨鏈夋晥鏈熶负2绉?/span> return CacheBuilder.newBuilder().build();// 涓嶈缃紦瀛樻湁鏁堟湡 } }
@RequestMapping(value = "/test", method = RequestMethod.POST) @NoRepeatSubmit public RestResult test() { int res=6; if(res>0){ return RestResultGenerator.genSuccessResult(); } return RestResultGenerator.genErrorResult("鏍稿崟澶辫触"); }
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>24.0-jre</version> </dependency>
鏉ヨ嚜锛歨ttps://www.jianshu.com/p/09c6b05b670a
https://blog.csdn.net/memmsc/article/details/80837996 鍒嗗竷寮忓弬鑰?/p>
以上是关于spring boot 閲嶅鎻愪氦的主要内容,如果未能解决你的问题,请参考以下文章
鍓嶇涔嬭矾锛氱敤 Angular 瑙勮寖鏉ョ害鏉熷洟闃?git 鎻愪氦