DaoAuthenticationProvider uses DelegatingPasswordEncoder

This means that passwords will be encoded with BCrypt by default

Fixes: gh-2775
This commit is contained in:
Rob Winch 2017-10-23 13:35:42 -05:00
parent d19b222b55
commit 8291f20796
40 changed files with 197 additions and 150 deletions

View File

@ -18,7 +18,6 @@ package org.springframework.security.config.annotation.authentication.configurer
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.ObjectPostProcessor; import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.SecurityBuilder; import org.springframework.security.config.annotation.SecurityBuilder;
import org.springframework.security.config.annotation.SecurityConfigurer;
import org.springframework.security.config.annotation.authentication.ProviderManagerBuilder; import org.springframework.security.config.annotation.authentication.ProviderManagerBuilder;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;

View File

@ -35,6 +35,7 @@ import org.springframework.security.config.annotation.configuration.ObjectPostPr
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication import org.springframework.security.core.Authentication
import org.springframework.security.core.userdetails.PasswordEncodedUser
import org.springframework.security.core.userdetails.UserDetailsService import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@ -90,10 +91,10 @@ class AuthenticationManagerBuilderTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and() .withUser(PasswordEncodedUser.user())
.and() .and()
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("admin").password("password").roles("USER","ADMIN") .withUser(PasswordEncodedUser.admin())
} }
} }

View File

@ -25,6 +25,7 @@ import org.springframework.security.config.annotation.authentication.builders.Au
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication import org.springframework.security.core.Authentication
import org.springframework.security.core.userdetails.PasswordEncodedUser
/** /**
* *
@ -50,7 +51,7 @@ class NamespaceAuthenticationManagerTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user())
} }
// Only necessary to have access to verify the AuthenticationManager // Only necessary to have access to verify the AuthenticationManager
@ -68,7 +69,7 @@ class NamespaceAuthenticationManagerTests extends BaseSpringSpec {
Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("user","password")) Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("user","password"))
then: then:
auth.credentials == "password" auth.credentials == "password"
auth.principal.password == "password" auth.principal.password
} }
@EnableWebSecurity @EnableWebSecurity
@ -77,7 +78,7 @@ class NamespaceAuthenticationManagerTests extends BaseSpringSpec {
auth auth
.eraseCredentials(false) .eraseCredentials(false)
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user())
} }
// Only necessary to have access to verify the AuthenticationManager // Only necessary to have access to verify the AuthenticationManager
@ -95,7 +96,7 @@ class NamespaceAuthenticationManagerTests extends BaseSpringSpec {
Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("user","password")) Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("user","password"))
then: then:
auth.credentials == "password" auth.credentials == "password"
auth.principal.password == "password" auth.principal.password
} }
@EnableWebSecurity @EnableWebSecurity
@ -105,7 +106,7 @@ class NamespaceAuthenticationManagerTests extends BaseSpringSpec {
auth auth
.eraseCredentials(false) .eraseCredentials(false)
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user())
} }
} }
} }

View File

@ -15,6 +15,8 @@
*/ */
package org.springframework.security.config.annotation.authentication package org.springframework.security.config.annotation.authentication
import org.springframework.security.core.userdetails.PasswordEncodedUser
import javax.sql.DataSource import javax.sql.DataSource
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
@ -89,9 +91,7 @@ class NamespaceJdbcUserServiceTests extends BaseSpringSpec {
// imports the default schema (will fail if already exists) // imports the default schema (will fail if already exists)
.withDefaultSchema() .withDefaultSchema()
// adds this user automatically (will fail if already exists) // adds this user automatically (will fail if already exists)
.withUser("user") .withUser(PasswordEncodedUser.user())
.password("password")
.roles("USER")
} }
// Only necessary to have access to verify the AuthenticationManager // Only necessary to have access to verify the AuthenticationManager

View File

