Java Code Examples for org.springframework.security.core.Authentication#setAuthenticated()

The following examples show how to use org.springframework.security.core.Authentication#setAuthenticated() . 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 File: AuthenticationTestAction.java    From blackduck-alert with Apache License 2.0 6 votes vote down vote up
private void performLdapTest(FieldModel fieldModel, FieldAccessor registeredFieldValues) throws IntegrationException {
    logger.info("LDAP enabled testing LDAP authentication.");
    String userName = fieldModel.getFieldValue(AuthenticationUIConfig.TEST_FIELD_KEY_USERNAME).orElse("");
    Optional<LdapAuthenticationProvider> ldapProvider = ldapManager.createAuthProvider(registeredFieldValues);
    String errorMessage = String.format("Ldap Authentication test failed for the test user %s.  Please check the LDAP configuration.", userName);
    Map<String, String> errorsMap = new HashMap<>();
    if (!ldapProvider.isPresent()) {
        errorsMap.put(AuthenticationDescriptor.KEY_LDAP_ENABLED, errorMessage);
    } else {
        Authentication pendingAuthentication = new UsernamePasswordAuthenticationToken(userName,
            fieldModel.getFieldValue(AuthenticationUIConfig.TEST_FIELD_KEY_PASSWORD).orElse(""));
        Authentication authentication = ldapProvider.get().authenticate(pendingAuthentication);
        if (!authentication.isAuthenticated()) {
            errorsMap.put(AuthenticationDescriptor.KEY_LDAP_ENABLED, errorMessage);
        }
        authentication.setAuthenticated(false);
    }

    if (!errorsMap.isEmpty()) {
        throw new AlertFieldException(errorsMap);
    }
}
 
Example 2
Source File: FeedbackControllerTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@BeforeEach
void beforeMethod() {
  reset(mailSender, appSettings, userService, reCaptchaService);
  when(appSettings.getTitle()).thenReturn("app123");
  mockMvcFeedback =
      MockMvcBuilders.standaloneSetup(feedbackController)
          .setMessageConverters(gsonHttpMessageConverter)
          .build();
  Authentication authentication = new TestingAuthenticationToken("userName", null);
  authentication.setAuthenticated(true);

  previousContext = SecurityContextHolder.getContext();
  SecurityContext testContext = SecurityContextHolder.createEmptyContext();
  testContext.setAuthentication(authentication);
  SecurityContextHolder.setContext(testContext);
}
 
Example 3
Source File: AbstractJWTFilter.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
  if (StringUtils.isEmpty(getProvidedUrl())) {
    throw new BadCredentialsException("Authentication provider URL must not be null or empty.");
  }
  if (StringUtils.isEmpty(getPublicKey())) {
    throw new BadCredentialsException("Public key for signature validation must be provisioned.");
  }

  try {
    Claims claims = Jwts
      .parser()
      .setSigningKey(parseRSAPublicKey(getPublicKey()))
      .parseClaimsJws(getJWTFromCookie(request))
      .getBody();
    String userName  = claims.getSubject();
    logger.info("USERNAME: " + userName);
    logger.info("URL = " + request.getRequestURL());
    if (StringUtils.isNotEmpty(claims.getAudience()) && !getAudiences().contains(claims.getAudience())) {
      throw new IllegalArgumentException(String.format("Audience validation failed. (Not found: %s)", claims.getAudience()));
    }
    Authentication authentication = new JWTAuthenticationToken(userName, getPublicKey(), getAuthorities(userName));
    authentication.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return authentication;
  } catch (ExpiredJwtException | MalformedJwtException | SignatureException | IllegalArgumentException e) {
    logger.info("URL = " + request.getRequestURL());
    logger.warn("Error during JWT authentication: {}", e.getMessage());
    throw new BadCredentialsException(e.getMessage(), e);
  }
}
 
Example 4
Source File: CertificateAuthenticationProvider.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Authentication authenticate(Authentication authentication) {
    authentication.setAuthenticated(true);
    return authentication;
}