Skip to content

Commit 89e96e9

Browse files
author
Vishnu Alapati
committed
Worked in review comments
1 parent ca7a085 commit 89e96e9

File tree

3 files changed

+28
-32
lines changed

3 files changed

+28
-32
lines changed

publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/config/CustomAuthenticationEntryPoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint
2424
public void commence(HttpServletRequest request, HttpServletResponse response,
2525
AuthenticationException authException) throws IOException, ServletException {
2626
if (authException instanceof BadCredentialsException) {
27-
LOGGER.info("Bad Credentials {}", HttpStatus.UNAUTHORIZED);
27+
LOGGER.warn("Bad Credentials {}", HttpStatus.UNAUTHORIZED);
2828
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid credentials");
2929
}
3030
}

publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/config/SecurityConfig.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,21 @@
1414
*/
1515
package com.ericsson.eiffel.remrem.publish.config;
1616

17-
import java.io.IOException;
1817
import java.util.HashMap;
1918

20-
import javax.servlet.ServletException;
21-
import javax.servlet.http.HttpServletRequest;
22-
import javax.servlet.http.HttpServletResponse;
23-
2419
import org.slf4j.Logger;
2520
import org.slf4j.LoggerFactory;
2621
import org.springframework.beans.factory.annotation.Autowired;
2722
import org.springframework.beans.factory.annotation.Value;
2823
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
29-
import org.springframework.context.annotation.Bean;
3024
import org.springframework.context.annotation.Configuration;
3125
import org.springframework.context.annotation.Profile;
32-
import org.springframework.http.HttpStatus;
3326
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
3427
import org.springframework.ldap.core.support.LdapContextSource;
35-
import org.springframework.security.authentication.BadCredentialsException;
36-
import org.springframework.security.authentication.DisabledException;
37-
import org.springframework.security.authentication.LockedException;
3828
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
3929
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
4030
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
4131
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
42-
import org.springframework.security.core.Authentication;
43-
import org.springframework.security.core.AuthenticationException;
44-
import org.springframework.security.core.context.SecurityContextHolder;
45-
import org.springframework.security.core.userdetails.UserDetails;
46-
import org.springframework.security.web.AuthenticationEntryPoint;
47-
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
48-
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
49-
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
50-
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
5132

5233
/**
5334
* This class is used to enable the ldap authentication based on property

publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
import java.util.EnumSet;
1818
import java.util.Map;
19+
1920
import org.slf4j.LoggerFactory;
2021
import org.springframework.beans.factory.annotation.Autowired;
2122
import org.springframework.beans.factory.annotation.Qualifier;
23+
import org.springframework.beans.factory.annotation.Value;
2224
import org.springframework.context.annotation.ComponentScan;
2325
import org.springframework.http.HttpEntity;
2426
import org.springframework.http.HttpHeaders;
2527
import org.springframework.http.HttpStatus;
2628
import org.springframework.http.MediaType;
2729
import org.springframework.http.ResponseEntity;
28-
import org.springframework.security.authentication.BadCredentialsException;
2930
import org.springframework.security.core.Authentication;
3031
import org.springframework.security.core.context.SecurityContextHolder;
3132
import org.springframework.security.core.userdetails.UserDetails;
@@ -39,13 +40,13 @@
3940
import org.springframework.web.client.RestTemplate;
4041

4142
import com.ericsson.eiffel.remrem.protocol.MsgService;
43+
import com.ericsson.eiffel.remrem.publish.exception.RemRemPublishException;
4244
import com.ericsson.eiffel.remrem.publish.helper.PublishUtils;
4345
import com.ericsson.eiffel.remrem.publish.helper.RMQHelper;
4446
import com.ericsson.eiffel.remrem.publish.service.EventTemplateHandler;
47+
import com.ericsson.eiffel.remrem.publish.service.GenerateURLTemplate;
4548
import com.ericsson.eiffel.remrem.publish.service.MessageService;
4649
import com.ericsson.eiffel.remrem.publish.service.SendResult;
47-
import com.ericsson.eiffel.remrem.publish.service.GenerateURLTemplate;
48-
import com.ericsson.eiffel.remrem.publish.exception.RemRemPublishException;
4950
import com.fasterxml.jackson.databind.JsonNode;
5051
import com.google.gson.JsonElement;
5152
import com.google.gson.JsonObject;
@@ -77,6 +78,9 @@ public class ProducerController {
7778
@Autowired
7879
private GenerateURLTemplate generateURLTemplate;
7980

81+
@Value("${activedirectory.publish.enabled}")
82+
private boolean isAuthenticationEnabled;
83+
8084
private RestTemplate restTemplate = new RestTemplate();
8185

8286
private JsonParser parser = new JsonParser();
@@ -91,6 +95,18 @@ public void setRestTemplate(RestTemplate restTemplate) {
9195
this.restTemplate = restTemplate;
9296
}
9397

98+
public void getUserName() {
99+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
100+
// Check if the user is authenticated
101+
if (authentication != null && authentication.isAuthenticated()) {
102+
// Get the UserDetails object, which contains user information
103+
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
104+
// Get the username of the authenticated user
105+
String username = userDetails.getUsername();
106+
log.info("User name {} ", username);
107+
}
108+
}
109+
94110
@SuppressWarnings({ "rawtypes", "unchecked" })
95111
@ApiOperation(value = "To publish eiffel event to message bus", response = String.class)
96112
@ApiResponses(value = { @ApiResponse(code = 200, message = "Event sent successfully"),
@@ -106,20 +122,16 @@ public ResponseEntity send(
106122
@ApiParam(value = "tag") @RequestParam(value = "tag", required = false) final String tag,
107123
@ApiParam(value = "routing key") @RequestParam(value = "rk", required = false) final String routingKey,
108124
@ApiParam(value = "eiffel event", required = true) @RequestBody final JsonElement body) {
125+
if(isAuthenticationEnabled) {
126+
getUserName();
127+
}
128+
109129
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices);
110130
log.debug("mp: " + msgProtocol);
111131
log.debug("body: " + body);
112132
log.debug("user domain suffix: " + userDomain + " tag: " + tag + " Routing Key: "
113133
+ routingKey);
114-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
115-
// Check if the user is authenticated
116-
if (authentication != null && authentication.isAuthenticated()) {
117-
// Get the UserDetails object, which contains user information
118-
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
119-
// Get the username of the authenticated user
120-
String username = userDetails.getUsername();
121-
log.info("Authentication Successful for user {} ", username);
122-
}
134+
123135
if (msgService != null && msgProtocol != null) {
124136
try {
125137
rmqHelper.rabbitMqPropertiesInit(msgProtocol);
@@ -182,6 +194,9 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r
182194
+ "event fields from the input event data that does not validate successfully, "
183195
+ "and add those removed field information into customData/remremGenerateFailures") @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields,
184196
@ApiParam(value = "JSON message", required = true) @RequestBody final JsonObject bodyJson) {
197+
if (isAuthenticationEnabled) {
198+
getUserName();
199+
}
185200

186201
String bodyJsonOut = null;
187202
if(parseData) {

0 commit comments

Comments
 (0)