@ -39,6 +39,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
import org.springframework.security.core.AuthenticationException import org.springframework.security.core.AuthenticationException
import org.springframework.security.core.authority.AuthorityUtils import org.springframework.security.core.authority.AuthorityUtils
import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.PasswordEncodedUser
import org.springframework.security.core.userdetails.User import org.springframework.security.core.userdetails.User
import org.springframework.security.core.userdetails.UserDetailsService import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
@ -64,7 +65,7 @@ class AuthenticationConfigurationTests extends BaseSpringSpec {
static class GlobalMethodSecurityAutowiredConfig { static class GlobalMethodSecurityAutowiredConfig {
@Autowired @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) { public void configureGlobal(AuthenticationManagerBuilder auth) {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER") auth.inMemoryAuthentication().withUser(PasswordEncodedUser.user())
} }
} }
@ -88,7 +89,7 @@ class AuthenticationConfigurationTests extends BaseSpringSpec {
static class WebSecurityConfig extends WebSecurityConfigurerAdapter { static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) { public void configureGlobal(AuthenticationManagerBuilder auth) {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER") auth.inMemoryAuthentication().withUser(PasswordEncodedUser.user())
} }
} }
@ -111,7 +112,7 @@ class AuthenticationConfigurationTests extends BaseSpringSpec {
static class WebMvcSecurityConfig extends WebSecurityConfigurerAdapter { static class WebMvcSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) { public void configureGlobal(AuthenticationManagerBuilder auth) {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER") auth.inMemoryAuthentication().withUser(PasswordEncodedUser.user())
} }
} }
@ -148,7 +149,7 @@ class AuthenticationConfigurationTests extends BaseSpringSpec {
@Configuration @Configuration
static class GlobalAuthenticationConfiguererAdapterImpl extends GlobalAuthenticationConfigurerAdapter { static class GlobalAuthenticationConfiguererAdapterImpl extends GlobalAuthenticationConfigurerAdapter {
public void init(AuthenticationManagerBuilder auth) throws Exception { public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER") auth.inMemoryAuthentication().withUser(PasswordEncodedUser.user())
} }
} }
@ -264,7 +265,7 @@ class AuthenticationConfigurationTests extends BaseSpringSpec {
public void init(AuthenticationManagerBuilder auth) throws Exception { public void init(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user())
} }
} }
@ -282,7 +283,7 @@ class AuthenticationConfigurationTests extends BaseSpringSpec {
return; return;
} }
User user = new User("boot","password", AuthorityUtils.createAuthorityList("ROLE_USER")) User user = User.withUserDetails(PasswordEncodedUser.user()).username("boot").build()
List<User> users = Arrays.asList(user); List<User> users = Arrays.asList(user);
InMemoryUserDetailsManager inMemory = new InMemoryUserDetailsManager(users); InMemoryUserDetailsManager inMemory = new InMemoryUserDetailsManager(users);
@ -373,11 +374,11 @@ class AuthenticationConfigurationTests extends BaseSpringSpec {
when: when:
am.authenticate(new UsernamePasswordAuthenticationToken("user", "password")) am.authenticate(new UsernamePasswordAuthenticationToken("user", "password"))
then: then:
1 * uds.loadUserByUsername("user") >> new User("user","password",AuthorityUtils.createAuthorityList("ROLE_USER")) 1 * uds.loadUserByUsername("user") >> PasswordEncodedUser.user()
when: when:
am.authenticate(new UsernamePasswordAuthenticationToken("user", "invalid")) am.authenticate(new UsernamePasswordAuthenticationToken("user", "invalid"))
then: then:
1 * uds.loadUserByUsername("user") >> new User("user","password",AuthorityUtils.createAuthorityList("ROLE_USER")) 1 * uds.loadUserByUsername("user") >> PasswordEncodedUser.user()
thrown(AuthenticationException.class) thrown(AuthenticationException.class)
} }
@ -514,4 +515,4 @@ class AuthenticationConfigurationTests extends BaseSpringSpec {
@Autowired @Autowired
Service service Service service
} }
} }

View File

@ -15,6 +15,8 @@
*/ */
package org.springframework.security.config.annotation.web package org.springframework.security.config.annotation.web
import org.springframework.security.core.userdetails.PasswordEncodedUser
import javax.servlet.http.HttpServletResponse import javax.servlet.http.HttpServletResponse
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
@ -93,7 +95,7 @@ public class SampleWebSecurityConfigurerAdapterTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) { protected void configure(AuthenticationManagerBuilder auth) {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER"); .withUser(PasswordEncodedUser.user());
} }
} }
@ -180,8 +182,8 @@ public class SampleWebSecurityConfigurerAdapterTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) { protected void configure(AuthenticationManagerBuilder auth) {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and() .withUser(PasswordEncodedUser.user())
.withUser("admin").password("password").roles("USER", "ADMIN"); .withUser(PasswordEncodedUser.admin());
} }
} }
@ -276,8 +278,8 @@ public class SampleWebSecurityConfigurerAdapterTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) { protected void configure(AuthenticationManagerBuilder auth) {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and() .withUser(PasswordEncodedUser.user())
.withUser("admin").password("password").roles("USER", "ADMIN"); .withUser(PasswordEncodedUser.admin());
} }
@Configuration @Configuration

View File

@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.security.config.annotation.web; package org.springframework.security.config.annotation.web
import org.springframework.security.core.userdetails.PasswordEncodedUser;
import static org.junit.Assert.* import static org.junit.Assert.*
import static org.springframework.security.config.annotation.web.WebSecurityConfigurerAdapterTestsConfigs.* import static org.springframework.security.config.annotation.web.WebSecurityConfigurerAdapterTestsConfigs.*
@ -94,7 +96,7 @@ class WebSecurityConfigurerAdapterTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user())
} }
@Override @Override
@ -117,7 +119,7 @@ class WebSecurityConfigurerAdapterTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user())
} }
@Override @Override
@ -153,7 +155,7 @@ class WebSecurityConfigurerAdapterTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser("user").password("{noop}password").roles("USER")
} }
@Override @Override
@ -234,7 +236,7 @@ class WebSecurityConfigurerAdapterTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user())
} }
} }

View File

@ -17,6 +17,7 @@ package org.springframework.security.config.annotation.web.configuration;
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.core.userdetails.PasswordEncodedUser
/** /**
* *
@ -34,7 +35,7 @@ public abstract class BaseWebConfig extends WebSecurityConfigurerAdapter {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and() .withUser(PasswordEncodedUser.user())
.withUser("admin").password("password").roles("USER", "ADMIN"); .withUser(PasswordEncodedUser.admin());
} }
} }

View File

@ -20,6 +20,7 @@ import org.springframework.security.authentication.TestingAuthenticationToken
import org.springframework.security.core.annotation.AuthenticationPrincipal import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.context.SecurityContext import org.springframework.security.core.context.SecurityContext
import org.springframework.security.core.context.SecurityContextImpl import org.springframework.security.core.context.SecurityContextImpl
import org.springframework.security.core.userdetails.PasswordEncodedUser
import org.springframework.security.core.userdetails.User import org.springframework.security.core.userdetails.User
import org.springframework.security.web.context.HttpSessionSecurityContextRepository import org.springframework.security.web.context.HttpSessionSecurityContextRepository
import org.springframework.test.context.web.WebAppConfiguration import org.springframework.test.context.web.WebAppConfiguration
@ -65,7 +66,7 @@ class EnableWebSecurityTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER"); .withUser(PasswordEncodedUser.user());
} }
@Bean @Bean

View File

@ -15,6 +15,8 @@
*/ */
package org.springframework.security.config.annotation.web.configurers package org.springframework.security.config.annotation.web.configurers
import org.springframework.security.core.userdetails.PasswordEncodedUser
import javax.servlet.http.HttpServletResponse import javax.servlet.http.HttpServletResponse
import spock.lang.Unroll import spock.lang.Unroll
@ -135,8 +137,8 @@ class CsrfConfigurerTests extends BaseSpringSpec {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user());
} }
} }
@ -257,8 +259,8 @@ class CsrfConfigurerTests extends BaseSpringSpec {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user());
} }
} }
@ -447,8 +449,8 @@ class CsrfConfigurerTests extends BaseSpringSpec {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user());
} }
} }
@ -487,8 +489,8 @@ class CsrfConfigurerTests extends BaseSpringSpec {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user());
} }
} }

View File

@ -15,6 +15,8 @@
*/ */
package org.springframework.security.config.annotation.web.configurers package org.springframework.security.config.annotation.web.configurers
import org.springframework.security.core.userdetails.PasswordEncodedUser
import javax.servlet.http.Cookie import javax.servlet.http.Cookie
import org.springframework.beans.factory.BeanCreationException import org.springframework.beans.factory.BeanCreationException
@ -75,7 +77,7 @@ public class RememberMeConfigurerTests extends BaseSpringSpec {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
User user = new User("user", "password", AuthorityUtils.createAuthorityList("ROLE_USER")) User user = PasswordEncodedUser.user();
DaoAuthenticationProvider provider = new DaoAuthenticationProvider() DaoAuthenticationProvider provider = new DaoAuthenticationProvider()
provider.userDetailsService = new InMemoryUserDetailsManager([user]) provider.userDetailsService = new InMemoryUserDetailsManager([user])
auth auth
@ -215,7 +217,7 @@ public class RememberMeConfigurerTests extends BaseSpringSpec {
public void configureGlobal(AuthenticationManagerBuilder auth) { public void configureGlobal(AuthenticationManagerBuilder auth) {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER"); .withUser(PasswordEncodedUser.user());
} }
} }
@ -235,8 +237,8 @@ public class RememberMeConfigurerTests extends BaseSpringSpec {
@Autowired @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) { public void configureGlobal(AuthenticationManagerBuilder auth) {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER"); .withUser(PasswordEncodedUser.user());
} }
} }
@ -261,8 +263,8 @@ public class RememberMeConfigurerTests extends BaseSpringSpec {
@Autowired @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) { public void configureGlobal(AuthenticationManagerBuilder auth) {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER"); .withUser(PasswordEncodedUser.user());
} }
} }

View File

@ -15,6 +15,8 @@
*/ */
package org.springframework.security.config.annotation.web.configurers package org.springframework.security.config.annotation.web.configurers
import org.springframework.security.core.userdetails.PasswordEncodedUser
import javax.servlet.http.HttpServletResponse import javax.servlet.http.HttpServletResponse
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
@ -178,7 +180,7 @@ class RequestCacheConfigurerTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user());
} }
} }
} }

View File

@ -15,6 +15,8 @@
*/ */
package org.springframework.security.config.annotation.web.configurers package org.springframework.security.config.annotation.web.configurers
import org.springframework.security.core.userdetails.PasswordEncodedUser
import javax.servlet.http.HttpServletResponse import javax.servlet.http.HttpServletResponse
import org.springframework.mock.web.MockFilterChain import org.springframework.mock.web.MockFilterChain
@ -144,7 +146,7 @@ class SessionManagementConfigurerTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) { protected void configure(AuthenticationManagerBuilder auth) {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user())
} }
} }
@ -200,7 +202,7 @@ class SessionManagementConfigurerTests extends BaseSpringSpec {
protected void configure(AuthenticationManagerBuilder auth) { protected void configure(AuthenticationManagerBuilder auth) {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER") .withUser(PasswordEncodedUser.user())
} }
} }

