Skip to content

Commit 2d4074c

Browse files
Christoffer-Cortesvasile-baluta
authored andcommitted
Add LDAP authorization and MongoDB session storage (#86)
Add LDAP authorization to http requests
1 parent 5e7bc2d commit 2d4074c

32 files changed

+365
-170
lines changed

pom.xml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<groupId>org.springframework.boot</groupId>
1212
<artifactId>spring-boot-starter-parent</artifactId>
13-
<version>1.5.5.RELEASE</version>
13+
<version>2.0.1.RELEASE</version>
1414
<relativePath/> <!-- .. lookup parent from repository -->
1515
</parent>
1616

@@ -83,11 +83,31 @@
8383
<artifactId>spring-boot-starter-tomcat</artifactId>
8484
<scope>compile</scope>
8585
</dependency>
86+
87+
<dependency>
88+
<groupId>org.springframework.ldap</groupId>
89+
<artifactId>spring-ldap-core</artifactId>
90+
</dependency>
91+
92+
<dependency>
93+
<groupId>org.springframework.security</groupId>
94+
<artifactId>spring-security-ldap</artifactId>
95+
</dependency>
96+
97+
<dependency>
98+
<groupId>org.springframework.session</groupId>
99+
<artifactId>spring-session-data-mongodb</artifactId>
100+
</dependency>
101+
102+
<dependency>
103+
<groupId>org.springframework.boot</groupId>
104+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
105+
</dependency>
86106

87107
<dependency>
88108
<groupId>com.fasterxml.jackson.core</groupId>
89109
<artifactId>jackson-databind</artifactId>
90-
<version>2.8.9</version>
110+
<version>2.9.4</version>
91111
</dependency>
92112

93113
<dependency>
@@ -202,8 +222,8 @@
202222
<groupId>org.springframework.boot</groupId>
203223
<artifactId>spring-boot-maven-plugin</artifactId>
204224
</plugin>
205-
206-
<!-- PhoenixNAP RAML Code Generator plugin used to generate sources
225+
226+
<!-- PhoenixNAP RAML Code Generator plugin used to generate sources
207227
from raml -->
208228
<plugin>
209229
<groupId>com.phoenixnap.oss</groupId>
@@ -254,8 +274,8 @@
254274
</configuration>
255275
</execution>
256276
</executions>
257-
</plugin>
258-
277+
</plugin>
278+
259279
<plugin>
260280
<groupId>org.apache.maven.plugins</groupId>
261281
<artifactId>maven-surefire-plugin</artifactId>

src/main/java/com/ericsson/ei/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.springframework.boot.SpringApplication;
2727
import org.springframework.boot.autoconfigure.SpringBootApplication;
2828
import org.springframework.boot.builder.SpringApplicationBuilder;
29-
import org.springframework.boot.web.support.SpringBootServletInitializer;
29+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
3030
import org.springframework.context.support.SimpleThreadScope;
3131
import org.springframework.scheduling.annotation.EnableAsync;
3232
import org.springframework.scheduling.annotation.EnableScheduling;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright 2017 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package com.ericsson.ei;
19+
20+
import org.apache.tomcat.util.codec.binary.Base64;
21+
import org.apache.tomcat.util.codec.binary.StringUtils;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import org.springframework.beans.factory.annotation.Value;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
27+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
28+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
29+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
30+
import org.springframework.security.config.http.SessionCreationPolicy;
31+
32+
@Configuration
33+
@EnableWebSecurity
34+
public class EndpointSecurity extends WebSecurityConfigurerAdapter {
35+
36+
private static Logger LOGGER = (Logger) LoggerFactory.getLogger(EndpointSecurity.class);
37+
38+
@Value("${ldap.enabled}")
39+
private boolean ldapEnabled;
40+
41+
@Value("${ldap.url}")
42+
private String ldapUrl;
43+
44+
@Value("${ldap.base.dn}")
45+
private String ldapBaseDn;
46+
47+
@Value("${ldap.username}")
48+
private String ldapUsername;
49+
50+
@Value("${ldap.password}")
51+
private String ldapPassword;
52+
53+
@Value("${ldap.user.filter}")
54+
private String ldapUserFilter;
55+
56+
@Override
57+
protected void configure(HttpSecurity http) throws Exception {
58+
if(ldapEnabled) {
59+
LOGGER.info("LDAP security configuration is enabled");
60+
http
61+
.authorizeRequests()
62+
.anyRequest().authenticated()
63+
.and()
64+
.sessionManagement()
65+
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
66+
.and()
67+
.httpBasic()
68+
.and()
69+
.csrf().disable();
70+
}
71+
else {
72+
LOGGER.info("LDAP security configuration is disabled");
73+
http
74+
.csrf().disable();
75+
}
76+
}
77+
78+
@Override
79+
public void configure(AuthenticationManagerBuilder auth) throws Exception {
80+
if(ldapEnabled) {
81+
auth
82+
.eraseCredentials(false)
83+
.ldapAuthentication()
84+
.userSearchFilter(ldapUserFilter)
85+
.contextSource()
86+
.url(ldapUrl)
87+
.root(ldapBaseDn)
88+
.managerDn(ldapUsername)
89+
.managerPassword(decodeBase64(ldapPassword));
90+
}
91+
}
92+
93+
private String decodeBase64(String password) {
94+
return StringUtils.newStringUtf8(Base64.decodeBase64(password));
95+
}
96+
}

src/main/java/com/ericsson/ei/EnpointSecurity.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.ericsson.ei.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Primary;
6+
import org.springframework.data.mongodb.core.MongoOperations;
7+
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
8+
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
9+
10+
@EnableMongoHttpSession(collectionName="sessions")
11+
public class HttpSessionConfig {
12+
13+
@Value("${server.session-timeout}")
14+
private int maxInactiveIntervalInSeconds;
15+
16+
@Primary
17+
@Bean
18+
public MongoOperationsSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
19+
MongoOperationsSessionRepository repository = new MongoOperationsSessionRepository(mongoOperations);
20+
repository.setMaxInactiveIntervalInSeconds(maxInactiveIntervalInSeconds);
21+
return repository;
22+
}
23+
}

