假设下面是你的视频网站链接列表,如果别人想爬取你的数据十分轻松,看规则就知道数据库是序列自增的
http://www.xxxx.com/video/1
http://www.xxxx.com/video/2
http://www.xxxx.com/video/3
那么解决这一问题,我们可以使用短地址,不对外暴露真实链接,使用对称加密是一个很好的方案。
Hashids是一个很好的选择,它提供了JS/PHP/JAVA/PYTHON等编程语言的实现,这里我使用的就是它。
下面是我基于blade框架搭建的java短地址服务。
1
2
3
4
5
|
CREATE TABLE `t_url` ( `id` int (10) NOT NULL AUTO_INCREMENT, `url` text NOT NULL , PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8; |
路由
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
@Path public class IndexRoute { // 盐值 private static final Hashids HASHIDS = new Hashids( "blade-shorturl" ); private UrlModel urlModel = new UrlModel(); @Route ( "/:key" ) public void get(Request req, Response response) { String key = req.pathParam( ":key" ).replaceAll( "[^A-Za-z0-9]" , "" ); long [] numbers = HASHIDS.decode(key); if ( null == numbers || numbers.length < 1 ) { response.text( "没有找到" ); return ; } int id = ( int ) numbers[ 0 ]; String result = get(id).getUrl(); if (result == null ) { response.text( "没有找到" ); return ; } response.redirect(result); } @Route (value = "/" , method = HttpMethod.GET) public String index() { return "index" ; } @Route (value = "/" , method = HttpMethod.POST) public String save(Request request, Response response) { String resJsp = "index" ; String longUrl = request.query( "url" ); if (!isURL(longUrl)) { request.attribute( "error" , "无效的URL" ); return resJsp; } Integer id = this .save(longUrl); if (id == 0 ) { request.attribute( "error" , "保存失败" ); return resJsp; } String hash = HASHIDS.encode(id); request.attribute( "url_hash" , hash); System.out.println( "id = " + id + ",url_hash=" + hash); return resJsp; } private Integer save(String url) { return urlModel.insert().param( "url" , url).executeAndCommit(); } private UrlModel get( int id) { return urlModel.fetchByPk(id); } private final String REGEX = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]" ; private boolean isURL(String url) { if (StringKit.isNotBlank(url)){ Pattern pattern = Pattern.compile(REGEX); Matcher matcher = pattern.matcher(url); if (matcher.find()) { return true ; } } return false ; } } |
实现效果:
代码位置:https://github.com/bladejava/blade-shorturl
以上就是本文的全部内容,希望对大家的学习有所帮助。