View File

@ -19,10 +19,10 @@ public abstract class ConfigTestUtils {
public static final String AUTH_PROVIDER_XML = "<authentication-manager alias='authManager'>" public static final String AUTH_PROVIDER_XML = "<authentication-manager alias='authManager'>"
+ " <authentication-provider>" + " <authentication-provider>"
+ " <user-service id='us'>" + " <user-service id='us'>"
+ " <user name='bob' password='bobspassword' authorities='ROLE_A,ROLE_B' />" + " <user name='bob' password='{noop}bobspassword' authorities='ROLE_A,ROLE_B' />"
+ " <user name='bill' password='billspassword' authorities='ROLE_A,ROLE_B,AUTH_OTHER' />" + " <user name='bill' password='{noop}billspassword' authorities='ROLE_A,ROLE_B,AUTH_OTHER' />"
+ " <user name='admin' password='password' authorities='ROLE_ADMIN,ROLE_USER' />" + " <user name='admin' password='{noop}password' authorities='ROLE_ADMIN,ROLE_USER' />"
+ " <user name='user' password='password' authorities='ROLE_USER' />" + " <user name='user' password='{noop}password' authorities='ROLE_USER' />"
+ " </user-service>" + " </user-service>"
+ " </authentication-provider>" + " </authentication-provider>"
+ "</authentication-manager>"; + "</authentication-manager>";

View File

@ -46,13 +46,13 @@ public class DataSourcePopulator implements InitializingBean {
* is disabled) Encoded password for bill is "wombat" Encoded password for bob is * is disabled) Encoded password for bill is "wombat" Encoded password for bob is
* "wombat" Encoded password for jane is "wombat" * "wombat" Encoded password for jane is "wombat"
*/ */
template.execute("INSERT INTO USERS VALUES('rod','koala',TRUE);"); template.execute("INSERT INTO USERS VALUES('rod','{noop}koala',TRUE);");
template.execute("INSERT INTO USERS VALUES('dianne','65d15fe9156f9c4bbffd98085992a44e',TRUE);"); template.execute("INSERT INTO USERS VALUES('dianne','{MD5}65d15fe9156f9c4bbffd98085992a44e',TRUE);");
template.execute("INSERT INTO USERS VALUES('scott','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);"); template.execute("INSERT INTO USERS VALUES('scott','{MD5}2b58af6dddbd072ed27ffc86725d7d3a',TRUE);");
template.execute("INSERT INTO USERS VALUES('peter','22b5c9accc6e1ba628cedc63a72d57f8',FALSE);"); template.execute("INSERT INTO USERS VALUES('peter','{MD5}22b5c9accc6e1ba628cedc63a72d57f8',FALSE);");
template.execute("INSERT INTO USERS VALUES('bill','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);"); template.execute("INSERT INTO USERS VALUES('bill','{MD5}2b58af6dddbd072ed27ffc86725d7d3a',TRUE);");
template.execute("INSERT INTO USERS VALUES('bob','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);"); template.execute("INSERT INTO USERS VALUES('bob','{MD5}2b58af6dddbd072ed27ffc86725d7d3a',TRUE);");
template.execute("INSERT INTO USERS VALUES('jane','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);"); template.execute("INSERT INTO USERS VALUES('jane','{MD5}2b58af6dddbd072ed27ffc86725d7d3a',TRUE);");
template.execute("INSERT INTO AUTHORITIES VALUES('rod','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('rod','ROLE_USER');");
template.execute("INSERT INTO AUTHORITIES VALUES('rod','ROLE_SUPERVISOR');"); template.execute("INSERT INTO AUTHORITIES VALUES('rod','ROLE_SUPERVISOR');");
template.execute("INSERT INTO AUTHORITIES VALUES('dianne','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('dianne','ROLE_USER');");

View File

@ -45,6 +45,7 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextImpl; import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.PasswordEncodedUser;
import org.springframework.security.web.context.HttpRequestResponseHolder; import org.springframework.security.web.context.HttpRequestResponseHolder;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository; import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.security.web.csrf.CsrfToken; import org.springframework.security.web.csrf.CsrfToken;
@ -126,7 +127,7 @@ public class SessionManagementConfigurerServlet31Tests {
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER"); .withUser(PasswordEncodedUser.user());
} }
// @formatter:on // @formatter:on
} }

View File

@ -32,6 +32,7 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.PasswordEncodedUser;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.FilterChainProxy; import org.springframework.security.web.FilterChainProxy;
@ -66,9 +67,7 @@ public class AuthenticationConfigurationGh3935Tests {
public void delegateUsesExisitingAuthentication() { public void delegateUsesExisitingAuthentication() {
String username = "user"; String username = "user";
String password = "password"; String password = "password";
User user = new User(username, password, when(this.uds.loadUserByUsername(username)).thenReturn(PasswordEncodedUser.user());
AuthorityUtils.createAuthorityList("ROLE_USER"));
when(this.uds.loadUserByUsername(username)).thenReturn(user);
AuthenticationManager authenticationManager = this.adapter.authenticationManager; AuthenticationManager authenticationManager = this.adapter.authenticationManager;
assertThat(authenticationManager).isNotNull(); assertThat(authenticationManager).isNotNull();
@ -77,7 +76,7 @@ public class AuthenticationConfigurationGh3935Tests {
new UsernamePasswordAuthenticationToken(username, password)); new UsernamePasswordAuthenticationToken(username, password));
verify(this.uds).loadUserByUsername(username); verify(this.uds).loadUserByUsername(username);
assertThat(auth.getPrincipal()).isEqualTo(user); assertThat(auth.getPrincipal()).isEqualTo(PasswordEncodedUser.user());
} }
@EnableWebSecurity @EnableWebSecurity

View File

@ -39,7 +39,7 @@ public class AuthenticationManagerBeanDefinitionParserTests {
private static final String CONTEXT = "<authentication-manager id='am'>" private static final String CONTEXT = "<authentication-manager id='am'>"
+ " <authentication-provider>" + " <authentication-provider>"
+ " <user-service>" + " <user-service>"
+ " <user name='bob' password='bobspassword' authorities='ROLE_A,ROLE_B' />" + " <user name='bob' password='{noop}bobspassword' authorities='ROLE_A,ROLE_B' />"
+ " </user-service>" + " </authentication-provider>" + " </user-service>" + " </authentication-provider>"
+ "</authentication-manager>"; + "</authentication-manager>";
private AbstractXmlApplicationContext appContext; private AbstractXmlApplicationContext appContext;

View File

@ -51,7 +51,7 @@ public class AuthenticationProviderBeanDefinitionParserTests {
public void worksWithEmbeddedUserService() { public void worksWithEmbeddedUserService() {
setContext(" <authentication-provider>" setContext(" <authentication-provider>"
+ " <user-service>" + " <user-service>"
+ " <user name='bob' password='bobspassword' authorities='ROLE_A' />" + " <user name='bob' password='{noop}bobspassword' authorities='ROLE_A' />"
+ " </user-service>" + " </authentication-provider>"); + " </user-service>" + " </authentication-provider>");
getProvider().authenticate(bob); getProvider().authenticate(bob);
} }
@ -63,7 +63,7 @@ public class AuthenticationProviderBeanDefinitionParserTests {
+ " <authentication-provider user-service-ref='myUserService' />" + " <authentication-provider user-service-ref='myUserService' />"
+ " </authentication-manager>" + " </authentication-manager>"
+ " <user-service id='myUserService'>" + " <user-service id='myUserService'>"
+ " <user name='bob' password='bobspassword' authorities='ROLE_A' />" + " <user name='bob' password='{noop}bobspassword' authorities='ROLE_A' />"
+ " </user-service>"); + " </user-service>");
getProvider().authenticate(bob); getProvider().authenticate(bob);
} }

View File

