|
| 1 | +package com.baeldung.si.security; |
| 2 | + |
| 3 | +import java.util.stream.Collectors; |
| 4 | + |
| 5 | +import org.springframework.context.annotation.Bean; |
| 6 | +import org.springframework.context.annotation.Configuration; |
| 7 | +import org.springframework.integration.annotation.ServiceActivator; |
| 8 | +import org.springframework.integration.channel.DirectChannel; |
| 9 | +import org.springframework.integration.channel.PublishSubscribeChannel; |
| 10 | +import org.springframework.integration.config.EnableIntegration; |
| 11 | +import org.springframework.integration.config.GlobalChannelInterceptor; |
| 12 | +import org.springframework.integration.security.channel.SecuredChannel; |
| 13 | +import org.springframework.integration.security.channel.SecurityContextPropagationChannelInterceptor; |
| 14 | +import org.springframework.integration.support.DefaultMessageBuilderFactory; |
| 15 | +import org.springframework.integration.support.MessageBuilder; |
| 16 | +import org.springframework.messaging.Message; |
| 17 | +import org.springframework.messaging.support.ChannelInterceptor; |
| 18 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| 19 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 20 | +import org.springframework.security.core.context.SecurityContext; |
| 21 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 22 | + |
| 23 | +@Configuration |
| 24 | +@EnableIntegration |
| 25 | +public class SecurityPubSubChannel { |
| 26 | + |
| 27 | + @Bean(name = "startPSChannel") |
| 28 | + @SecuredChannel(interceptor = "channelSecurityInterceptor", sendAccess = "ROLE_VIEWER") |
| 29 | + public PublishSubscribeChannel startChannel() { |
| 30 | + return new PublishSubscribeChannel(executor()); |
| 31 | + } |
| 32 | + |
| 33 | + @ServiceActivator(inputChannel = "startPSChannel", outputChannel = "finalPSResult") |
| 34 | + @PreAuthorize("hasRole('ROLE_LOGGER')") |
| 35 | + public Message<?> changeMessageToRole(Message<?> message) { |
| 36 | + return buildNewMessage(getRoles(), message); |
| 37 | + } |
| 38 | + |
| 39 | + @ServiceActivator(inputChannel = "startPSChannel", outputChannel = "finalPSResult") |
| 40 | + @PreAuthorize("hasRole('ROLE_VIEWER')") |
| 41 | + public Message<?> changeMessageToUserName(Message<?> message) { |
| 42 | + return buildNewMessage(getUsername(), message); |
| 43 | + } |
| 44 | + |
| 45 | + @Bean(name = "finalPSResult") |
| 46 | + public DirectChannel finalPSResult() { |
| 47 | + return new DirectChannel(); |
| 48 | + } |
| 49 | + |
| 50 | + @Bean |
| 51 | + @GlobalChannelInterceptor(patterns = { "startPSChannel", "endDirectChannel" }) |
| 52 | + public ChannelInterceptor securityContextPropagationInterceptor() { |
| 53 | + return new SecurityContextPropagationChannelInterceptor(); |
| 54 | + } |
| 55 | + |
| 56 | + @Bean |
| 57 | + public ThreadPoolTaskExecutor executor() { |
| 58 | + ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor(); |
| 59 | + pool.setCorePoolSize(10); |
| 60 | + pool.setMaxPoolSize(10); |
| 61 | + pool.setWaitForTasksToCompleteOnShutdown(true); |
| 62 | + return pool; |
| 63 | + } |
| 64 | + |
| 65 | + public String getRoles() { |
| 66 | + SecurityContext securityContext = SecurityContextHolder.getContext(); |
| 67 | + return securityContext.getAuthentication().getAuthorities().stream().map(auth -> auth.getAuthority()).collect(Collectors.joining(",")); |
| 68 | + } |
| 69 | + |
| 70 | + public String getUsername() { |
| 71 | + SecurityContext securityContext = SecurityContextHolder.getContext(); |
| 72 | + return securityContext.getAuthentication().getName(); |
| 73 | + } |
| 74 | + |
| 75 | + public Message<String> buildNewMessage(String content, Message<?> message) { |
| 76 | + DefaultMessageBuilderFactory builderFactory = new DefaultMessageBuilderFactory(); |
| 77 | + MessageBuilder<String> messageBuilder = builderFactory.withPayload(content); |
| 78 | + messageBuilder.copyHeaders(message.getHeaders()); |
| 79 | + return messageBuilder.build(); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments