Skip to content

Commit 56316fd

Browse files
linhvovnmaibin
authored andcommitted
[tlinh2110-BAEL1382] Add Security in SI (eugenp#3593)
* [tlinh2110-BAEL1382] Add Security in SI * [tlinh2110-BAEL1382] Upgrade to Spring 5 & add Logger
1 parent 372cba1 commit 56316fd

8 files changed

Lines changed: 452 additions & 6 deletions

File tree

‎spring-integration/pom.xml‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<properties>
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21-
<spring.integration.version>4.3.5.RELEASE</spring.integration.version>
21+
<spring.version>5.0.1.RELEASE</spring.version>
2222
<spring-social.version>1.1.4.RELEASE</spring-social.version>
2323
<javax-mail.version>1.4.7</javax-mail.version>
2424
<javax-activation.version>1.1.1</javax-activation.version>
@@ -68,7 +68,7 @@
6868
<dependency>
6969
<groupId>org.springframework.integration</groupId>
7070
<artifactId>spring-integration-core</artifactId>
71-
<version>${spring.integration.version}</version>
71+
<version>${spring.version}</version>
7272
</dependency>
7373
<dependency>
7474
<groupId>javax.activation</groupId>
@@ -84,17 +84,17 @@
8484
<dependency>
8585
<groupId>org.springframework.integration</groupId>
8686
<artifactId>spring-integration-twitter</artifactId>
87-
<version>${spring.integration.version}</version>
87+
<version>${spring.version}</version>
8888
</dependency>
8989
<dependency>
9090
<groupId>org.springframework.integration</groupId>
9191
<artifactId>spring-integration-mail</artifactId>
92-
<version>${spring.integration.version}</version>
92+
<version>${spring.version}</version>
9393
</dependency>
9494
<dependency>
9595
<groupId>org.springframework.integration</groupId>
9696
<artifactId>spring-integration-ftp</artifactId>
97-
<version>${spring.integration.version}</version>
97+
<version>${spring.version}</version>
9898
</dependency>
9999
<dependency>
100100
<groupId>org.springframework.social</groupId>
@@ -104,7 +104,36 @@
104104
<dependency>
105105
<groupId>org.springframework.integration</groupId>
106106
<artifactId>spring-integration-file</artifactId>
107-
<version>${spring.integration.version}</version>
107+
<version>${spring.version}</version>
108+
</dependency>
109+
110+
<dependency>
111+
<groupId>org.springframework.security</groupId>
112+
<artifactId>spring-security-core</artifactId>
113+
<version>${spring.version}</version>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.springframework.security</groupId>
117+
<artifactId>spring-security-config</artifactId>
118+
<version>${spring.version}</version>
119+
</dependency>
120+
<dependency>
121+
<groupId>org.springframework.integration</groupId>
122+
<artifactId>spring-integration-security</artifactId>
123+
<version>${spring.version}</version>
124+
</dependency>
125+
126+
<dependency>
127+
<groupId>org.springframework.security</groupId>
128+
<artifactId>spring-security-test</artifactId>
129+
<version>${spring.version}</version>
130+
<scope>test</scope>
131+
</dependency>
132+
<dependency>
133+
<groupId>org.springframework</groupId>
134+
<artifactId>spring-test</artifactId>
135+
<version>${spring.version}</version>
136+
<scope>test</scope>
108137
</dependency>
109138
<dependency>
110139
<groupId>junit</groupId>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.si.security;
2+
3+
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
import java.util.logging.Logger;
6+
7+
import org.springframework.integration.annotation.ServiceActivator;
8+
import org.springframework.messaging.Message;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public class MessageConsumer {
13+
14+
private String messageContent;
15+
16+
private Map<String, String> messagePSContent = new ConcurrentHashMap<>();
17+
18+
public String getMessageContent() {
19+
return messageContent;
20+
}
21+
22+
public void setMessageContent(String messageContent) {
23+
this.messageContent = messageContent;
24+
}
25+
26+
public Map<String, String> getMessagePSContent() {
27+
return messagePSContent;
28+
}
29+
30+
public void setMessagePSContent(Map<String, String> messagePSContent) {
31+
this.messagePSContent = messagePSContent;
32+
}
33+
34+
@ServiceActivator(inputChannel = "endDirectChannel")
35+
public void endDirectFlow(Message<?> message) {
36+
setMessageContent(message.getPayload().toString());
37+
}
38+
39+
@ServiceActivator(inputChannel = "finalPSResult")
40+
public void endPSFlow(Message<?> message) {
41+
Logger.getAnonymousLogger().info(Thread.currentThread().getName() + " has completed ---------------------------");
42+
messagePSContent.put(Thread.currentThread().getName(), (String) message.getPayload());
43+
}
44+
45+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.si.security;
2+
3+
import java.util.logging.Logger;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.integration.annotation.ServiceActivator;
9+
import org.springframework.integration.channel.DirectChannel;
10+
import org.springframework.integration.config.EnableIntegration;
11+
import org.springframework.integration.security.channel.ChannelSecurityInterceptor;
12+
import org.springframework.integration.security.channel.SecuredChannel;
13+
import org.springframework.messaging.Message;
14+
import org.springframework.security.access.AccessDecisionManager;
15+
import org.springframework.security.access.prepost.PreAuthorize;
16+
import org.springframework.security.authentication.AuthenticationManager;
17+
18+
@Configuration
19+
@EnableIntegration
20+
public class SecuredDirectChannel {
21+
22+
@Bean(name = "startDirectChannel")
23+
@SecuredChannel(interceptor = "channelSecurityInterceptor", sendAccess = { "ROLE_VIEWER", "jane" })
24+
public DirectChannel startDirectChannel() {
25+
return new DirectChannel();
26+
}
27+
28+
@ServiceActivator(inputChannel = "startDirectChannel", outputChannel = "endDirectChannel")
29+
@PreAuthorize("hasRole('ROLE_LOGGER')")
30+
public Message<?> logMessage(Message<?> message) {
31+
Logger.getAnonymousLogger().info(message.toString());
32+
return message;
33+
}
34+
35+
@Bean(name = "endDirectChannel")
36+
@SecuredChannel(interceptor = "channelSecurityInterceptor", sendAccess = { "ROLE_EDITOR" })
37+
public DirectChannel endDirectChannel() {
38+
return new DirectChannel();
39+
}
40+
41+
@Autowired
42+
@Bean
43+
public ChannelSecurityInterceptor channelSecurityInterceptor(AuthenticationManager authenticationManager, AccessDecisionManager customAccessDecisionManager) {
44+
ChannelSecurityInterceptor channelSecurityInterceptor = new ChannelSecurityInterceptor();
45+
channelSecurityInterceptor.setAuthenticationManager(authenticationManager);
46+
channelSecurityInterceptor.setAccessDecisionManager(customAccessDecisionManager);
47+
return channelSecurityInterceptor;
48+
}
49+
50+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.si.security;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.integration.security.channel.ChannelSecurityInterceptor;
10+
import org.springframework.security.access.AccessDecisionManager;
11+
import org.springframework.security.access.AccessDecisionVoter;
12+
import org.springframework.security.access.vote.AffirmativeBased;
13+
import org.springframework.security.access.vote.RoleVoter;
14+
import org.springframework.security.authentication.AuthenticationManager;
15+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
16+
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
17+
18+
@Configuration
19+
@EnableGlobalMethodSecurity(prePostEnabled = true)
20+
public class SecurityConfig extends GlobalMethodSecurityConfiguration {
21+
22+
@Override
23+
@Bean
24+
public AuthenticationManager authenticationManager() throws Exception {
25+
return super.authenticationManager();
26+
}
27+
28+
@Bean
29+
public AccessDecisionManager customAccessDecisionManager() {
30+
List<AccessDecisionVoter<? extends Object>> decisionVoters = new ArrayList<>();
31+
decisionVoters.add(new RoleVoter());
32+
decisionVoters.add(new UsernameAccessDecisionVoter());
33+
AccessDecisionManager accessDecisionManager = new AffirmativeBased(decisionVoters);
34+
return accessDecisionManager;
35+
}
36+
37+
@Autowired
38+
@Bean
39+
public ChannelSecurityInterceptor channelSecurityInterceptor(AuthenticationManager authenticationManager, AccessDecisionManager customAccessDecisionManager) {
40+
ChannelSecurityInterceptor channelSecurityInterceptor = new ChannelSecurityInterceptor();
41+
channelSecurityInterceptor.setAuthenticationManager(authenticationManager);
42+
channelSecurityInterceptor.setAccessDecisionManager(customAccessDecisionManager);
43+
return channelSecurityInterceptor;
44+
}
45+
46+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.si.security;
2+
3+
import java.util.Collection;
4+
5+
import org.springframework.security.access.AccessDecisionVoter;
6+
import org.springframework.security.access.ConfigAttribute;
7+
import org.springframework.security.core.Authentication;
8+
9+
public class UsernameAccessDecisionVoter implements AccessDecisionVoter<Object> {
10+
private String rolePrefix = "ROLE_";
11+
12+
@Override
13+
public boolean supports(ConfigAttribute attribute) {
14+
if ((attribute.getAttribute() != null)
15+
&& !attribute.getAttribute().startsWith(rolePrefix)) {
16+
return true;
17+
}else {
18+
return false;
19+
}
20+
}
21+
22+
@Override
23+
public boolean supports(Class<?> clazz) {
24+
return true;
25+
}
26+
27+
@Override
28+
public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
29+
if (authentication == null) {
30+
return ACCESS_DENIED;
31+
}
32+
String name = authentication.getName();
33+
int result = ACCESS_ABSTAIN;
34+
for (ConfigAttribute attribute : attributes) {
35+
if (this.supports(attribute)) {
36+
result = ACCESS_DENIED;
37+
if (attribute.getAttribute().equals(name)) {
38+
return ACCESS_GRANTED;
39+
}
40+
}
41+
}
42+
return result;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)