Skip to content

Commit f8891a3

Browse files
committed
add spring boot interceptor to copy header(dubbo-tag)/cookie(dubbo.tag)
1 parent e6e68f6 commit f8891a3

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,4 +639,6 @@ public interface CommonConstants {
639639

640640
String DUBBO_PACKABLE_METHOD_FACTORY = "dubbo.application.parameters." + PACKABLE_METHOD_FACTORY_KEY;
641641

642+
String DUBBO_TAG_HEADER = "dubbo-tag";
643+
642644
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Dubbo Spring Boot interceptor
2+
3+
`dubbo-spring-boot-interceptor` copy the header(dubbo-tag)/cookie(dubbo.tag) to dubbo .
4+
5+
6+
7+
8+
## Integrate with spring boot
9+
10+
### copy the header(dubbo-tag) to dubbo
11+
```
12+
@Configuration
13+
public class WebMvcConfig implements WebMvcConfigurer {
14+
@Override
15+
public void addInterceptors(InterceptorRegistry registry) {
16+
registry.addInterceptor(new DubboTagHeaderInterceptor()).addPathPatterns("/*").excludePathPatterns("/admin");
17+
}
18+
}
19+
```
20+
### copy the cookie(dubbo.tag) to dubbo
21+
```
22+
@Configuration
23+
public class WebMvcConfig implements WebMvcConfigurer {
24+
@Override
25+
public void addInterceptors(InterceptorRegistry registry) {
26+
registry.addInterceptor(new DubboTagCookieInterceptor()).addPathPatterns("/*").excludePathPatterns("/admin");
27+
}
28+
}
29+
```
30+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<parent>
21+
<groupId>org.apache.dubbo</groupId>
22+
<artifactId>dubbo-spring-boot</artifactId>
23+
<version>${revision}</version>
24+
<relativePath>../pom.xml</relativePath>
25+
</parent>
26+
<packaging>jar</packaging>
27+
<modelVersion>4.0.0</modelVersion>
28+
<artifactId>dubbo-spring-boot-interceptor</artifactId>
29+
<description>Apache Dubbo Spring Boot Interceptor</description>
30+
31+
<dependencies>
32+
<!-- Dubbo -->
33+
<dependency>
34+
<groupId>org.apache.dubbo</groupId>
35+
<artifactId>dubbo</artifactId>
36+
<version>${project.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.apache.dubbo</groupId>
40+
<artifactId>dubbo-common</artifactId>
41+
<version>${project.version}</version>
42+
<optional>true</optional>
43+
</dependency>
44+
<!-- spring -->
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-web</artifactId>
48+
<optional>true</optional>
49+
</dependency>
50+
</dependencies>
51+
52+
53+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.boot.interceptor;
18+
19+
import org.apache.dubbo.common.constants.CommonConstants;
20+
import org.apache.dubbo.rpc.RpcContext;
21+
import org.springframework.web.servlet.HandlerInterceptor;
22+
23+
import javax.servlet.http.Cookie;
24+
import javax.servlet.http.HttpServletRequest;
25+
import javax.servlet.http.HttpServletResponse;
26+
27+
public class DubboTagCookieInterceptor implements HandlerInterceptor {
28+
29+
@Override
30+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
31+
String tag = getSingleCookieValue(request.getCookies(), CommonConstants.TAG_KEY);
32+
RpcContext.getClientAttachment().setAttachment(CommonConstants.TAG_KEY,tag);
33+
return true;
34+
}
35+
36+
private static String getSingleCookieValue(Cookie[] cookies, String name){
37+
if (cookies == null || cookies.length == 0) {
38+
return null;
39+
}
40+
for (Cookie cookie: cookies) {
41+
if (name.equals(cookie.getName())) {
42+
return cookie.getValue();
43+
}
44+
}
45+
return null;
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.boot.interceptor;
18+
19+
import org.apache.dubbo.common.constants.CommonConstants;
20+
import org.apache.dubbo.rpc.RpcContext;
21+
import org.springframework.web.servlet.HandlerInterceptor;
22+
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
26+
public class DubboTagHeaderInterceptor implements HandlerInterceptor {
27+
28+
@Override
29+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
30+
RpcContext.getClientAttachment().setAttachment(CommonConstants.TAG_KEY, request.getHeader(CommonConstants.DUBBO_TAG_HEADER));
31+
return true;
32+
}
33+
}

dubbo-spring-boot/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<module>dubbo-spring-boot-compatible</module>
3838
<module>dubbo-spring-boot-starter</module>
3939
<module>dubbo-spring-boot-starters</module>
40+
<module>dubbo-spring-boot-interceptor</module>
4041
</modules>
4142

4243
<properties>

0 commit comments

Comments
 (0)