Skip to content

Commit af54de9

Browse files
committed
添加接口防刷功能
1 parent dc1feab commit af54de9

38 files changed

+4120
-2
lines changed

pom.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<gson.version>2.6</gson.version>
2424
<hutool.version>5.3.7</hutool.version>
2525
<kk-anti-reptile.version>1.0.0-RELEASE</kk-anti-reptile.version>
26+
<redisson.version>3.11.0</redisson.version>
27+
<ua-util.version>1.21</ua-util.version>
28+
<commons-fileupload.version>1.4</commons-fileupload.version>
2629
</properties>
2730

2831
<!-- Inherit defaults from Spring Boot -->
@@ -140,10 +143,27 @@
140143
<artifactId>commons-pool2</artifactId>
141144
</dependency>
142145

143-
<dependency>
146+
<!-- <dependency>
144147
<groupId>cn.keking.project</groupId>
145148
<artifactId>kk-anti-reptile</artifactId>
146149
<version>${kk-anti-reptile.version}</version>
150+
</dependency>-->
151+
<dependency>
152+
<groupId>org.redisson</groupId>
153+
<artifactId>redisson</artifactId>
154+
<version>${redisson.version}</version>
155+
</dependency>
156+
<!-- User-Agent库 -->
157+
<dependency>
158+
<groupId>eu.bitwalker</groupId>
159+
<artifactId>UserAgentUtils</artifactId>
160+
<version>${ua-util.version}</version>
161+
</dependency>
162+
<!-- common-fileupload用于servlet处理form表单验证请求 -->
163+
<dependency>
164+
<groupId>commons-fileupload</groupId>
165+
<artifactId>commons-fileupload</artifactId>
166+
<version>${commons-fileupload.version}</version>
147167
</dependency>
148168
</dependencies>
149169

src/main/java/com/company/project/Application.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.boot.SpringApplication;
77
import org.springframework.boot.autoconfigure.SpringBootApplication;
88
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.context.annotation.FilterType;
910
import org.springframework.context.annotation.Import;
1011

