This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo-samples.git
The following commit(s) were added to refs/heads/master by this push: new 5f66eb90d UPDATE dubbo-samples-spring-security (#1081) 5f66eb90d is described below commit 5f66eb90d449935cca3a43c0fb191c3b20f37239 Author: 0turnsole0 <130824243+0turnso...@users.noreply.github.com> AuthorDate: Thu Mar 21 19:25:08 2024 +0800 UPDATE dubbo-samples-spring-security (#1081) --- .../dubbo-samples-spring-security-common/pom.xml | 56 ++++ .../DefaultObjectMapperCodecCustomer.java | 2 +- .../SecuritySerializationConfig.java} | 19 +- .../serialization/SerializationApplication.java} | 17 +- .../dubbo-samples-spring-security-consumer/pom.xml | 83 ++++++ .../samples/security/ConsumerApplication.java} | 23 +- .../org/apache/dubbo/samples/security/Task.java} | 49 ++-- .../src/main/resources/application.yml | 28 ++ .../src/test/java/security/ConsumerTests.java | 77 +++++ .../pom.xml | 31 +++ .../dubbo/samples/security}/api/UserService.java | 4 +- .../apache/dubbo/samples/security}/model/User.java | 9 +- .../dubbo-samples-spring-security-provider/pom.xml | 88 ++++++ .../samples/security/ProviderApplication.java} | 32 +-- .../samples/security/impl}/UserServiceImpl.java | 8 +- .../src/main/resources/application.yml | 24 ++ 2-advanced/dubbo-samples-spring-security/pom.xml | 146 ++++------ .../{src/main/resources => }/readme.md | 23 +- .../{src/main/resources => }/readme_zh.md | 25 +- .../org/apache/dubbo/samples/SecurityProvider.java | 59 ---- .../dubbo/samples/zookeeper/EmbeddedZooKeeper.java | 309 --------------------- .../apache/dubbo/samples/zookeeper/ZKTools.java | 107 ------- ...ring.security.jackson.ObjectMapperCodecCustomer | 1 - .../src/main/resources/config-center.properties | 32 --- .../src/main/resources/log4j2.xml | 29 -- .../resources/spring/dubbo-consumer.properties | 21 -- .../resources/spring/dubbo-provider.properties | 22 -- 27 files changed, 557 insertions(+), 767 deletions(-) diff --git a/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/pom.xml b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/pom.xml new file mode 100644 index 000000000..a351dadf5 --- /dev/null +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/pom.xml @@ -0,0 +1,56 @@ +<?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-spring-security</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <artifactId>dubbo-samples-spring-security-common</artifactId> + + <dependencies> + + <!-- dubbo --> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo</artifactId> + </dependency> + + <!-- springboot --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project> diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/custom/DefaultObjectMapperCodecCustomer.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/src/main/java/org/apache/dubbo/samples/security/serialization/DefaultObjectMapperCodecCustomer.java similarity index 95% copy from 2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/custom/DefaultObjectMapperCodecCustomer.java copy to 2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/src/main/java/org/apache/dubbo/samples/security/serialization/DefaultObjectMapperCodecCustomer.java index d89889bfa..a2ed0607e 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/custom/DefaultObjectMapperCodecCustomer.java +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/src/main/java/org/apache/dubbo/samples/security/serialization/DefaultObjectMapperCodecCustomer.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.dubbo.samples.custom; +package org.apache.dubbo.samples.security.serialization; import org.apache.dubbo.spring.security.jackson.ObjectMapperCodec; import org.apache.dubbo.spring.security.jackson.ObjectMapperCodecCustomer; diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/api/UserService.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/src/main/java/org/apache/dubbo/samples/security/serialization/SecuritySerializationConfig.java similarity index 68% copy from 2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/api/UserService.java copy to 2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/src/main/java/org/apache/dubbo/samples/security/serialization/SecuritySerializationConfig.java index 1c4e475db..6dc41c4d4 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/api/UserService.java +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/src/main/java/org/apache/dubbo/samples/security/serialization/SecuritySerializationConfig.java @@ -14,17 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.dubbo.samples.security.serialization; -package org.apache.dubbo.samples.api; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; -import org.apache.dubbo.samples.model.User; - -import java.util.List; - -public interface UserService { - - User findByUsername(String username); - - List<User> queryAll(); +@Configuration +public class SecuritySerializationConfig { + @Bean + public DefaultObjectMapperCodecCustomer objectMapperCodecCustomer() { + return new DefaultObjectMapperCodecCustomer(); + } } diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/custom/DefaultObjectMapperCodecCustomer.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/src/main/java/org/apache/dubbo/samples/security/serialization/SerializationApplication.java similarity index 65% rename from 2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/custom/DefaultObjectMapperCodecCustomer.java rename to 2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/src/main/java/org/apache/dubbo/samples/security/serialization/SerializationApplication.java index d89889bfa..77af8b9cf 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/custom/DefaultObjectMapperCodecCustomer.java +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-common/src/main/java/org/apache/dubbo/samples/security/serialization/SerializationApplication.java @@ -14,16 +14,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.dubbo.samples.security.serialization; -package org.apache.dubbo.samples.custom; +import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.apache.dubbo.spring.security.jackson.ObjectMapperCodec; -import org.apache.dubbo.spring.security.jackson.ObjectMapperCodecCustomer; - -public class DefaultObjectMapperCodecCustomer implements ObjectMapperCodecCustomer { - @Override - public void customize(ObjectMapperCodec objectMapperCodec) { - System.out.println("============objectMapper config============="); +@SpringBootApplication +@EnableDubbo +public class SerializationApplication { + public static void main(String[] args) { + SpringApplication.run(SerializationApplication.class, args); } } diff --git a/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/pom.xml b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/pom.xml new file mode 100644 index 000000000..9599a82df --- /dev/null +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/pom.xml @@ -0,0 +1,83 @@ +<?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-spring-security</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <artifactId>dubbo-samples-spring-security-consumer</artifactId> + + <dependencies> + + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-samples-spring-security-interface</artifactId> + <version>${project.parent.version}</version> + </dependency> + + <!-- dubbo --> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-zookeeper-curator5-spring-boot-starter</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-spring-security</artifactId> + </dependency> + + <!-- springboot --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-log4j2</artifactId> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project> diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/SecurityConsumer.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/main/java/org/apache/dubbo/samples/security/ConsumerApplication.java similarity index 51% copy from 2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/SecurityConsumer.java copy to 2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/main/java/org/apache/dubbo/samples/security/ConsumerApplication.java index 660ac1d73..cc12471cb 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/SecurityConsumer.java +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/main/java/org/apache/dubbo/samples/security/ConsumerApplication.java @@ -15,25 +15,18 @@ * limitations under the License. */ -package org.apache.dubbo.samples; +package org.apache.dubbo.samples.security; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; -public class SecurityConsumer { +@SpringBootApplication +@EnableDubbo +public class ConsumerApplication { - public static void main(String[] args){ - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class); - context.start(); + public static void main(String[] args) { + SpringApplication.run(ConsumerApplication.class, args); } - @Configuration - @EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.client", multipleConfig = true) - @PropertySource("classpath:/spring/dubbo-consumer.properties") - @ComponentScan(value = { "org.apache.dubbo.samples.client" }) - static public class ConsumerConfiguration { - } } diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/client/UserServiceClient.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/main/java/org/apache/dubbo/samples/security/Task.java similarity index 67% rename from 2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/client/UserServiceClient.java rename to 2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/main/java/org/apache/dubbo/samples/security/Task.java index c753d2565..f71c9d7c8 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/client/UserServiceClient.java +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/main/java/org/apache/dubbo/samples/security/Task.java @@ -14,39 +14,56 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package org.apache.dubbo.samples.client; +package org.apache.dubbo.samples.security; import org.apache.dubbo.common.utils.Assert; import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.samples.api.UserService; -import org.apache.dubbo.samples.model.User; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.SmartInitializingSingleton; +import org.apache.dubbo.samples.security.api.UserService; +import org.apache.dubbo.samples.security.model.User; +import org.springframework.boot.CommandLineRunner; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Service; +import org.springframework.stereotype.Component; import java.util.List; import java.util.function.Predicate; -@Service -public class UserServiceClient implements SmartInitializingSingleton { +@Component +public class Task implements CommandLineRunner { - private static final Logger LOG = LoggerFactory.getLogger(UserServiceClient.class); @DubboReference private UserService userService; @Override - public void afterSingletonsInstantiated() { + public void run(String... args) { + new Thread(()-> { + int num = 0; + while (true) { + try { + Thread.sleep(5000); + test(); + num++; + System.out.println("The number of program executions: " + num); + } catch (InterruptedException e) { + e.printStackTrace(); + Thread.currentThread().interrupt(); + } + } + }).start(); + } + + public void test() { findByUsername(); assertThrowable(()->{ - bindSecurityContext(null); - userService.queryAll(); + bindSecurityContext(AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")); + List<User> users = userService.queryAll(); + for (User user :users) { + System.out.println("queryAll result: " + user); + } + }, ex-> ex instanceof RpcException && ((RpcException) ex).isAuthorization()); } @@ -54,8 +71,7 @@ public class UserServiceClient implements SmartInitializingSingleton { bindSecurityContext(AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN")); User user = userService.findByUsername("dubbo"); - - LOG.info("result {} ",user); + System.out.println("findByUsername result: " + user); Assert.notNull(user, "user is null"); } @@ -74,4 +90,5 @@ public class UserServiceClient implements SmartInitializingSingleton { Assert.assertTrue(result, String.format("test error %s", ex.getMessage())); } } + } diff --git a/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/main/resources/application.yml b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/main/resources/application.yml new file mode 100644 index 000000000..2d7752201 --- /dev/null +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/main/resources/application.yml @@ -0,0 +1,28 @@ +#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. + +dubbo: + application: + name: dubbo-samples-spring-security-consumer + consumer: + loadbalance: random + timeout: 6666 + protocol: + port: -1 + threadpool: fixed + threads: 100 + registry: + address: zookeeper://${zookeeper.address:127.0.0.1}:2181 + diff --git a/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/test/java/security/ConsumerTests.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/test/java/security/ConsumerTests.java new file mode 100644 index 000000000..412379a1b --- /dev/null +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-consumer/src/test/java/security/ConsumerTests.java @@ -0,0 +1,77 @@ +/* + * 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 security; + +import org.apache.dubbo.samples.security.ConsumerApplication; +import org.apache.dubbo.samples.security.api.UserService; +import org.apache.dubbo.samples.security.model.User; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +@SpringBootTest(classes = ConsumerApplication.class) +@ExtendWith(MockitoExtension.class) +class ConsumerTests { + + @Mock + private UserService userService; + + @InjectMocks + private ConsumerApplication consumerApplication; + + @Test + void testFindByUsername() { + // Arrange + String username = "testUser"; + User expectedUser = new User(); + expectedUser.setGender("male"); + expectedUser.setUserName(username); + expectedUser.setId("123456"); + when(userService.findByUsername(username)).thenReturn(expectedUser); + + // Act + User actualUser = userService.findByUsername(username); + + // Assert + assertEquals(expectedUser, actualUser); + verify(userService, times(1)).findByUsername(username); + } + + @Test + void testQueryAll() { + // Arrange + List<User> expectedUsers = new ArrayList<>(); + expectedUsers.add(new User()); + when(userService.queryAll()).thenReturn(expectedUsers); + + // Act + List<User> actualUsers = userService.queryAll(); + + // Assert + assertEquals(expectedUsers, actualUsers); + verify(userService, times(1)).queryAll(); + } +} diff --git a/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-interface/pom.xml b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-interface/pom.xml new file mode 100644 index 000000000..01b088ce9 --- /dev/null +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-interface/pom.xml @@ -0,0 +1,31 @@ +<?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-spring-security</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <artifactId>dubbo-samples-spring-security-interface</artifactId> + +</project> diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/api/UserService.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-interface/src/main/java/org/apache/dubbo/samples/security/api/UserService.java similarity index 90% rename from 2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/api/UserService.java rename to 2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-interface/src/main/java/org/apache/dubbo/samples/security/api/UserService.java index 1c4e475db..6f3dafe8e 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/api/UserService.java +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-interface/src/main/java/org/apache/dubbo/samples/security/api/UserService.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package org.apache.dubbo.samples.api; +package org.apache.dubbo.samples.security.api; -import org.apache.dubbo.samples.model.User; +import org.apache.dubbo.samples.security.model.User; import java.util.List; diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/model/User.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-interface/src/main/java/org/apache/dubbo/samples/security/model/User.java similarity index 96% rename from 2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/model/User.java rename to 2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-interface/src/main/java/org/apache/dubbo/samples/security/model/User.java index ad334a0b3..3c0d9093b 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/model/User.java +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-interface/src/main/java/org/apache/dubbo/samples/security/model/User.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.dubbo.samples.model; +package org.apache.dubbo.samples.security.model; import java.io.Serializable; @@ -24,18 +24,23 @@ public class User implements Serializable { private String id; private String userName; private String gender; + public String getId() { return id; } + public void setId(String id) { this.id = id; } + public String getUserName() { return userName; } + public void setUserName(String userName) { this.userName = userName; } + public String getGender() { return gender; } @@ -43,8 +48,10 @@ public class User implements Serializable { public void setGender(String gender) { this.gender = gender; } + @Override public String toString() { return "User{" + "id='" + id + '\'' + ", userName='" + userName + '\'' + ", gender='" + gender + '\'' + '}'; } + } diff --git a/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/pom.xml b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/pom.xml new file mode 100644 index 000000000..abff490f7 --- /dev/null +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/pom.xml @@ -0,0 +1,88 @@ +<?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-spring-security</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <artifactId>dubbo-samples-spring-security-provider</artifactId> + + <dependencies> + + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-samples-spring-security-interface</artifactId> + <version>${project.parent.version}</version> + </dependency> + + <!-- dubbo --> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-spring-security</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-zookeeper-curator5-spring-boot-starter</artifactId> + </dependency> + + <!-- springboot --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.security</groupId> + <artifactId>spring-security-config</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-log4j2</artifactId> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project> diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/SecurityConsumer.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/src/main/java/org/apache/dubbo/samples/security/ProviderApplication.java similarity index 51% rename from 2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/SecurityConsumer.java rename to 2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/src/main/java/org/apache/dubbo/samples/security/ProviderApplication.java index 660ac1d73..dbb5d517c 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/SecurityConsumer.java +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/src/main/java/org/apache/dubbo/samples/security/ProviderApplication.java @@ -14,26 +14,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package org.apache.dubbo.samples; +package org.apache.dubbo.samples.security; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; -public class SecurityConsumer { +import java.util.concurrent.CountDownLatch; - public static void main(String[] args){ - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class); - context.start(); - } +@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) +@EnableCaching +@SpringBootApplication +@EnableDubbo +public class ProviderApplication { + private static CountDownLatch countDownLatch = new CountDownLatch(1); - @Configuration - @EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.client", multipleConfig = true) - @PropertySource("classpath:/spring/dubbo-consumer.properties") - @ComponentScan(value = { "org.apache.dubbo.samples.client" }) - static public class ConsumerConfiguration { + public static void main(String[] args) throws InterruptedException { + SpringApplication.run(ProviderApplication.class, args).registerShutdownHook(); + countDownLatch.await(); } + } diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/service/UserServiceImpl.java b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/src/main/java/org/apache/dubbo/samples/security/impl/UserServiceImpl.java similarity index 90% rename from 2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/service/UserServiceImpl.java rename to 2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/src/main/java/org/apache/dubbo/samples/security/impl/UserServiceImpl.java index 9c7b7614b..5e148142d 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/service/UserServiceImpl.java +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/src/main/java/org/apache/dubbo/samples/security/impl/UserServiceImpl.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package org.apache.dubbo.samples.service; +package org.apache.dubbo.samples.security.impl; import org.apache.dubbo.config.annotation.DubboService; -import org.apache.dubbo.samples.api.UserService; -import org.apache.dubbo.samples.model.User; +import org.apache.dubbo.samples.security.api.UserService; +import org.apache.dubbo.samples.security.model.User; import org.springframework.security.access.annotation.Secured; import org.springframework.security.access.prepost.PreAuthorize; @@ -40,8 +40,8 @@ public class UserServiceImpl implements UserService { return user; } - @Secured("ROLE_USER") @Override + @Secured("ROLE_USER") public List<User> queryAll() { List<User> result = new ArrayList<>(); result.add(new User()); diff --git a/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/src/main/resources/application.yml b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/src/main/resources/application.yml new file mode 100644 index 000000000..980df4d70 --- /dev/null +++ b/2-advanced/dubbo-samples-spring-security/dubbo-samples-spring-security-provider/src/main/resources/application.yml @@ -0,0 +1,24 @@ +#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. +dubbo: + application: + name: dubbo-samples-spring-security-provider + protocol: + port: -1 + provider: + timeout: 5000 + token: true + registry: + address: zookeeper://${zookeeper.address:127.0.0.1}:2181 diff --git a/2-advanced/dubbo-samples-spring-security/pom.xml b/2-advanced/dubbo-samples-spring-security/pom.xml index 4f3f172f8..94799ac75 100644 --- a/2-advanced/dubbo-samples-spring-security/pom.xml +++ b/2-advanced/dubbo-samples-spring-security/pom.xml @@ -22,35 +22,56 @@ <modelVersion>4.0.0</modelVersion> <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-samples-spring-security</artifactId> <version>1.0-SNAPSHOT</version> - <artifactId>dubbo-samples-spring-security</artifactId> + <packaging>pom</packaging> + <name>Dubbo Samples Spring Security</name> - <url>http://maven.apache.org</url> + + <modules> + <module>dubbo-samples-spring-security-consumer</module> + <module>dubbo-samples-spring-security-provider</module> + <module>dubbo-samples-spring-security-interface</module> + <module>dubbo-samples-spring-security-common</module> + </modules> <properties> - <maven.compiler.source>1.8</maven.compiler.source> - <maven.compiler.target>1.8</maven.compiler.target> + <maven.compiler.source>17</maven.compiler.source> + <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <log4j2.version>2.20.0</log4j2.version> - <dubbo.version>3.2.6</dubbo.version> - <spring.version>4.3.30.RELEASE</spring.version> - <spring-boot.version>2.7.8</spring-boot.version> + <dubbo.version>3.3.0-beta.1</dubbo.version> + <spring-boot.version>3.2.3</spring-boot.version> + + <junit5.version>5.10.1</junit5.version> + <junit.platform>1.9.3</junit.platform> <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version> </properties> + <dependencyManagement> <dependencies> <dependency> - <groupId>org.apache.dubbo</groupId> - <artifactId>dubbo-dependencies-bom</artifactId> - <version>${dubbo.version}</version> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-dependencies</artifactId> + <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + <version>${spring-boot.version}</version> + <exclusions> + <exclusion> + <artifactId>spring-boot-starter-logging</artifactId> + <groupId>org.springframework.boot</groupId> + </exclusion> + </exclusions> + </dependency> <dependency> <groupId>org.apache.dubbo</groupId> @@ -60,98 +81,33 @@ <scope>import</scope> </dependency> - <dependency> - <groupId>org.apache.dubbo</groupId> - <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId> - <version>${dubbo.version}</version> - <type>pom</type> - <exclusions> - <exclusion> - <artifactId>logback-classic</artifactId> - <groupId>ch.qos.logback</groupId> - </exclusion> - <exclusion> - <artifactId>logback-core</artifactId> - <groupId>ch.qos.logback</groupId> - </exclusion> - <exclusion> - <artifactId>log4j</artifactId> - <groupId>log4j</groupId> - </exclusion> - <exclusion> - <artifactId>slf4j-log4j12</artifactId> - <groupId>org.slf4j</groupId> - </exclusion> - </exclusions> - </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> - <groupId>org.apache.dubbo</groupId> - <artifactId>dubbo</artifactId> - </dependency> - - <dependency> - <groupId>org.apache.dubbo</groupId> - <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId> - <type>pom</type> - </dependency> - - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-slf4j-impl</artifactId> - <version>${log4j2.version}</version> - </dependency> - - <dependency> - <groupId>org.apache.dubbo</groupId> - <artifactId>dubbo-spring-security</artifactId> - </dependency> - - <dependency> - <groupId>org.springframework.security</groupId> - <artifactId>spring-security-config</artifactId> - </dependency> - - <!-- Embedded Zookeeper Server Dependencies --> - <dependency> - <groupId>io.dropwizard.metrics</groupId> - <artifactId>metrics-core</artifactId> - <version>4.1.12.1</version> - </dependency> - <dependency> - <groupId>org.xerial.snappy</groupId> - <artifactId>snappy-java</artifactId> - <version>1.1.10.5</version> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> </dependency> </dependencies> - <profiles> - <!-- For jdk 11 above JavaEE annotation --> - <profile> - <id>javax.annotation</id> - <activation> - <jdk>[1.11,)</jdk> - </activation> - <dependencies> - <dependency> - <groupId>javax.annotation</groupId> - <artifactId>javax.annotation-api</artifactId> - <version>1.3.2</version> - </dependency> - </dependencies> - </profile> - </profiles> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>${maven-compiler-plugin.version}</version> - </plugin> - </plugins> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <version>${spring-boot.version}</version> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </pluginManagement> </build> </project> diff --git a/2-advanced/dubbo-samples-spring-security/src/main/resources/readme.md b/2-advanced/dubbo-samples-spring-security/readme.md similarity index 85% rename from 2-advanced/dubbo-samples-spring-security/src/main/resources/readme.md rename to 2-advanced/dubbo-samples-spring-security/readme.md index 6ace78f4b..46c6bd748 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/resources/readme.md +++ b/2-advanced/dubbo-samples-spring-security/readme.md @@ -51,9 +51,8 @@ A --> B --> C #### provider enable security ```java -@Configuration -@EnableMethodSecurity(jsr250Enabled = true, securedEnabled = true) -public class SecurityConfiguration { +@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) +public class ProviderApplication { } ``` @@ -62,10 +61,18 @@ public class SecurityConfiguration { ### custom serialization -+ In the resources directory, add `/META-INF/dubbo/` directory. -+ Add a configuration file, filename ` org.apache.dubbo.spring.security.jackson.ObjectMapperCodecCustomer` . -+ File contents `customizerConfigurationObjectMapperCodecCustomer=org.apache.dubbo.samples.custom.DefaultObjectMapperCodecCustomer`. -+ Implementation code ++ Add a configuration class. + +```java +@Configuration +public class SecuritySerializationConfig { + + @Bean + public DefaultObjectMapperCodecCustomer objectMapperCodecCustomer() { + return new DefaultObjectMapperCodecCustomer(); + } +} +``` ```java public class DefaultObjectMapperCodecCustomer implements ObjectMapperCodecCustomer { @@ -83,5 +90,3 @@ public class DefaultObjectMapperCodecCustomer implements ObjectMapperCodecCustom + If you do not have a custom implementation of deserializer errors, Dubbo ignores the current error + In spring security, custom deserialization implementation references `UsernamePasswordAuthenticationTokenDeserializer` - - diff --git a/2-advanced/dubbo-samples-spring-security/src/main/resources/readme_zh.md b/2-advanced/dubbo-samples-spring-security/readme_zh.md similarity index 85% rename from 2-advanced/dubbo-samples-spring-security/src/main/resources/readme_zh.md rename to 2-advanced/dubbo-samples-spring-security/readme_zh.md index 93c10e005..2a9c0f061 100644 --- a/2-advanced/dubbo-samples-spring-security/src/main/resources/readme_zh.md +++ b/2-advanced/dubbo-samples-spring-security/readme_zh.md @@ -51,9 +51,8 @@ A --> B --> C #### provider 启用 Security 配置 ```java -@Configuration -@EnableMethodSecurity(jsr250Enabled = true, securedEnabled = true) -public class SecurityConfiguration { +@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) +public class ProviderApplication { } ``` @@ -62,9 +61,19 @@ public class SecurityConfiguration { ### 自定义序列化 -+ 在 resources 目录下,添加 `/META-INF/dubbo/` 目录。 -+ 添加配置文件,文件名 ` org.apache.dubbo.spring.security.jackson.ObjectMapperCodecCustomer` 。 -+ 文件内容 `customizerConfigurationObjectMapperCodecCustomer=org.apache.dubbo.samples.custom.DefaultObjectMapperCodecCustomer` 。 ++ 添加配置类。 + +```java +@Configuration +public class SecuritySerializationConfig { + + @Bean + public DefaultObjectMapperCodecCustomer objectMapperCodecCustomer() { + return new DefaultObjectMapperCodecCustomer(); + } +} +``` + + 自定义实现代码 ```java @@ -84,7 +93,3 @@ public class DefaultObjectMapperCodecCustomer implements ObjectMapperCodecCustom + 如果没有自定义实现反序列化器产生的错误,dubbo 会忽略当前错误 + 在Spring Security 中,自定义反序列器可以参考`UsernamePasswordAuthenticationTokenDeserializer` - - - - diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/SecurityProvider.java b/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/SecurityProvider.java deleted file mode 100644 index c209f56bc..000000000 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/SecurityProvider.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.samples; - -import org.apache.dubbo.config.ProviderConfig; -import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; -import org.apache.dubbo.samples.zookeeper.EmbeddedZooKeeper; -import org.apache.dubbo.samples.zookeeper.ZKTools; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; - -import java.util.concurrent.CountDownLatch; - -public class SecurityProvider { - - public static void main(String[] args) throws Exception { - new EmbeddedZooKeeper(2181, false).start(); - ZKTools.generateDubboProperties(); - - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( - ProviderConfiguration.class); - - context.start(); - - System.out.println("dubbo service started"); - new CountDownLatch(1).await(); - } - - @Configuration - @EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.service") - @PropertySource("classpath:/spring/dubbo-provider.properties") - @EnableMethodSecurity(jsr250Enabled = true, securedEnabled = true) - static public class ProviderConfiguration { - @Bean - public ProviderConfig providerConfig() { - ProviderConfig providerConfig = new ProviderConfig(); - providerConfig.setTimeout(1000); - return providerConfig; - } - } -} diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/zookeeper/EmbeddedZooKeeper.java b/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/zookeeper/EmbeddedZooKeeper.java deleted file mode 100644 index a04ae8df0..000000000 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/zookeeper/EmbeddedZooKeeper.java +++ /dev/null @@ -1,309 +0,0 @@ - -/* - * Copyright 2014 the original author or authors. - * - * Licensed 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.samples.zookeeper; - -import org.apache.zookeeper.server.ServerConfig; -import org.apache.zookeeper.server.ZooKeeperServerMain; -import org.apache.zookeeper.server.quorum.QuorumPeerConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.SmartLifecycle; -import org.springframework.util.ErrorHandler; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Method; -import java.net.ServerSocket; -import java.util.List; -import java.util.Properties; -import java.util.Random; -import java.util.UUID; -import java.util.stream.Collectors; - -/** - * from: https://github.com/spring-projects/spring-xd/blob/v1.3.1.RELEASE/spring-xd-dirt/src/main/java/org/springframework/xd/dirt/zookeeper/ZooKeeperUtils.java - * <p> - * Helper class to start an embedded instance of standalone (non clustered) ZooKeeper. - * <p> - * NOTE: at least an external standalone server (if not an ensemble) are recommended, even for - * {@link org.springframework.xd.dirt.server.singlenode.SingleNodeApplication} - * - * @author Patrick Peralta - * @author Mark Fisher - * @author David Turanski - */ -public class EmbeddedZooKeeper implements SmartLifecycle { - - private static final Random RANDOM = new Random(); - - /** - * Logger. - */ - private static final Logger logger = LoggerFactory.getLogger(EmbeddedZooKeeper.class); - - /** - * ZooKeeper client port. This will be determined dynamically upon startup. - */ - private final int clientPort; - - /** - * Whether to auto-start. Default is true. - */ - private boolean autoStartup = true; - - /** - * Lifecycle phase. Default is 0. - */ - private int phase = 0; - - /** - * Thread for running the ZooKeeper server. - */ - private volatile Thread zkServerThread; - - /** - * ZooKeeper server. - */ - private volatile ZooKeeperServerMain zkServer; - - /** - * {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. - */ - private ErrorHandler errorHandler; - - private boolean daemon = true; - - /** - * Construct an EmbeddedZooKeeper with a random port. - */ - public EmbeddedZooKeeper() { - clientPort = findRandomPort(30000, 65535); - } - - /** - * Construct an EmbeddedZooKeeper with the provided port. - * - * @param clientPort port for ZooKeeper server to bind to - */ - public EmbeddedZooKeeper(int clientPort, boolean daemon) { - this.clientPort = clientPort; - this.daemon = daemon; - } - - /** - * Returns the port that clients should use to connect to this embedded server. - * - * @return dynamically determined client port - */ - public int getClientPort() { - return this.clientPort; - } - - /** - * Specify whether to start automatically. Default is true. - * - * @param autoStartup whether to start automatically - */ - public void setAutoStartup(boolean autoStartup) { - this.autoStartup = autoStartup; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isAutoStartup() { - return this.autoStartup; - } - - /** - * Specify the lifecycle phase for the embedded server. - * - * @param phase the lifecycle phase - */ - public void setPhase(int phase) { - this.phase = phase; - } - - /** - * {@inheritDoc} - */ - @Override - public int getPhase() { - return this.phase; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isRunning() { - return (zkServerThread != null); - } - - /** - * Start the ZooKeeper server in a background thread. - * <p> - * Register an error handler via {@link #setErrorHandler} in order to handle - * any exceptions thrown during startup or execution. - */ - @Override - public synchronized void start() { - if (zkServerThread == null) { - zkServerThread = new Thread(new ServerRunnable(), "ZooKeeper Server Starter"); - zkServerThread.setDaemon(daemon); - zkServerThread.start(); - } - } - - /** - * Shutdown the ZooKeeper server. - */ - @Override - public synchronized void stop() { - if (zkServerThread != null) { - // The shutdown method is protected...thus this hack to invoke it. - // This will log an exception on shutdown; see - // https://issues.apache.org/jira/browse/ZOOKEEPER-1873 for details. - try { - Method shutdown = ZooKeeperServerMain.class.getDeclaredMethod("shutdown"); - shutdown.setAccessible(true); - shutdown.invoke(zkServer); - } catch (Exception e) { - throw new RuntimeException(e); - } - - // It is expected that the thread will exit after - // the server is shutdown; this will block until - // the shutdown is complete. - try { - zkServerThread.join(5000); - zkServerThread = null; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - logger.warn("Interrupted while waiting for embedded ZooKeeper to exit"); - // abandoning zk thread - zkServerThread = null; - } - } - } - - /** - * Stop the server if running and invoke the callback when complete. - */ - @Override - public void stop(Runnable callback) { - stop(); - callback.run(); - } - - /** - * Provide an {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. If none - * is provided, only error-level logging will occur. - * - * @param errorHandler the {@link ErrorHandler} to be invoked - */ - public void setErrorHandler(ErrorHandler errorHandler) { - this.errorHandler = errorHandler; - } - - /** - * Runnable implementation that starts the ZooKeeper server. - */ - private class ServerRunnable implements Runnable { - - @Override - public void run() { - try { - Properties properties = new Properties(); - File file = new File(System.getProperty("java.io.tmpdir") - + File.separator + UUID.randomUUID()); - file.deleteOnExit(); - properties.setProperty("dataDir", file.getAbsolutePath()); - properties.setProperty("clientPort", String.valueOf(clientPort)); - - QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); - quorumPeerConfig.parseProperties(properties); - - zkServer = new ZooKeeperServerMain(); - ServerConfig configuration = new ServerConfig(); - configuration.readFrom(quorumPeerConfig); - - System.setProperty("zookeeper.admin.enableServer", "false"); - - zkServer.runFromConfig(configuration); - } catch (Exception e) { - if (errorHandler != null) { - errorHandler.handleError(e); - } else { - logger.error("Exception running embedded ZooKeeper", e); - } - } - } - } - - /** - * Workaround for SocketUtils.findRandomPort() deprecation. - * - * @param min min port - * @param max max port - * @return a random generated available port - */ - private static int findRandomPort(int min, int max) { - if (min < 1024) { - throw new IllegalArgumentException("Max port shouldn't be less than 1024."); - } - - if (max > 65535) { - throw new IllegalArgumentException("Max port shouldn't be greater than 65535."); - } - - if (min > max) { - throw new IllegalArgumentException("Min port shouldn't be greater than max port."); - } - - int port = 0; - int counter = 0; - - // Workaround for legacy JDK doesn't support Random.nextInt(min, max). - List<Integer> randomInts = RANDOM.ints(min, max + 1) - .limit(max - min) - .mapToObj(Integer::valueOf) - .collect(Collectors.toList()); - - do { - if (counter > max - min) { - throw new IllegalStateException("Unable to find a port between " + min + "-" + max); - } - - port = randomInts.get(counter); - counter++; - } while (isPortInUse(port)); - - return port; - } - - private static boolean isPortInUse(int port) { - try (ServerSocket ignored = new ServerSocket(port)) { - return false; - } catch (IOException e) { - // continue - } - return true; - } -} diff --git a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/zookeeper/ZKTools.java b/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/zookeeper/ZKTools.java deleted file mode 100644 index 5aa6ad092..000000000 --- a/2-advanced/dubbo-samples-spring-security/src/main/java/org/apache/dubbo/samples/zookeeper/ZKTools.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * 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.samples.zookeeper; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.dubbo.common.utils.StringUtils; - -public class ZKTools { - private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1"); - private static CuratorFramework client; - - public static void main(String[] args) throws Exception { - generateDubboProperties(); - } - - public static void generateDubboProperties() { - client = CuratorFrameworkFactory.newClient(zookeeperHost + ":2181", 60 * 1000, 60 * 1000, - new ExponentialBackoffRetry(1000, 3)); - client.start(); - - generateDubboPropertiesForGlobal(); - generateDubboPropertiesForProvider(); - generateDubboPropertiesForConsumer(); - } - - public static void generateDubboPropertiesForGlobal() { - String str = "dubbo.registry.address=zookeeper://" + zookeeperHost + ":2181\n" + - "#global config for consumer\n" + - "dubbo.consumer.timeout=6000\n" + - "#global config for provider\n" + - "dubbo.protocol.port=20990\n" + - "dubbo.provider.timeout=5000"; - - System.out.println(str); - - try { - String path = "/dubbo/config/dubbo/dubbo.properties"; - if (client.checkExists().forPath(path) == null) { - client.create().creatingParentsIfNeeded().forPath(path); - } - setData(path, str); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public static void generateDubboPropertiesForConsumer() { - String str = "dubbo.consumer.timeout=6666"; - - System.out.println(str); - - try { - String path = "/dubbo/config/configcenter-annotation-consumer/dubbo.properties"; - if (client.checkExists().forPath(path) == null) { - client.create().creatingParentsIfNeeded().forPath(path); - } - setData(path, str); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public static void generateDubboPropertiesForProvider() { - String str = "dubbo.protocol.threadpool=fixed\n" + - "dubbo.protocol.threads=100"; - - System.out.println(str); - - try { - String path = "/dubbo/config/configcenter-annotation-provider/dubbo.properties"; - if (client.checkExists().forPath(path) == null) { - client.create().creatingParentsIfNeeded().forPath(path); - } - setData(path, str); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private static void setData(String path, String data) throws Exception { - client.setData().forPath(path, data.getBytes()); - } - - private static String pathToKey(String path) { - if (StringUtils.isEmpty(path)) { - return path; - } - return path.replace("/dubbo/config/", "").replaceAll("/", "."); - } - -} diff --git a/2-advanced/dubbo-samples-spring-security/src/main/resources/META-INF/dubbo/org.apache.dubbo.spring.security.jackson.ObjectMapperCodecCustomer b/2-advanced/dubbo-samples-spring-security/src/main/resources/META-INF/dubbo/org.apache.dubbo.spring.security.jackson.ObjectMapperCodecCustomer deleted file mode 100644 index ed4d694b4..000000000 --- a/2-advanced/dubbo-samples-spring-security/src/main/resources/META-INF/dubbo/org.apache.dubbo.spring.security.jackson.ObjectMapperCodecCustomer +++ /dev/null @@ -1 +0,0 @@ -objectMapperCodecCustomer=org.apache.dubbo.samples.custom.DefaultObjectMapperCodecCustomer \ No newline at end of file diff --git a/2-advanced/dubbo-samples-spring-security/src/main/resources/config-center.properties b/2-advanced/dubbo-samples-spring-security/src/main/resources/config-center.properties deleted file mode 100644 index a4b13ea0b..000000000 --- a/2-advanced/dubbo-samples-spring-security/src/main/resources/config-center.properties +++ /dev/null @@ -1,32 +0,0 @@ -# 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. - -# Add the following global external properties to `/dubbo/config/dubbo/dubbo.properties` -dubbo.registry.address=zookeeper://127.0.0.1:2181 -#global config for consumer -dubbo.consumer.loadbalance=random -#global config for provider -dubbo.protocol.port=20990 -dubbo.provider.timeout=5000 -dubbo.registry.simplified=true - -# Optional. Add the following application(Provider) specific external properties -# to `/dubbo/config/configcenter-annotation-provider/dubbo.properties` -dubbo.protocol.threadpool=fixed -dubbo.protocol.threads=100 - -# Optional. Add the following application(Consumer) specific external properties -# to `/dubbo/config/configcenter-annotation-consumer/dubbo.properties` -dubbo.consumer.timeout=6666 diff --git a/2-advanced/dubbo-samples-spring-security/src/main/resources/log4j2.xml b/2-advanced/dubbo-samples-spring-security/src/main/resources/log4j2.xml deleted file mode 100644 index 69e1321d2..000000000 --- a/2-advanced/dubbo-samples-spring-security/src/main/resources/log4j2.xml +++ /dev/null @@ -1,29 +0,0 @@ -<?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. - --> -<Configuration status="WARN"> - <Appenders> - <Console name="Console" target="SYSTEM_OUT" follow="true"> - <PatternLayout pattern="%style{%d{HH:mm:ss.SSS}}{Magenta} %style{|-}{White}%highlight{%-5p} [%t] %style{%40.40c}{Cyan}:%style{%-3L}{Blue} %style{-|}{White} %m%n%rEx{filters(jdk.internal.reflect,java.lang.reflect,sun.reflect)}" disableAnsi="false" charset="UTF-8"/> - </Console> - </Appenders> - <Loggers> - <Root level="info"> - <AppenderRef ref="Console"/> - </Root> - </Loggers> -</Configuration> diff --git a/2-advanced/dubbo-samples-spring-security/src/main/resources/spring/dubbo-consumer.properties b/2-advanced/dubbo-samples-spring-security/src/main/resources/spring/dubbo-consumer.properties deleted file mode 100644 index af566b5ca..000000000 --- a/2-advanced/dubbo-samples-spring-security/src/main/resources/spring/dubbo-consumer.properties +++ /dev/null @@ -1,21 +0,0 @@ -# -# -# 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. -# -# -# TODO application should not be restricted to plural mode. -dubbo.applications.configcenter-annotation-provider.name=configcenter-security-provider -dubbo.config-centers.zookeeper.address=zookeeper://${zookeeper.address:127.0.0.1}:2181 diff --git a/2-advanced/dubbo-samples-spring-security/src/main/resources/spring/dubbo-provider.properties b/2-advanced/dubbo-samples-spring-security/src/main/resources/spring/dubbo-provider.properties deleted file mode 100644 index e85a1c84a..000000000 --- a/2-advanced/dubbo-samples-spring-security/src/main/resources/spring/dubbo-provider.properties +++ /dev/null @@ -1,22 +0,0 @@ -# -# -# 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. -# -# - -dubbo.application.name=configcenter-security-provider -dubbo.config-center.address=zookeeper://${zookeeper.address:127.0.0.1}:2181 -dubbo.provider.token=true \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org