|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2013 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2018 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.
|
|
@@ -13,26 +13,39 @@
|
|
* 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.authentication;
|
|
package org.springframework.security.config.annotation.authentication;
|
|
|
|
|
|
|
|
+import org.junit.Rule;
|
|
|
|
+import org.junit.Test;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
|
-import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
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.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.config.test.SpringTestRule;
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
+
|
|
|
|
+import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
|
|
|
|
+import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Class Containing the {@link Configuration} for {@link PasswordEncoderConfigurerTests}.
|
|
|
|
- * Separate to ensure the configuration compiles in Java (i.e. we are not using hidden
|
|
|
|
- * methods).
|
|
|
|
- *
|
|
|
|
* @author Rob Winch
|
|
* @author Rob Winch
|
|
- * @since 3.2
|
|
|
|
*/
|
|
*/
|
|
-public class PasswordEncoderConfigurerConfigs {
|
|
|
|
|
|
+public class PasswordEncoderConfigurerTests {
|
|
|
|
+
|
|
|
|
+ @Rule
|
|
|
|
+ public final SpringTestRule spring = new SpringTestRule();
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private MockMvc mockMvc;
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void passwordEncoderRefWhenNoAuthenticationManagerBeanThenNoExceptionThrown() {
|
|
|
|
+ this.spring.register(PasswordEncoderConfig.class).autowire();
|
|
|
|
+ }
|
|
|
|
|
|
@EnableWebSecurity
|
|
@EnableWebSecurity
|
|
static class PasswordEncoderConfig extends WebSecurityConfigurerAdapter {
|
|
static class PasswordEncoderConfig extends WebSecurityConfigurerAdapter {
|
|
@@ -46,12 +59,6 @@ public class PasswordEncoderConfigurerConfigs {
|
|
}
|
|
}
|
|
// @formatter:on
|
|
// @formatter:on
|
|
|
|
|
|
- @Bean
|
|
|
|
- @Override
|
|
|
|
- public AuthenticationManager authenticationManagerBean() throws Exception {
|
|
|
|
- return super.authenticationManagerBean();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
protected void configure(HttpSecurity http) throws Exception {
|
|
protected void configure(HttpSecurity http) throws Exception {
|
|
}
|
|
}
|
|
@@ -62,9 +69,17 @@ public class PasswordEncoderConfigurerConfigs {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void passwordEncoderRefWhenAuthenticationManagerBuilderThenAuthenticationSuccess() throws Exception {
|
|
|
|
+ this.spring.register(PasswordEncoderNoAuthManagerLoadsConfig.class).autowire();
|
|
|
|
+
|
|
|
|
+ this.mockMvc.perform(formLogin())
|
|
|
|
+ .andExpect(authenticated());
|
|
|
|
+ }
|
|
|
|
+
|
|
@EnableWebSecurity
|
|
@EnableWebSecurity
|
|
static class PasswordEncoderNoAuthManagerLoadsConfig extends
|
|
static class PasswordEncoderNoAuthManagerLoadsConfig extends
|
|
- WebSecurityConfigurerAdapter {
|
|
|
|
|
|
+ WebSecurityConfigurerAdapter {
|
|
// @formatter:off
|
|
// @formatter:off
|
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
BCryptPasswordEncoder encoder = passwordEncoder();
|
|
BCryptPasswordEncoder encoder = passwordEncoder();
|