mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
примеры к занятию
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.otus.spring.sso.controller;
|
||||||
|
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.security.oauth2.core.user.OAuth2User;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class UserController {
|
||||||
|
@GetMapping("/user")
|
||||||
|
public Map<String, Object> user( @AuthenticationPrincipal OAuth2User principal) {
|
||||||
|
return Collections.singletonMap("name", principal.getAttribute("name"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package ru.otus.spring.sso.security;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
|
||||||
|
|
||||||
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
@Override
|
||||||
|
protected void configure( HttpSecurity http ) throws Exception {
|
||||||
|
// @formatter:off
|
||||||
|
http
|
||||||
|
.authorizeRequests( a -> a
|
||||||
|
.antMatchers( "/", "/error", "/webjars/**" ).permitAll()
|
||||||
|
.anyRequest().authenticated()
|
||||||
|
)
|
||||||
|
.exceptionHandling( e -> e
|
||||||
|
.authenticationEntryPoint( new HttpStatusEntryPoint( HttpStatus.UNAUTHORIZED ) )
|
||||||
|
)
|
||||||
|
.csrf().disable()
|
||||||
|
.logout( l -> l
|
||||||
|
.logoutSuccessUrl( "/" ).permitAll()
|
||||||
|
)
|
||||||
|
|
||||||
|
.oauth2Login();
|
||||||
|
// @formatter:on
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/build/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<groupId>ru.otus</groupId>
|
||||||
|
<artifactId>spring-31</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>spring-31-exercise</module>
|
||||||
|
<module>spring-31-solution</module>
|
||||||
|
|
||||||
|
</modules>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<groupId>ru.otus</groupId>
|
||||||
|
<artifactId>spring-31-exercise</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.3.3.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-integration</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-messaging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.7</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
+70
@@ -0,0 +1,70 @@
|
|||||||
|
package ru.otus.spring.integration;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.RandomUtils;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.support.AbstractApplicationContext;
|
||||||
|
import org.springframework.integration.annotation.IntegrationComponentScan;
|
||||||
|
|
||||||
|
import org.springframework.integration.channel.PublishSubscribeChannel;
|
||||||
|
import org.springframework.integration.channel.QueueChannel;
|
||||||
|
import org.springframework.integration.config.EnableIntegration;
|
||||||
|
import org.springframework.integration.dsl.IntegrationFlow;
|
||||||
|
import org.springframework.integration.dsl.IntegrationFlows;
|
||||||
|
|
||||||
|
import org.springframework.integration.dsl.MessageChannels;
|
||||||
|
import ru.otus.spring.integration.domain.Food;
|
||||||
|
import ru.otus.spring.integration.domain.OrderItem;
|
||||||
|
|
||||||
|
|
||||||
|
@IntegrationComponentScan
|
||||||
|
@SuppressWarnings({"resource", "Duplicates", "InfiniteLoopStatement"})
|
||||||
|
@ComponentScan
|
||||||
|
@Configuration
|
||||||
|
@EnableIntegration
|
||||||
|
public class App {
|
||||||
|
private static final String[] MENU = {"coffee", "tea", "smoothie", "whiskey", "beer", "cola", "water"};
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public QueueChannel itemsChannel() {
|
||||||
|
return MessageChannels.queue(10).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PublishSubscribeChannel foodChannel() {
|
||||||
|
return MessageChannels.publishSubscribe().get();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: create default poller
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public IntegrationFlow cafeFlow() {
|
||||||
|
return IntegrationFlows.from("itemsChannel")
|
||||||
|
// TODO: cook OrderItem in the kitchen
|
||||||
|
// TODO*: add splitter and aggregator
|
||||||
|
// TODO: forward it to the publish subscriber channel
|
||||||
|
.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(App.class);
|
||||||
|
|
||||||
|
// here we works with cafe using interface
|
||||||
|
Cafe cafe = ctx.getBean(Cafe.class);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
|
||||||
|
OrderItem orderItem = generateOrderItem();
|
||||||
|
System.out.println("New orderItem: " + orderItem.getItemName());
|
||||||
|
Food food = cafe.process(orderItem);
|
||||||
|
System.out.println("Ready food: " + food.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static OrderItem generateOrderItem() {
|
||||||
|
return new OrderItem(MENU[RandomUtils.nextInt(0, MENU.length)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package ru.otus.spring.integration;
|
||||||
|
|
||||||
|
|
||||||
|
import ru.otus.spring.integration.domain.Food;
|
||||||
|
import ru.otus.spring.integration.domain.OrderItem;
|
||||||
|
|
||||||
|
// TODO: add messaging gateway annotation
|
||||||
|
public interface Cafe {
|
||||||
|
|
||||||
|
// TODO: add gateway annotation with required channels
|
||||||
|
Food process(OrderItem orderItem);
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
package ru.otus.spring.integration.domain;
|
||||||
|
|
||||||
|
|
||||||
|
public class Food {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public Food(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
package ru.otus.spring.integration.domain;
|
||||||
|
|
||||||
|
public class OrderItem {
|
||||||
|
|
||||||
|
private final String itemName;
|
||||||
|
|
||||||
|
public OrderItem(String itemName) {
|
||||||
|
this.itemName = itemName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getItemName() {
|
||||||
|
return itemName;
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
package ru.otus.spring.integration.kitchen;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.otus.spring.integration.domain.Food;
|
||||||
|
import ru.otus.spring.integration.domain.OrderItem;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class KitchenService {
|
||||||
|
|
||||||
|
public Food cook(OrderItem orderItem) throws Exception {
|
||||||
|
System.out.println("Cooking " + orderItem.getItemName());
|
||||||
|
Thread.sleep(3000);
|
||||||
|
System.out.println("Cooking " + orderItem.getItemName() + " done");
|
||||||
|
return new Food(orderItem.getItemName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<groupId>ru.otus</groupId>
|
||||||
|
<artifactId>spring-31-solution</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.3.3.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-integration</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-messaging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.7</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
+96
@@ -0,0 +1,96 @@
|
|||||||
|
package ru.otus.spring.integration;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.RandomUtils;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.support.AbstractApplicationContext;
|
||||||
|
import org.springframework.integration.annotation.IntegrationComponentScan;
|
||||||
|
import org.springframework.integration.channel.PublishSubscribeChannel;
|
||||||
|
import org.springframework.integration.channel.QueueChannel;
|
||||||
|
import org.springframework.integration.config.EnableIntegration;
|
||||||
|
import org.springframework.integration.dsl.IntegrationFlow;
|
||||||
|
import org.springframework.integration.dsl.IntegrationFlows;
|
||||||
|
import org.springframework.integration.dsl.MessageChannels;
|
||||||
|
import org.springframework.integration.dsl.Pollers;
|
||||||
|
import org.springframework.integration.scheduling.PollerMetadata;
|
||||||
|
import ru.otus.spring.integration.domain.Food;
|
||||||
|
import ru.otus.spring.integration.domain.OrderItem;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ForkJoinPool;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
|
@IntegrationComponentScan
|
||||||
|
@SuppressWarnings({ "resource", "Duplicates", "InfiniteLoopStatement" })
|
||||||
|
@ComponentScan
|
||||||
|
@Configuration
|
||||||
|
@EnableIntegration
|
||||||
|
public class App {
|
||||||
|
private static final String[] MENU = { "coffee", "tea", "smoothie", "whiskey", "beer", "cola", "water" };
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public QueueChannel itemsChannel() {
|
||||||
|
return MessageChannels.queue( 10 ).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PublishSubscribeChannel foodChannel() {
|
||||||
|
return MessageChannels.publishSubscribe().get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = PollerMetadata.DEFAULT_POLLER)
|
||||||
|
public PollerMetadata poller() {
|
||||||
|
return Pollers.fixedRate( 100 ).maxMessagesPerPoll( 2 ).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public IntegrationFlow cafeFlow() {
|
||||||
|
return IntegrationFlows.from( "itemsChannel" )
|
||||||
|
.split()
|
||||||
|
.handle( "kitchenService", "cook" )
|
||||||
|
.aggregate()
|
||||||
|
.channel( "foodChannel" )
|
||||||
|
.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main( String[] args ) throws Exception {
|
||||||
|
AbstractApplicationContext ctx = new AnnotationConfigApplicationContext( App.class );
|
||||||
|
|
||||||
|
// here we works with cafe using interface
|
||||||
|
Cafe cafe = ctx.getBean( Cafe.class );
|
||||||
|
|
||||||
|
ForkJoinPool pool = ForkJoinPool.commonPool();
|
||||||
|
|
||||||
|
while ( true ) {
|
||||||
|
Thread.sleep( 7000 );
|
||||||
|
|
||||||
|
pool.execute( () -> {
|
||||||
|
Collection<OrderItem> items = generateOrderItems();
|
||||||
|
System.out.println( "New orderItems: " +
|
||||||
|
items.stream().map( OrderItem::getItemName )
|
||||||
|
.collect( Collectors.joining( "," ) ) );
|
||||||
|
Collection<Food> food = cafe.process( items );
|
||||||
|
System.out.println( "Ready food: " + food.stream()
|
||||||
|
.map( Food::getName )
|
||||||
|
.collect( Collectors.joining( "," ) ) );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static OrderItem generateOrderItem() {
|
||||||
|
return new OrderItem( MENU[ RandomUtils.nextInt( 0, MENU.length ) ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Collection<OrderItem> generateOrderItems() {
|
||||||
|
List<OrderItem> items = new ArrayList<>();
|
||||||
|
for ( int i = 0; i < RandomUtils.nextInt( 1, 5 ); ++ i ) {
|
||||||
|
items.add( generateOrderItem() );
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
package ru.otus.spring.integration;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.integration.annotation.Gateway;
|
||||||
|
import org.springframework.integration.annotation.MessagingGateway;
|
||||||
|
import ru.otus.spring.integration.domain.Food;
|
||||||
|
import ru.otus.spring.integration.domain.OrderItem;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
@MessagingGateway
|
||||||
|
public interface Cafe {
|
||||||
|
|
||||||
|
@Gateway(requestChannel = "itemsChannel", replyChannel = "foodChannel")
|
||||||
|
Collection<Food> process(Collection<OrderItem> orderItem);
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
package ru.otus.spring.integration.domain;
|
||||||
|
|
||||||
|
|
||||||
|
public class Food {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public Food(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
package ru.otus.spring.integration.domain;
|
||||||
|
|
||||||
|
public class OrderItem {
|
||||||
|
|
||||||
|
private final String itemName;
|
||||||
|
|
||||||
|
public OrderItem(String itemName) {
|
||||||
|
this.itemName = itemName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getItemName() {
|
||||||
|
return itemName;
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
package ru.otus.spring.integration.kitchen;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.otus.spring.integration.domain.Food;
|
||||||
|
import ru.otus.spring.integration.domain.OrderItem;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class KitchenService {
|
||||||
|
|
||||||
|
public Food cook(OrderItem orderItem) throws Exception {
|
||||||
|
System.out.println("Cooking " + orderItem.getItemName());
|
||||||
|
Thread.sleep(3000);
|
||||||
|
System.out.println("Cooking " + orderItem.getItemName() + " done");
|
||||||
|
return new Food(orderItem.getItemName());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user