Java Code Examples for org.springframework.security.web.authentication.WebAuthenticationDetails

The following examples show how to use org.springframework.security.web.authentication.WebAuthenticationDetails. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: data-highway   Source File: OnrampAuthorisation.java    License: Apache License 2.0 6 votes vote down vote up
private AuthorisationOutcome isCidrAuthorised(
    Authentication authentication,
    String roadName,
    Optional<Onramp> onramp) {
  List<String> cidrBlocks = onramp.map(Onramp::getCidrBlocks).orElse(emptyList());
  WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
  boolean authorised = cidrBlockAuthorisation.isAuthorised(cidrBlocks, details.getRemoteAddress());

  log.debug("CIDR Authorisation for road: {}, authorised: {}, remoteAddress: {}", roadName, authorised, details.getRemoteAddress());
  if (!authorised && log.isWarnEnabled()) {
    Instant now = clock.instant();
    if (now.isAfter(lastWarn.plusSeconds(1))) {
      lastWarn = now;
      log.warn("CIDR Authorisation failed for road: {}, remoteAddress: {}", roadName, details.getRemoteAddress());
    }
  }

  return authorised ? AUTHORISED : UNAUTHORISED;
}
 
Example 2
Source Project: tutorials   Source File: AuditEventConverter.java    License: MIT License 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            Object object = entry.getValue();

            // Extract the data that will be saved.
            if (object instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) object;
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else if (object != null) {
                results.put(entry.getKey(), object.toString());
            } else {
                results.put(entry.getKey(), "null");
            }
        }
    }

    return results;
}
 
Example 3
Source Project: spring-microservice-exam   Source File: SysUtil.java    License: MIT License 6 votes vote down vote up
/**
 * 获取当前登录的租户code
 *
 * @return String
 */
private static String getCurrentUserTenantCode() {
    String tenantCode = "";
    try {
        ResourceServerTokenServices resourceServerTokenServices = SpringContextHolder.getApplicationContext().getBean(ResourceServerTokenServices.class);
        Object details = SecurityContextHolder.getContext().getAuthentication().getDetails();
        if (details instanceof OAuth2AuthenticationDetails) {
            OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails) details;
            OAuth2AccessToken oAuth2AccessToken = resourceServerTokenServices.readAccessToken(oAuth2AuthenticationDetails.getTokenValue());
            Object tenantObj = oAuth2AccessToken.getAdditionalInformation().get(SecurityConstant.TENANT_CODE);
            tenantCode = tenantObj == null ? "" : tenantObj.toString();
        } else if (details instanceof WebAuthenticationDetails) {
            // 未认证
            Object requestObj = RequestContextHolder.getRequestAttributes();
            if (requestObj != null) {
                HttpServletRequest request = ((ServletRequestAttributes) requestObj).getRequest();
                tenantCode = request.getParameter(SecurityConstant.TENANT_CODE);
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return tenantCode;
}
 
Example 4
Source Project: expper   Source File: AuditEventConverter.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (String key : data.keySet()) {
            Object object = data.get(key);

            // Extract the data that will be saved.
            if (object instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) object;
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else if (object != null) {
                results.put(key, object.toString());
            } else {
                results.put(key, "null");
            }
        }
    }

    return results;
}
 
Example 5
Source Project: flair-engine   Source File: AuditEventConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            Object object = entry.getValue();

            // Extract the data that will be saved.
            if (object instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) object;
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else if (object != null) {
                results.put(entry.getKey(), object.toString());
            } else {
                results.put(entry.getKey(), "null");
            }
        }
    }

    return results;
}
 
Example 6
Source Project: alchemy   Source File: AuditEventConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string.
 *
 * @param data the data to convert.
 * @return a map of {@link String}, {@link String}.
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 7
Source Project: metron   Source File: KnoxSSOAuthenticationFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Builds the Spring Authentication object using the supplied user name and groups looked up from LDAP.  Groups are currently
 * mapped directly to Spring roles by converting to upper case and prepending the name with "ROLE_".
 * @param userName The username to build the Authentication object with.
 * @param httpRequest HttpServletRequest
 * @return Authentication object for the given user.
 */
protected Authentication getAuthentication(String userName, HttpServletRequest httpRequest) {
  String ldapName = LdapNameBuilder.newInstance().add(userSearchBase).add("uid", userName).build().toString();

  // Search ldap for a user's groups and convert to a Spring role
  List<GrantedAuthority> grantedAuths = ldapTemplate.search(query()
          .where("objectclass")
          .is("groupOfNames")
          .and("member")
          .is(ldapName), (AttributesMapper<String>) attrs -> (String) attrs.get("cn").get())
          .stream()
          .map(group -> String.format("%s%s", SECURITY_ROLE_PREFIX, group.toUpperCase()))
          .map(SimpleGrantedAuthority::new).collect(Collectors.toList());

  final UserDetails principal = new User(userName, "", grantedAuths);
  final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
          principal, "", grantedAuths);
  WebAuthenticationDetails webDetails = new WebAuthenticationDetails(httpRequest);
  authentication.setDetails(webDetails);
  return authentication;
}
 
