如何在Spring Boot中应用基于IP的速率限制

huangapple 未分类评论57阅读模式
英文:

How to apply IP based rate-limiting in spring boot

问题

这是我的应用程序配置文件 application.properties:

spring.cache.jcache.config=classpath:ehcache.xml
logging.level.org.ehcache=info
bucket4j.enabled=true
bucket4j.filters[0].cache-name=buckets
bucket4j.filters[0].filter-method=servlet
bucket4j.filters[0].url=.*
bucket4j.filters[0].rate-limits[0].bandwidths[0].capacity=5
bucket4j.filters[0].rate-limits[0].bandwidths[0].time=10
bucket4j.filters[0].rate-limits[0].bandwidths[0].unit=seconds
bucket4j.filters[0].rate-limits[0].expression="getRemoteAddress()"
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval=0
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval-unit=seconds

这是我的 ehcache.xml 文件:

<config xmlns='http://www.ehcache.org/v3'
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
    <cache alias="buckets">
        <expiry><ttl unit="seconds">6</ttl></expiry>
        <heap unit="entries">2000</heap>
        <jsr107:mbeans enable-statistics="true"/>
    </cache>
</config>

这是我的 pom.xml 文件的相关部分:

<dependency>
    <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
    <artifactId>bucket4j-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>

<dependency>
    <groupId>javax.cache</groupId>
    <artifactId>cache-api</artifactId>
    <scope>runtime</scope>
</dependency>

这是我的服务器初始化类 Server Initializer:

@EnableCaching
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(EwseparationApplication.class);
    }

}
英文:

I want to apply rate-limit on the basis of IP for each (/get)service for my project for e.g each IP can access each service for a particular no. of time i.e 5 times /10 second. I have used bucket4j rate-limiting but it's not working. Does anyone have any idea about it??

this is my application.properties

spring.cache.jcache.config=classpath:ehcache.xml
logging.level.org.ehcache=info
bucket4j.enabled=true
bucket4j.filters[0].cache-name=buckets
bucket4j.filters[0].filter-method=servlet
bucket4j.filters[0].url=.* 
bucket4j.filters[0].rate-limits[0].bandwidths[0].capacity=5
bucket4j.filters[0].rate-limits[0].bandwidths[0].time=10
bucket4j.filters[0].rate-limits[0].bandwidths[0].unit=seconds
bucket4j.filters[0].rate-limits[0].expression=&quot;getRemoteAddress()&quot;
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval=0
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval-unit=seconds

this is encache.xml

&lt;config xmlns=&#39;http://www.ehcache.org/v3&#39;
		xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
		xmlns:jsr107=&quot;http://www.ehcache.org/v3/jsr107&quot;
		xsi:schemaLocation=&quot;http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
		http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd&quot;&gt;
	&lt;cache alias=&quot;buckets&quot;&gt;
        &lt;expiry&gt;&lt;ttl unit=&quot;seconds&quot;&gt;6&lt;/ttl&gt;&lt;/expiry&gt;
        &lt;heap unit=&quot;entries&quot;&gt;2000&lt;/heap&gt;
        &lt;jsr107:mbeans enable-statistics=&quot;true&quot;/&gt;
    &lt;/cache&gt;
&lt;/config&gt;

pom.xml

&lt;dependency&gt;
	&lt;groupId&gt;com.giffing.bucket4j.spring.boot.starter&lt;/groupId&gt;
	&lt;artifactId&gt;bucket4j-spring-boot-starter&lt;/artifactId&gt;
	&lt;version&gt;0.2.0&lt;/version&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-cache&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupId&gt;org.ehcache&lt;/groupId&gt;
	&lt;artifactId&gt;ehcache&lt;/artifactId&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
	&lt;groupId&gt;javax.cache&lt;/groupId&gt;
	&lt;artifactId&gt;cache-api&lt;/artifactId&gt;
	&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;

server Initializer

@EnableCaching
public class ServletInitializer extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(EwseparationApplication.class);
	}

}

答案1

得分: 0

我遇到了同样的问题。看起来关于这个问题在 bucket4j spring boot starter 的 GitHub 项目上开了一个 issue(链接)。

我按照问题中提到的个人的做法,将我的 application.properties 文件转换成了 application.yml,然后我的速率限制开始起作用了。我不太清楚实际问题是什么(为什么 application.yml 起作用而 application.properties 不起作用)。我有点倾向于使用 properties 格式而不是 yml。

英文:

I was having the same issue. It looks like there was an issue opened with the bucket4j spring boot starter github project about this (link).

I converted my application.properties to application.yml as the individual reported in the issue and my rate limit started working. I'm not sure what the actual issue is (why application.yml works and application.properties does not). I kindof prefer the properties format over yml.

答案2

得分: 0

你可以使用 .yaml,因为 .properties 在识别标签方面仍然存在一些问题。

英文:

you can use .yaml as .properties is still having some issue in recognizing the tags

huangapple
  • 本文由 发表于 2020年7月23日 14:24:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/63048121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定