@ -56,7 +56,7 @@ import org.springframework.util.ReflectionUtils;
public class SessionManagementConfigServlet31Tests { public class SessionManagementConfigServlet31Tests {
private static final String XML_AUTHENTICATION_MANAGER = "<authentication-manager>" private static final String XML_AUTHENTICATION_MANAGER = "<authentication-manager>"
+ " <authentication-provider>" + " <user-service>" + " <authentication-provider>" + " <user-service>"
+ " <user name='user' password='password' authorities='ROLE_USER' />" + " <user name='user' password='{noop}password' authorities='ROLE_USER' />"
+ " </user-service>" + " </authentication-provider>" + " </user-service>" + " </authentication-provider>"
+ "</authentication-manager>"; + "</authentication-manager>";

View File

@ -5,7 +5,7 @@ create table groups (id bigint generated by default as identity(start with 0) pr
create table group_authorities (group_id bigint not null,authority varchar(50) not null,constraint fk_group_authorities_group foreign key(group_id) references groups(id)); create table group_authorities (group_id bigint not null,authority varchar(50) not null,constraint fk_group_authorities_group foreign key(group_id) references groups(id));
create table group_members (id bigint generated by default as identity(start with 0) primary key,username varchar(50) not null,group_id bigint not null,constraint fk_group_members_group foreign key(group_id) references groups(id)); create table group_members (id bigint generated by default as identity(start with 0) primary key,username varchar(50) not null,group_id bigint not null,constraint fk_group_members_group foreign key(group_id) references groups(id));
insert into users values('user','password'); insert into users values('user','{noop}password');
insert into roles values('user','USER'); insert into roles values('user','USER');
insert into groups values(1,'OPERATIONS'); insert into groups values(1,'OPERATIONS');

View File

@ -1,2 +1,2 @@
joe=joespassword,ROLE_A joe={noop}joespassword,ROLE_A
bob=bobspassword,ROLE_A,ROLE_B bob={noop}bobspassword,ROLE_A,ROLE_B

View File

@ -16,4 +16,4 @@
# */ # */
# #
user=password,ROLE_USER user={noop}password,ROLE_USER

View File

@ -24,7 +24,7 @@ import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.NoOpPasswordEncoder; import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -63,7 +63,7 @@ public class DaoAuthenticationProvider extends AbstractUserDetailsAuthentication
private UserDetailsService userDetailsService; private UserDetailsService userDetailsService;
public DaoAuthenticationProvider() { public DaoAuthenticationProvider() {
setPasswordEncoder(NoOpPasswordEncoder.getInstance()); setPasswordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder());
} }
// ~ Methods // ~ Methods

View File

@ -50,6 +50,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache; import org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache;
import org.springframework.security.core.userdetails.cache.NullUserCache; import org.springframework.security.core.userdetails.cache.NullUserCache;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
/** /**
@ -70,7 +71,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"rod", "KOala"); "rod", "KOala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -86,7 +87,7 @@ public class DaoAuthenticationProviderTests {
@Test @Test
public void testReceivedBadCredentialsWhenCredentialsNotProvided() { public void testReceivedBadCredentialsWhenCredentialsNotProvided() {
// Test related to SEC-434 // Test related to SEC-434
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -106,7 +107,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"peter", "opal"); "peter", "opal");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService( provider.setUserDetailsService(
new MockAuthenticationDaoUserPeterAccountExpired()); new MockAuthenticationDaoUserPeterAccountExpired());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -125,7 +126,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"peter", "opal"); "peter", "opal");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserPeterAccountLocked()); provider.setUserDetailsService(new MockAuthenticationDaoUserPeterAccountLocked());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -143,7 +144,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"peter", "opal"); "peter", "opal");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService( provider.setUserDetailsService(
new MockAuthenticationDaoUserPeterCredentialsExpired()); new MockAuthenticationDaoUserPeterCredentialsExpired());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -174,7 +175,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"peter", "opal"); "peter", "opal");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserPeter()); provider.setUserDetailsService(new MockAuthenticationDaoUserPeter());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -192,7 +193,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"rod", "koala"); "rod", "koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoSimulateBackendError()); provider.setUserDetailsService(new MockAuthenticationDaoSimulateBackendError());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -209,7 +210,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
null, "koala"); null, "koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -227,7 +228,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"rod", "INVALID_PASSWORD"); "rod", "INVALID_PASSWORD");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -245,7 +246,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"INVALID_USER", "koala"); "INVALID_USER", "koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setHideUserNotFoundExceptions(false); // we want provider.setHideUserNotFoundExceptions(false); // we want
// UsernameNotFoundExceptions // UsernameNotFoundExceptions
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
@ -265,7 +266,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"INVALID_USER", "koala"); "INVALID_USER", "koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
assertThat(provider.isHideUserNotFoundExceptions()).isTrue(); assertThat(provider.isHideUserNotFoundExceptions()).isTrue();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -284,7 +285,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"RoD", "koala"); "RoD", "koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -303,7 +304,7 @@ public class DaoAuthenticationProviderTests {
"rod", "koala"); "rod", "koala");
token.setDetails("192.168.0.1"); token.setDetails("192.168.0.1");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -327,7 +328,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"rod", "koala"); "rod", "koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -352,7 +353,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"rod", "koala"); "rod", "koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
provider.setForcePrincipalAsString(true); provider.setForcePrincipalAsString(true);
@ -373,7 +374,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"rod", "koala"); "rod", "koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoReturnsNull()); provider.setUserDetailsService(new MockAuthenticationDaoReturnsNull());
try { try {
@ -410,7 +411,7 @@ public class DaoAuthenticationProviderTests {
MockAuthenticationDaoUserrod authenticationDao = new MockAuthenticationDaoUserrod(); MockAuthenticationDaoUserrod authenticationDao = new MockAuthenticationDaoUserrod();
MockUserCache cache = new MockUserCache(); MockUserCache cache = new MockUserCache();
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(authenticationDao); provider.setUserDetailsService(authenticationDao);
provider.setUserCache(cache); provider.setUserCache(cache);
@ -448,7 +449,7 @@ public class DaoAuthenticationProviderTests {
@Test @Test
public void testStartupFailsIfNoUserCacheSet() throws Exception { public void testStartupFailsIfNoUserCacheSet() throws Exception {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
assertThat(provider.getUserCache().getClass()).isEqualTo(NullUserCache.class); assertThat(provider.getUserCache().getClass()).isEqualTo(NullUserCache.class);
provider.setUserCache(null); provider.setUserCache(null);
@ -464,7 +465,7 @@ public class DaoAuthenticationProviderTests {
@Test @Test
public void testStartupSuccess() throws Exception { public void testStartupSuccess() throws Exception {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
UserDetailsService userDetailsService = new MockAuthenticationDaoUserrod(); UserDetailsService userDetailsService = new MockAuthenticationDaoUserrod();
provider.setUserDetailsService(userDetailsService); provider.setUserDetailsService(userDetailsService);
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
@ -475,7 +476,7 @@ public class DaoAuthenticationProviderTests {
@Test @Test
public void testSupports() { public void testSupports() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
assertThat(provider.supports(UsernamePasswordAuthenticationToken.class)).isTrue(); assertThat(provider.supports(UsernamePasswordAuthenticationToken.class)).isTrue();
assertThat(!provider.supports(TestingAuthenticationToken.class)).isTrue(); assertThat(!provider.supports(TestingAuthenticationToken.class)).isTrue();
} }
@ -527,7 +528,7 @@ public class DaoAuthenticationProviderTests {
public void testUserNotFoundDefaultEncoder() { public void testUserNotFoundDefaultEncoder() {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"missing", null); "missing", null);
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setHideUserNotFoundExceptions(false); provider.setHideUserNotFoundExceptions(false);
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
try { try {
@ -713,4 +714,10 @@ public class DaoAuthenticationProviderTests {
} }
} }
} }
private DaoAuthenticationProvider createProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
return provider;
}
} }

View File

@ -26,7 +26,7 @@
<authentication-manager> <authentication-manager>
<authentication-provider> <authentication-provider>
<user-service> <user-service>
<user name="bob" password="bobspassword" authorities="ROLE_A,ROLE_B"/> <user name="bob" password="{noop}bobspassword" authorities="ROLE_A,ROLE_B"/>
</user-service> </user-service>
</authentication-provider> </authentication-provider>
</authentication-manager> </authentication-manager>

View File

@ -10,7 +10,7 @@
<security:authentication-manager alias="authenticationManager"> <security:authentication-manager alias="authenticationManager">
<security:authentication-provider> <security:authentication-provider>
<security:user-service> <security:user-service>
<security:user name="bob" password="bobspassword" authorities="ROLE_A,ROLE_B"/> <security:user name="bob" password="{noop}bobspassword" authorities="ROLE_A,ROLE_B"/>
</security:user-service> </security:user-service>
</security:authentication-provider> </security:authentication-provider>
</security:authentication-manager> </security:authentication-manager>

View File

@ -9,11 +9,11 @@
<authentication-manager alias="authenticationManager"> <authentication-manager alias="authenticationManager">
<authentication-provider> <authentication-provider>
<user-service> <user-service>
<user name="miles" password="milespassword" authorities="ROLE_USER,ROLE_JAZZ,ROLE_TRUMPETER"/> <user name="miles" password="{noop}milespassword" authorities="ROLE_USER,ROLE_JAZZ,ROLE_TRUMPETER"/>
<user name="johnc" password="johncspassword" authorities="ROLE_USER,ROLE_JAZZ,ROLE_SAXOPHONIST"/> <user name="johnc" password="{noop}johncspassword" authorities="ROLE_USER,ROLE_JAZZ,ROLE_SAXOPHONIST"/>
<user name="jimi" password="jimispassword" authorities="ROLE_USER,ROLE_ROCK,ROLE_GUITARIST"/> <user name="jimi" password="{noop}jimispassword" authorities="ROLE_USER,ROLE_ROCK,ROLE_GUITARIST"/>
<user name="bessie" password="bessiespassword" authorities="ROLE_USER,ROLE_JAZZ,ROLE_SINGER"/> <user name="bessie" password="{noop}bessiespassword" authorities="ROLE_USER,ROLE_JAZZ,ROLE_SINGER"/>
<user name="theescapist&lt;&gt;&amp;." password="theescapistspassword" authorities="ROLE_USER"/> <user name="theescapist&lt;&gt;&amp;." password="{noop}theescapistspassword" authorities="ROLE_USER"/>
</user-service> </user-service>
</authentication-provider> </authentication-provider>
</authentication-manager> </authentication-manager>

View File

@ -20,6 +20,7 @@ import org.springframework.security.config.annotation.authentication.builders.Au
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
/** /**
* @author Joe Grandja * @author Joe Grandja
@ -44,7 +45,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER"); .withUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("USER"));
} }
// @formatter:on // @formatter:on
} }

View File

@ -20,6 +20,7 @@ import org.springframework.security.config.annotation.authentication.builders.Au
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
@EnableWebSecurity @EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { public class SecurityConfig extends WebSecurityConfigurerAdapter {
@ -47,7 +48,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
AuthenticationManagerBuilder auth) throws Exception { AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER"); .withUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("USER"));
} }
// @formatter:on // @formatter:on
} }

View File

@ -18,6 +18,7 @@ package org.springframework.security.samples.config;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
@EnableWebSecurity @EnableWebSecurity
public class SecurityConfig { public class SecurityConfig {
@ -28,7 +29,7 @@ public class SecurityConfig {
AuthenticationManagerBuilder auth) throws Exception { AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("user").password("password").roles("USER"); .withUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("USER"));
} }
// @formatter:on // @formatter:on
} }

View File

@ -18,6 +18,7 @@ package org.springframework.security.samples.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@ -27,9 +28,8 @@ public class SecurityConfig {
// @formatter:off // @formatter:off
@Bean @Bean
public UserDetailsService userDetailsService() throws Exception { public UserDetailsService userDetailsService() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build();
manager.createUser(User.withUsername("user").password("password").roles("USER").build()); return new InMemoryUserDetailsManager(user);
return manager;
} }
// @formatter:on // @formatter:on
} }

View File

@ -15,21 +15,23 @@
*/ */
package org.springframework.security.samples.config; package org.springframework.security.samples.config;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@EnableWebSecurity @EnableWebSecurity
public class SecurityConfig { public class SecurityConfig {
// @formatter:off // @formatter:off
@Autowired @Bean
public void configureGlobal( public UserDetailsService userDetailsService() throws Exception {
AuthenticationManagerBuilder auth) throws Exception { User.UserBuilder builder = User.withDefaultPasswordEncoder();
auth UserDetails user = builder.username("user").password("password").roles("USER").build();
.inMemoryAuthentication() UserDetails admin = builder.username("admin").password("password").roles("USER", "ADMIN").build();
.withUser("user").password("password").roles("USER").and() return new InMemoryUserDetailsManager(user, admin);
.withUser("admin").password("password").roles("USER","ADMIN");
} }
// @formatter:on // @formatter:on
} }

View File

@ -20,6 +20,7 @@ import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
@EnableWebSecurity @EnableWebSecurity
public class SecurityConfig { public class SecurityConfig {
@ -33,7 +34,7 @@ public class SecurityConfig {
.jdbcAuthentication() .jdbcAuthentication()
.dataSource(dataSource) .dataSource(dataSource)
.withDefaultSchema() .withDefaultSchema()
.withUser("user").password("password").roles("USER"); .withUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("USER"));
} }
// @formatter:on // @formatter:on
} }

