下面列出了org.springframework.security.core.Authentication#setAuthenticated ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
@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);
}
@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);
}
}
@Override
public Authentication authenticate(Authentication authentication) {
authentication.setAuthenticated(true);
return authentication;
}