惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Affairs
S
Schneier on Security
T
Tenable Blog
G
GRAHAM CLULEY
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
A
Arctic Wolf
I
Intezer
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
T
Tailwind CSS Blog
K
Kaspersky official blog
Blog — PlanetScale
Blog — PlanetScale
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Threat Research - Cisco Blogs
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Palo Alto Networks Blog
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
The Cloudflare Blog
Help Net Security
Help Net Security
罗磊的独立博客
博客园 - 聂微东
Jina AI
Jina AI
Project Zero
Project Zero
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 最新话题
V
V2EX
人人都是产品经理
人人都是产品经理
美团技术团队
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
J
Java Code Geeks
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Security Latest
Security Latest
The Last Watchdog
The Last Watchdog
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
S
Securelist
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
P
Privacy International News Feed
宝玉的分享
宝玉的分享
C
CERT Recently Published Vulnerability Notes

博客园 - James.H.Fu

PMS-授权中心 如何从现有版本1.4.8升级到element UI2.0.11 Maven私有仓库: 发布release版本报错:Return code is: 400, ReasonPhrase: Repository does not allow upd ating assets: maven-releases. spring boot + dubbo开发遇到过的异常 SpringBoot favicon.ico SpringBoot 异常处理 spring boot 跨域请求 在centos 7上安装BIMServer maven 单独构建多模块项目中的单个模块 MongoDB 安装及副本集简单操作 spring-boot dubbo项目使用docker方式部署 Spring Boot和Dubbo整合 springboot 定制错误页面 Maven私有仓库-使用docker部署Nexus - James.H.Fu - 博客园 centos7 sentry部署指南 静态文件服务器部署指南 开始使用ansible 2016项目开发经验总结及后续计划 - James.H.Fu - 博客园 2016工作总结
java,javascript中的url编码
James.H.Fu · 2017-09-28 · via 博客园 - James.H.Fu

真实场景

url示例如下

http://localhost:31956/Login/Auto?Token=e8a67a9f-c062-4964-b703-d79f29c8b64e&ReturnUrl=/mobilesite/GoodsReceipt/JumpSourceIBuild?projectSysNo=19&urlReferer=http://www.baidu.com

/Login/Auto接收两个查询参数(query string)Token和ReturnUrl, 其中ReturnUrl 的值比较特殊 /mobilesite/GoodsReceipt/JumpSourceIBuild?projectSysNo=19&urlReferer=http://www.baidu.com ,它内部还有查询参数,其中含有两个特殊字符(?和&)。如果不进行编码处理/Login/Auto会认为自己有三个查询参数,分别是Token,ReturnUrl和UrlReferer。这显然不是我们想要的结果。我们应该对ReturnUrl进行编码处理。

java中使用java.net.URLEncoder.encode进行编码

 @Test
public void testEncode() throws EncoderException, UnsupportedEncodingException {
    String rawUrl = "http://www.baidu.com?param=~!@#$&*()=:/,;?+'";
    String encodeByURLEncoder = URLEncoder.encode(rawUrl, "utf-8");

    System.out.println("java.net.URLEncoder encode(UTF-8):");
    System.out.println(encodeByURLEncoder);
}

java中使用commons-codec进行编码

  1. 在pom.xml中增加commons-codec依赖
<!--apache commons-->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.5</version>
</dependency>
  1. 实例化URLCodec进行编码
 @Test
public void testEncodeReturnUrl() throws EncoderException {
    String loginUrl = "http://localhost:31956/Login/Auto?Token=%s&ReturnUrl=%s";
    String token = "e8a67a9f-c062-4964-b703-d79f29c8b64e";
    String returnUrl = "/mobilesite/GoodsReceipt/JumpSourceIBuild?projectSysNo=19&urlReferer=http://www.baidu.com";

    URLCodec codec = new URLCodec();
    String tokenEncode = codec.encode(token);
    String returnUrlEncode = codec.encode(returnUrl);

    String loginUrlEncode = String.format(loginUrl, tokenEncode, returnUrlEncode);
    System.out.println("编码结果:");
    System.out.println(loginUrlEncode);
}

编码结果http://localhost:31956/Login/Auto?Token=e8a67a9f-c062-4964-b703-d79f29c8b64e&ReturnUrl=%2Fmobilesite%2FGoodsReceipt%2FJumpSourceIBuild%3FprojectSysNo%3D19%26urlReferer%3Dhttp%3A%2F%2Fwww.baidu.com

javascript中使用encodeURIComponent进行编码

var loginUrl = "http://localhost:31956/Login/Auto?Token=#{1}#&ReturnUrl=#{2}#";
var token = "e8a67a9f-c062-4964-b703-d79f29c8b64e";
var returnUrl = "/mobilesite/GoodsReceipt/JumpSourceIBuild?projectSysNo=19&urlReferer=http://www.baidu.com";

var tokenEncode = encodeURIComponent(token);
var returnUrlEncode = encodeURIComponent(returnUrl);

var loginUrlEncode = loginUrl.replace("#{1}#", tokenEncode).replace("#{2}#", returnUrlEncode);
console.log("编码结果:", loginUrlEncode);

javascript中escape, encodeURI, encodeURIComponent的区别

  1. 对字符串(string)进行编码,其中 ASCII字母、数字、@*/+ ,这几个字符不会被编码,其余的都会。
  2. encodeURI方法不会对下列字符编码 ASCII字母、数字、~!@#$&*()=:/,;?+'
  3. encodeURIComponent方法不会对下列字符编码 ASCII字母、数字、~!*()'

参考资料