View File

@ -6,6 +6,6 @@
<http /> <http />
<user-service> <user-service>
<user name="user" password="password" authorities="ROLE_USER" /> <user name="user" password="{noop}password" authorities="ROLE_USER" />
</user-service> </user-service>
</b:beans> </b:beans>

View File

@ -20,9 +20,14 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
@ -81,11 +86,10 @@ public class SecurityMockMvcResultMatchersTests {
static class Config extends WebSecurityConfigurerAdapter { static class Config extends WebSecurityConfigurerAdapter {
// @formatter:off // @formatter:off
@Autowired @Bean
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { public UserDetailsService userDetailsService() {
auth UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER", "SELLER").build();
.inMemoryAuthentication() return new InMemoryUserDetailsManager(user);
.withUser("user").roles("USER","SELLER").password("password");
} }
// @formatter:on // @formatter:on

View File

@ -27,10 +27,15 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
@ -77,11 +82,10 @@ public class SecurityMockWithAuthoritiesMvcResultMatchersTests {
static class Config extends WebSecurityConfigurerAdapter { static class Config extends WebSecurityConfigurerAdapter {
// @formatter:off // @formatter:off
@Autowired @Bean
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { public UserDetailsService userDetailsService() {
auth UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("ADMIN", "SELLER").build();
.inMemoryAuthentication() return new InMemoryUserDetailsManager(user);
.withUser("user").authorities("ROLE_ADMIN", "ROLE_SELLER").password("password");
} }
// @formatter:on // @formatter:on

View File

@ -26,9 +26,14 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
@ -83,11 +88,10 @@ public class AuthenticationTests {
@EnableWebMvc @EnableWebMvc
static class Config extends WebSecurityConfigurerAdapter { static class Config extends WebSecurityConfigurerAdapter {
// @formatter:off // @formatter:off
@Autowired @Bean
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { public UserDetailsService userDetailsService() {
auth UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build();
.inMemoryAuthentication() return new InMemoryUserDetailsManager(user);
.withUser("user").password("password").roles("USER");
} }
// @formatter:on // @formatter:on
} }

View File

@ -31,6 +31,10 @@ import org.springframework.security.config.annotation.authentication.builders.Au
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository; import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.security.web.context.SecurityContextRepository; import org.springframework.security.web.context.SecurityContextRepository;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -106,11 +110,10 @@ public class CustomConfigAuthenticationTests {
// @formatter:on // @formatter:on
// @formatter:off // @formatter:off
@Autowired @Bean
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { public UserDetailsService userDetailsService() {
auth UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build();
.inMemoryAuthentication() return new InMemoryUserDetailsManager(user);
.withUser("user").password("password").roles("USER");
} }
// @formatter:on // @formatter:on

View File

@ -23,10 +23,14 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders; import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.FormLoginRequestBuilder; import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.FormLoginRequestBuilder;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -92,11 +96,10 @@ public class CustomLoginRequestBuilderAuthenticationTests {
// @formatter:on // @formatter:on
// @formatter:off // @formatter:off
@Autowired @Bean
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { public UserDetailsService userDetailsService() {
auth UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build();
.inMemoryAuthentication() return new InMemoryUserDetailsManager(user);
.withUser("user").password("password").roles("USER");
} }
// @formatter:on // @formatter:on
} }