src/main/java/com/ericsson/ei/handlers/EventToObjectMapHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class EventToObjectMapHandler {
4646
static Logger log = (Logger) LoggerFactory.getLogger(ExtractionHandler.class);
4747

4848
@Value("${event_object_map.collection.name}") private String collectionName;
49-
@Value("${database.name}") private String databaseName;
49+
@Value("${spring.data.mongodb.database}") private String databaseName;
5050

5151
private final String listPropertyName = "objects";
5252

src/main/java/com/ericsson/ei/handlers/ObjectHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public class ObjectHandler {
4848
@Value("${aggregated.collection.name}")
4949
private String collectionName;
5050

51-
@Getter
52-
@Setter
53-
@Value("${database.name}")
51+
52+
@Getter @Setter
53+
@Value("${spring.data.mongodb.database}")
5454
private String databaseName;
5555

5656
@Setter

src/main/java/com/ericsson/ei/mongodbhandler/MongoDBHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,20 @@
4949
@Component
5050
public class MongoDBHandler {
5151
static Logger log = (Logger) LoggerFactory.getLogger(MongoDBHandler.class);
52-
52+
53+
@Getter
5354
MongoClient mongoClient;
5455

5556
public void setMongoClient(MongoClient mongoClient) {
5657
this.mongoClient = mongoClient;
5758
}
5859

5960
@Getter
60-
@Value("${mongodb.host}")
61+
@Value("${spring.data.mongodb.host}")
6162
private String host;
6263

6364
@Getter
64-
@Value("${mongodb.port}")
65+
@Value("${spring.data.mongodb.port}")
6566
private int port;
6667

6768
// TODO establish connection automatically when Spring instantiate this

src/main/java/com/ericsson/ei/queryservice/ProcessAggregatedObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class ProcessAggregatedObject {
4343
@Value("${aggregated.collection.name}")
4444
private String aggregationCollectionName;
4545

46-
@Value("${database.name}")
46+
@Value("${spring.data.mongodb.database}")
4747
private String aggregationDataBaseName;
4848

4949
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(ProcessAggregatedObject.class);

src/main/java/com/ericsson/ei/queryservice/ProcessQueryParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ProcessQueryParams {
4242
@Value("${aggregated.collection.name}")
4343
private String aggregationCollectionName;
4444

45-
@Value("${database.name}")
45+
@Value("${spring.data.mongodb.database}")
4646
private String dataBaseName;
4747

4848
@Value("${missedNotificationCollectionName}")

0 commit comments

Comments
 (0)