Skip to content

Commit

Permalink
UPDATE dubbo-samples-spring-security (apache#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
0turnsole0 authored Mar 21, 2024
1 parent 5701859 commit 5f66eb9
Show file tree
Hide file tree
Showing 27 changed files with 621 additions and 733 deletions.
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.security.serialization;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SecuritySerializationConfig {

@Bean
public DefaultObjectMapperCodecCustomer objectMapperCodecCustomer() {
return new DefaultObjectMapperCodecCustomer();
}
}
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.samples.security.serialization;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class SerializationApplication {

public static void main(String[] args) {
SpringApplication.run(SerializationApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,64 @@
* 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());
}

private void findByUsername() {
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");
}
Expand All @@ -74,4 +90,5 @@ public void assertThrowable(Runnable runnable, Predicate<Exception> throwable){
Assert.assertTrue(result, String.format("test error %s", ex.getMessage()));
}
}

}
Original file line number Diff line number Diff line change
@@ -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

Loading

0 comments on commit 5f66eb9

Please sign in to comment.