-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RESTful Dubbo Integration with Spring Security and OAuth2 Sample (#…
…1193) * feat: add authorization server * feat: create a resource server * feat: create case-configuration.yml * feat: create case-versions.conf * feat: add RestExtension spi * fix: use restExtension to intercept request(unfinished) * fix: use restExtension to intercept request * feat: update case-configuration.yml * fix: use restExtension to intercept request * fix: change the name of the test file * fix: change the systemProps in the case-configuration.yml * fix: change the systemProps in the case-configuration.yml * feat: add test cases * fix: update config in AuthorizationSeverConfig.java --------- Co-authored-by: Sean Yang <[email protected]>
- Loading branch information
1 parent
ffdb948
commit f9a3d5f
Showing
17 changed files
with
881 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...ubbo-samples-triple-rest/dubbo-samples-triple-rest-spring-security/case-configuration.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
services: | ||
authorization: | ||
type: app | ||
basedir: spring-security-authorization-server | ||
mainClass: org.apache.dubbo.rest.demo.AuthorizationApplication | ||
systemProps: | ||
- authorization.address=authorization | ||
checkPorts: | ||
- 9000 | ||
checkLog: "authorization service started" | ||
|
||
resource: | ||
type: app | ||
basedir: spring-security-resource-server | ||
mainClass: org.apache.dubbo.rest.demo.ResourceApplication | ||
systemProps: | ||
- authorization.address=authorization | ||
checkPorts: | ||
- 50051 | ||
checkLog: "resource service started" | ||
|
||
test: | ||
type: test | ||
basedir: spring-security-resource-server | ||
tests: | ||
- "**/*IT.class" | ||
systemProps: | ||
- authorization.address=authorization | ||
- resource.address=resource | ||
waitPortsBeforeRun: | ||
- authorization:9000 | ||
- resource:50051 | ||
depends_on: | ||
- authorization | ||
- resource |
25 changes: 25 additions & 0 deletions
25
...ed/dubbo-samples-triple-rest/dubbo-samples-triple-rest-spring-security/case-versions.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
|
||
# Supported component versions of the test case | ||
|
||
# Spring app | ||
dubbo.version=3.3.* | ||
spring.version=6.* | ||
java.version= [>= 17] |
61 changes: 61 additions & 0 deletions
61
2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-spring-security/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.dubbo</groupId> | ||
<artifactId>dubbo-samples-triple-rest</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
<packaging>pom</packaging> | ||
<modules> | ||
<module>spring-security-resource-server</module> | ||
<module>spring-security-authorization-server</module> | ||
</modules> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-core</artifactId> | ||
<version>6.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-oauth2-authorization-server</artifactId> | ||
<version>1.2.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-oauth2-resource-server</artifactId> | ||
<version>6.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-oauth2-jose</artifactId> | ||
<version>6.2.2</version> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<groupId>org.apache.dubbo.rest.demo</groupId> | ||
<artifactId>dubbo-samples-triple-rest-spring-security</artifactId> | ||
|
||
</project> |
98 changes: 98 additions & 0 deletions
98
...st/dubbo-samples-triple-rest-spring-security/spring-security-authorization-server/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.apache.dubbo.rest.demo</groupId> | ||
<artifactId>dubbo-samples-triple-rest-spring-security</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>org.apache.dubbo.rest.demo</groupId> | ||
<artifactId>spring-security-authorization-server</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<java.version>17</java.version> | ||
|
||
</properties> | ||
|
||
<dependencies> | ||
<!-- Spring Boot Starter Web --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- Spring Boot Starter Security --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-security</artifactId> | ||
</dependency> | ||
|
||
<!-- Spring Security OAuth2 Authorization Server --> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-oauth2-authorization-server</artifactId> | ||
</dependency> | ||
|
||
<!-- Spring Security OAuth2 JOSE (for JWT support) --> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-oauth2-jose</artifactId> | ||
<exclusions> | ||
<exclusion> | ||
<artifactId>nimbus-jose-jwt</artifactId> | ||
<groupId>com.nimbusds</groupId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<!-- Spring Boot Starter Logging --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-logging</artifactId> | ||
</dependency> | ||
|
||
<!-- Spring Boot Starter Test (optional, for testing purposes) --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Spring Security Test (optional, for testing security) --> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
30 changes: 30 additions & 0 deletions
30
...thorization-server/src/main/java/org/apache/dubbo/rest/demo/AuthorizationApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.dubbo.rest.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class AuthorizationApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(AuthorizationApplication.class, args); | ||
System.out.println("authorization service started"); | ||
} | ||
} | ||
|
73 changes: 73 additions & 0 deletions
73
...tion-server/src/main/java/org/apache/dubbo/rest/demo/config/AuthorizationSeverConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.dubbo.rest.demo.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository; | ||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; | ||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; | ||
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration; | ||
import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
import java.util.UUID; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class AuthorizationSeverConfig { | ||
|
||
private static final String HOST = System.getProperty("authorization.address", "localhost"); | ||
|
||
String issuer = "http://" + HOST + ":9000"; | ||
|
||
@Bean | ||
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception { | ||
OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http); | ||
|
||
return http.build(); | ||
} | ||
|
||
@Bean | ||
public RegisteredClientRepository registeredClientRepository() { | ||
RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString()) | ||
.clientId("49fd8518-12eb-422b-9264-2bae0ab89f66") //configure the client id | ||
.clientSecret("{noop}H3DTtm2fR3GRAdr4ls1mcg") // configure the client secret | ||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) | ||
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) | ||
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) | ||
.redirectUri("http://localhost:9000/oauth2/token") // configure the redirect uri | ||
.scope("openid") | ||
.scope("read") | ||
.scope("write") | ||
.build(); | ||
|
||
return new InMemoryRegisteredClientRepository(registeredClient); | ||
} | ||
|
||
@Bean | ||
public AuthorizationServerSettings authorizationServerSettings() { | ||
return AuthorizationServerSettings.builder() | ||
.issuer(issuer) // set the address of the authorization server | ||
.build(); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...t-spring-security/spring-security-authorization-server/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
server: | ||
port: 9000 | ||
|
||
spring: | ||
application: | ||
name: authorization-server # Application name for Spring Boot | ||
|
||
logging: | ||
level: | ||
org.springframework.security: DEBUG | ||
org.springframework.boot.autoconfigure.security: DEBUG |
Oops, something went wrong.