1112
/**
@@ -21,6 +22,7 @@
2122
@SpringBootApplication
2223
@ComponentScan(basePackages={"cn.hutool.extra.spring","com.company.project"})
2324
@Import(cn.hutool.extra.spring.SpringUtil.class)
25+
//@ComponentScan(basePackages = {"cn.hutool.extra.spring","com.company.project"}, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {WebMvcConfig.class}))
2426
public class Application {
2527
private static Logger log = LoggerFactory.getLogger(Application.class);
2628
public static void main(String[] args) {

src/main/java/com/company/project/demoUser/web/DemoUserController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.company.project.core.ResultGenerator;
44
import com.company.project.demoUser.domain.DemoUser;
55
import com.company.project.demoUser.service.DemoUserService;
6+
import com.company.project.kkantireptile.cn.keking.anti_reptile.annotation.AntiReptile;
67
import com.github.pagehelper.PageHelper;
78
import com.github.pagehelper.PageInfo;
89
import org.springframework.web.bind.annotation.PostMapping;
@@ -83,6 +84,7 @@ public Result getById(@PathVariable("id") Long id){
8384

8485
@ApiOperation(value = "分页查询",notes = "分页查询",responseContainer = "List",response = PageInfo.class)
8586
@GetMapping
87+
@AntiReptile
8688
public Result queryist(DemoUser demoUser, Page page) {
8789
PageHelper.startPage(page.getPageNum(), page.getPageSize());
8890
List<DemoUser> demoUserList = demoUserService.find(demoUser);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.company.project.kkantireptile.cn.keking.anti_reptile;
2+
3+
import com.company.project.kkantireptile.cn.keking.anti_reptile.module.VerifyImageDTO;
4+
import com.company.project.kkantireptile.cn.keking.anti_reptile.module.VerifyImageVO;
5+
import com.company.project.kkantireptile.cn.keking.anti_reptile.rule.RuleActuator;
6+
import com.company.project.kkantireptile.cn.keking.anti_reptile.util.VerifyImageUtil;
7+
8+
import org.apache.commons.fileupload.FileItem;
9+
import org.apache.commons.fileupload.FileUploadException;
10+
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
11+
import org.apache.commons.fileupload.servlet.ServletFileUpload;
12+
import org.springframework.beans.BeanUtils;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
15+
import javax.servlet.http.HttpServletRequest;
16+
import java.io.UnsupportedEncodingException;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
/**
22+
* @author kl @kailing.pub
23+
* @since 2019/7/9
24+
*/
25+
26+
public class ValidateFormService {
27+
28+
@Autowired
29+
private RuleActuator actuator;
30+
31+
@Autowired
32+
private VerifyImageUtil verifyImageUtil;
33+
34+
public String validate(HttpServletRequest request) throws UnsupportedEncodingException {
35+
DiskFileItemFactory factory = new DiskFileItemFactory();
36+
ServletFileUpload upload = new ServletFileUpload(factory);
37+
upload.setHeaderEncoding("UTF-8");
38+
List items = null;
39+
try {
40+
items = upload.parseRequest(request);
41+
} catch (FileUploadException e) {
42+
e.printStackTrace();
43+
}
44+
Map<String, String> params = new HashMap<String, String>();
45+
for(Object object : items){
46+
FileItem fileItem = (FileItem) object;
47+
if (fileItem.isFormField()) {
48+
params.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
49+
}
50+
}
51+
String verifyId = params.get("verifyId");
52+
String result = params.get("result");
53+
String realRequestUri = params.get("realRequestUri");
54+
String actualResult = verifyImageUtil.getVerifyCodeFromRedis(verifyId);
55+
if (actualResult != null && request != null && actualResult.equals(result.toLowerCase())) {
56+
actuator.reset(request, realRequestUri);
57+
return "{\"result\":true}";
58+
}
59+
return "{\"result\":false}";
60+
}
61+
62+
public String refresh(HttpServletRequest request) {
63+
String verifyId = request.getParameter("verifyId");
64+
verifyImageUtil.deleteVerifyCodeFromRedis(verifyId);
65+
VerifyImageDTO verifyImage = verifyImageUtil.generateVerifyImg();
66+
verifyImageUtil.saveVerifyCodeToRedis(verifyImage);
67+
VerifyImageVO verifyImageVO = new VerifyImageVO();
68+
BeanUtils.copyProperties(verifyImage, verifyImageVO);
69+
String result = "{\"verifyId\": \"" + verifyImageVO.getVerifyId() + "\",\"verifyImgStr\": \"" + verifyImageVO.getVerifyImgStr() + "\"}";
70+
return result;
71+
}
72+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.company.project.kkantireptile.cn.keking.anti_reptile.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* @author chenjh 接口反爬虫注解
10+
* @since 2020/2/4 15:44
11+
*/
12+
@Target(value = {ElementType.METHOD})
13+
@Retention(value = RetentionPolicy.RUNTIME)
14+
public @interface AntiReptile {
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.company.project.kkantireptile.cn.keking.anti_reptile.config;
2+
3+
import com.company.project.kkantireptile.cn.keking.anti_reptile.ValidateFormService;
4+
import com.company.project.kkantireptile.cn.keking.anti_reptile.config.AntiReptileProperties;
5+
import com.company.project.kkantireptile.cn.keking.anti_reptile.config.RedissonAutoConfig;
6+
import com.company.project.kkantireptile.cn.keking.anti_reptile.config.WebMvcConfig;
7+
import com.company.project.kkantireptile.cn.keking.anti_reptile.constant.AntiReptileConsts;
8+
import com.company.project.kkantireptile.cn.keking.anti_reptile.interceptor.AntiReptileInterceptor;
9+
import com.company.project.kkantireptile.cn.keking.anti_reptile.rule.AntiReptileRule;
10+
import com.company.project.kkantireptile.cn.keking.anti_reptile.rule.IpRule;
11+
import com.company.project.kkantireptile.cn.keking.anti_reptile.rule.RuleActuator;
12+
import com.company.project.kkantireptile.cn.keking.anti_reptile.rule.UaRule;
13+
import com.company.project.kkantireptile.cn.keking.anti_reptile.servlet.RefreshFormServlet;
14+
import com.company.project.kkantireptile.cn.keking.anti_reptile.servlet.ValidateFormServlet;
15+
import com.company.project.kkantireptile.cn.keking.anti_reptile.util.VerifyImageUtil;
16+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
17+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
18+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Import;
22+
23+
import java.util.Comparator;
24+
import java.util.List;
25+
import java.util.stream.Collectors;
26+
27+
/**
28+
* RedissonAutoConfiguration 的 AutoConfigureOrder 为默认值(0),此处在它后面加载
29+
* @author kl @kailing.pub
30+
* @since 2019/7/8
31+
*/
32+
@Configuration
33+
@EnableConfigurationProperties(AntiReptileProperties.class)
34+
@ConditionalOnProperty(prefix = "anti.reptile.manager", value = "enabled", havingValue = "true")
35+
@Import({RedissonAutoConfig.class, WebMvcConfig.class})
36+
public class AntiReptileAutoConfig {
37+
38+
@Bean
39+
public ServletRegistrationBean validateFormServlet() {
40+
return new ServletRegistrationBean(new ValidateFormServlet(), AntiReptileConsts.VALIDATE_REQUEST_URI);
41+
}
42+
43+
@Bean
44+
public ServletRegistrationBean refreshFormServlet() {
45+
return new ServletRegistrationBean(new RefreshFormServlet(), AntiReptileConsts.REFRESH_REQUEST_URI);
46+
}
47+
48+
@Bean
49+
@ConditionalOnProperty(prefix = "anti.reptile.manager.ip-rule",value = "enabled", havingValue = "true", matchIfMissing = true)
50+
public IpRule ipRule(){
51+
return new IpRule();
52+
}
53+
54+
@Bean
55+
@ConditionalOnProperty(prefix = "anti.reptile.manager.ua-rule",value = "enabled", havingValue = "true", matchIfMissing = true)
56+
public UaRule uaRule() {
57+
return new UaRule();
58+
}
59+
60+
@Bean
61+
public VerifyImageUtil verifyImageUtil() {
62+
return new VerifyImageUtil();
63+
}
64+
65+
@Bean
66+
public RuleActuator ruleActuator(final List<AntiReptileRule> rules){
67+
final List<AntiReptileRule> antiReptileRules = rules.stream()
68+
.sorted(Comparator.comparingInt(AntiReptileRule::getOrder)).collect(Collectors.toList());
69+
return new RuleActuator(antiReptileRules);
70+
}
71+
72+
@Bean
73+
public ValidateFormService validateFormService(){
74+
return new ValidateFormService();
75+
}
76+
77+
@Bean
78+
public AntiReptileInterceptor antiReptileInterceptor() {
79+
return new AntiReptileInterceptor();
80+
}
81+
82+
}

0 commit comments

Comments
 (0)