Example 8
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string.
 *
 * @param data the data to convert.
 * @return a map of {@link String}, {@link String}.
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 9
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 10
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 11
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string.
 *
 * @param data the data to convert.
 * @return a map of {@link String}, {@link String}.
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 12
Source Project: cubeai   Source File: AuditEventConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            Object object = entry.getValue();

            // Extract the data that will be saved.
            if (object instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) object;
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else if (object != null) {
                results.put(entry.getKey(), object.toString());
            } else {
                results.put(entry.getKey(), "null");
            }
        }
    }

    return results;
}
 
Example 13
Source Project: cubeai   Source File: CustomAuditEventRepositoryIntTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 14
Source Project: cubeai   Source File: AuditEventConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            Object object = entry.getValue();

            // Extract the data that will be saved.
            if (object instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) object;
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else if (object != null) {
                results.put(entry.getKey(), object.toString());
            } else {
                results.put(entry.getKey(), "null");
            }
        }
    }

    return results;
}
 
Example 15
Source Project: tutorials   Source File: CustomAuditEventRepositoryIntTest.java    License: MIT License 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 16
Source Project: tutorials   Source File: AuditEventConverter.java    License: MIT License 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 17
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 18
/**
 * Get current user's IP address.
 *
 * @return IP
 */
public static String getCurrentUserIp() {
  Authentication authentication = getAuthentication();
  if (authentication == null) {
    return "";
  }
  Object details = authentication.getDetails();
  if (details instanceof OAuth2AuthenticationDetails) {
    OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails) details;
    return oAuth2AuthenticationDetails.getRemoteAddress();
  }
  if (details instanceof WebAuthenticationDetails) {
    WebAuthenticationDetails webDetails = (WebAuthenticationDetails) details;
    return webDetails.getRemoteAddress();
  }
  return "";
}
 
Example 19
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 20
Source Project: tutorials   Source File: AuditEventConverter.java    License: MIT License 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 21
@Override
public String extractKey(OAuth2Authentication authentication) {
    Map<String, String> values = new LinkedHashMap<>();
    OAuth2Request authorizationRequest = authentication.getOAuth2Request();
    if (!authentication.isClientOnly()) {
        values.put(USERNAME, authentication.getName());
    }
    values.put(CLIENT_ID, authorizationRequest.getClientId());
    if (authorizationRequest.getScope() != null) {
        values.put(SCOPE, OAuth2Utils.formatParameterList(new TreeSet<>(authorizationRequest.getScope())));
    }
    Authentication auth = authentication.getUserAuthentication();
    if (auth != null && auth.getDetails() instanceof WebAuthenticationDetails) {
        String sessionId = ((WebAuthenticationDetails) auth.getDetails()).getSessionId();
        logger.info("sessionId : {}", sessionId);
        if (!StringUtils.isEmpty(sessionId)) {
            values.put(SESSION, sessionId);
        }
    }
    return generateKey(values);
}
 
Example 22
Source Project: tutorials   Source File: AuditEventConverter.java    License: MIT License 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            Object object = entry.getValue();

            // Extract the data that will be saved.
            if (object instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) object;
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else if (object != null) {
                results.put(entry.getKey(), object.toString());
            } else {
                results.put(entry.getKey(), "null");
            }
        }
    }

    return results;
}
 
Example 23
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 24
Source Project: lemon   Source File: SpringSecurityUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 取得当前用户登录IP, 如果当前用户未登录则返回空字符串.
 * 
 * @return String
 */
public static String getCurrentUserIp() {
    Authentication authentication = getAuthentication();

    if (authentication == null) {
        return "";
    }

    Object details = authentication.getDetails();

    if (!(details instanceof WebAuthenticationDetails)) {
        return "";
    }

    WebAuthenticationDetails webDetails = (WebAuthenticationDetails) details;

    return webDetails.getRemoteAddress();
}
 
Example 25
Source Project: Spring-5.0-Projects   Source File: AuditEventConverter.java    License: MIT License 6 votes vote down vote up
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 26
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 27
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 28
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}
 
Example 29
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 30
/**
 * Internal conversion. This method will allow to save additional data.
 * By default, it will save the object as string
 *
 * @param data the data to convert
 * @return a map of String, String
 */
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
    Map<String, String> results = new HashMap<>();

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            // Extract the data that will be saved.
            if (entry.getValue() instanceof WebAuthenticationDetails) {
                WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) entry.getValue();
                results.put("remoteAddress", authenticationDetails.getRemoteAddress());
                results.put("sessionId", authenticationDetails.getSessionId());
            } else {
                results.put(entry.getKey(), Objects.toString(entry.getValue()));
            }
        }
    }
    return results;
}