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 閲嶅鎻愪氦的主要内容,如果未能解决你的问题,请参考以下文章

django-鎻愪氦璁㈠崟

[Git] 鎾ら攢鎻愪氦commit

window绯荤粺-椤圭洰鎻愪氦鑷砱ithub

鍓嶇涔嬭矾锛氱敤 Angular 瑙勮寖鏉ョ害鏉熷洟闃?git 鎻愪氦

鍦―jango妗嗘灦涓娇鐢ˋjax杩涜POST鏁版嵁鎻愪氦鏃跺繀椤绘惡甯srf_token

Spring闈㈣瘯鑰冪偣