|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2018 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2022 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -54,6 +54,7 @@ import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
import org.springframework.security.ldap.DefaultLdapUsernameToDnMapper;
|
|
import org.springframework.security.ldap.DefaultLdapUsernameToDnMapper;
|
|
@@ -82,6 +83,9 @@ public class LdapUserDetailsManager implements UserDetailsManager {
|
|
|
|
|
|
private final Log logger = LogFactory.getLog(LdapUserDetailsManager.class);
|
|
private final Log logger = LogFactory.getLog(LdapUserDetailsManager.class);
|
|
|
|
|
|
|
|
+ private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
|
|
|
|
+ .getContextHolderStrategy();
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* The strategy for mapping usernames to LDAP distinguished names. This will be used
|
|
* The strategy for mapping usernames to LDAP distinguished names. This will be used
|
|
* when building DNs for creating new users etc.
|
|
* when building DNs for creating new users etc.
|
|
@@ -179,7 +183,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
public void changePassword(final String oldPassword, final String newPassword) {
|
|
public void changePassword(final String oldPassword, final String newPassword) {
|
|
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
|
|
+ Authentication authentication = this.securityContextHolderStrategy.getContext().getAuthentication();
|
|
Assert.notNull(authentication,
|
|
Assert.notNull(authentication,
|
|
"No authentication object found in security context. Can't change current user's password!");
|
|
"No authentication object found in security context. Can't change current user's password!");
|
|
String username = authentication.getName();
|
|
String username = authentication.getName();
|
|
@@ -388,6 +392,17 @@ public class LdapUserDetailsManager implements UserDetailsManager {
|
|
this.usePasswordModifyExtensionOperation = usePasswordModifyExtensionOperation;
|
|
this.usePasswordModifyExtensionOperation = usePasswordModifyExtensionOperation;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
|
|
|
|
+ * the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
|
|
|
|
+ *
|
|
|
|
+ * @since 5.8
|
|
|
|
+ */
|
|
|
|
+ public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
|
|
|
|
+ Assert.notNull(securityContextHolderStrategy, "securityContextHolderStrategy cannot be null");
|
|
|
|
+ this.securityContextHolderStrategy = securityContextHolderStrategy;
|
|
|
|
+ }
|
|
|
|
+
|
|
private void changePasswordUsingAttributeModification(DistinguishedName userDn, String oldPassword,
|
|
private void changePasswordUsingAttributeModification(DistinguishedName userDn, String oldPassword,
|
|
String newPassword) {
|
|
String newPassword) {
|
|
ModificationItem[] passwordChange = new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
|
|
ModificationItem[] passwordChange = new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
|