Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
ecee1905
Commit
ecee1905
authored
Oct 15, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add some precautionary tests for documenting AuthenticationManager config
parent
7cb3ae43
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
131 additions
and
11 deletions
+131
-11
SecurityAutoConfigurationTests.java
...utoconfigure/security/SecurityAutoConfigurationTests.java
+18
-9
SpringBootWebSecurityConfigurationTests.java
...ure/security/SpringBootWebSecurityConfigurationTests.java
+113
-2
No files found.
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SecurityAutoConfigurationTests.java
View file @
ecee1905
...
...
@@ -16,9 +16,15 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
security
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
fail
;
import
java.util.List
;
import
java.util.concurrent.atomic.AtomicReference
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.TestAutoConfigurationPackage
;
...
...
@@ -45,11 +51,6 @@ import org.springframework.security.web.FilterChainProxy;
import
org.springframework.security.web.SecurityFilterChain
;
import
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
fail
;
/**
* Tests for {@link SecurityAutoConfiguration}.
*
...
...
@@ -59,6 +60,13 @@ public class SecurityAutoConfigurationTests {
private
AnnotationConfigWebApplicationContext
context
;
@After
public
void
close
()
{
if
(
context
!=
null
)
{
context
.
close
();
}
}
@Test
public
void
testWebConfiguration
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigWebApplicationContext
();
...
...
@@ -137,11 +145,12 @@ public class SecurityAutoConfigurationTests {
public
void
testOverrideAuthenticationManager
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigWebApplicationContext
();
this
.
context
.
setServletContext
(
new
MockServletContext
());
this
.
context
.
register
(
Test
Configuration
.
class
,
SecurityAuto
Configuration
.
class
,
ServerPropertiesAutoConfiguration
.
class
,
this
.
context
.
register
(
Test
Authentication
Configuration
.
class
,
Se
curityAutoConfiguration
.
class
,
Se
rverPropertiesAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertEquals
(
this
.
context
.
getBean
(
TestConfiguration
.
class
).
authenticationManager
,
assertEquals
(
this
.
context
.
getBean
(
TestAuthenticationConfiguration
.
class
).
authenticationManager
,
this
.
context
.
getBean
(
AuthenticationManager
.
class
));
}
...
...
@@ -168,7 +177,7 @@ public class SecurityAutoConfigurationTests {
}
@Configuration
protected
static
class
TestConfiguration
{
protected
static
class
Test
Authentication
Configuration
{
private
AuthenticationManager
authenticationManager
;
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SpringBootWebSecurityConfigurationTests.java
View file @
ecee1905
...
...
@@ -16,10 +16,38 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
security
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.lang.annotation.Documented
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
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.WebSecurity
;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
;
/**
* Tests for {@link SpringBootWebSecurityConfiguration}.
*
...
...
@@ -27,10 +55,93 @@ import static org.junit.Assert.assertTrue;
*/
public
class
SpringBootWebSecurityConfigurationTests
{
private
ConfigurableApplicationContext
context
;
@After
public
void
close
()
{
if
(
context
!=
null
)
{
context
.
close
();
}
}
@Test
public
void
testDefaultIgnores
()
{
assertTrue
(
SpringBootWebSecurityConfiguration
.
getIgnored
(
new
SecurityProperties
()).
contains
(
"/css/**"
));
}
@Test
public
void
testWebConfigurationOverrideGlobalAuthentication
()
throws
Exception
{
this
.
context
=
SpringApplication
.
run
(
TestWebConfiguration
.
class
,
"--server.port=0"
,
"--debug"
);
assertNotNull
(
this
.
context
.
getBean
(
AuthenticationManagerBuilder
.
class
));
assertNotNull
(
this
.
context
.
getBean
(
AuthenticationManager
.
class
).
authenticate
(
new
UsernamePasswordAuthenticationToken
(
"dave"
,
"secret"
)));
}
@Test
public
void
testWebConfigurationInjectGlobalAuthentication
()
throws
Exception
{
this
.
context
=
SpringApplication
.
run
(
TestInjectWebConfiguration
.
class
,
"--server.port=0"
,
"--debug"
);
assertNotNull
(
this
.
context
.
getBean
(
AuthenticationManagerBuilder
.
class
));
assertNotNull
(
this
.
context
.
getBean
(
AuthenticationManager
.
class
).
authenticate
(
new
UsernamePasswordAuthenticationToken
(
"dave"
,
"secret"
)));
}
@Configuration
@Import
(
TestWebConfiguration
.
class
)
@Order
(
Ordered
.
LOWEST_PRECEDENCE
)
protected
static
class
TestInjectWebConfiguration
extends
WebSecurityConfigurerAdapter
{
// It's a bad idea to inject an AuthenticationManager into a
// WebSecurityConfigurerAdapter because it can cascade early instantiation,
// unless you explicitly want the Boot default AuthenticationManager. It's
// better to inject the builder, if you want the global AuthenticationManager. It
// might even be necessary to wrap the builder in a lazy AuthenticationManager
// (that calls getOrBuild() only when the AuthenticationManager is actually
// called).
@Autowired
private
AuthenticationManagerBuilder
auth
;
@Override
public
void
init
(
WebSecurity
web
)
throws
Exception
{
auth
.
getOrBuild
();
}
}
@MinimalWebConfiguration
@Import
(
SecurityAutoConfiguration
.
class
)
@Order
(
Ordered
.
HIGHEST_PRECEDENCE
+
10
)
protected
static
class
TestWebConfiguration
extends
WebSecurityConfigurerAdapter
{
@Autowired
public
void
init
(
AuthenticationManagerBuilder
auth
)
throws
Exception
{
// @formatter:off
auth
.
inMemoryAuthentication
()
.
withUser
(
"dave"
)
.
password
(
"secret"
)
.
roles
(
"USER"
);
// @formatter:on
}
@Override
protected
void
configure
(
HttpSecurity
http
)
throws
Exception
{
http
.
authorizeRequests
().
anyRequest
().
denyAll
();
}
}
@Configuration
@Target
(
ElementType
.
TYPE
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
@Import
({
EmbeddedServletContainerAutoConfiguration
.
class
,
ServerPropertiesAutoConfiguration
.
class
,
DispatcherServletAutoConfiguration
.
class
,
WebMvcAutoConfiguration
.
class
,
HttpMessageConvertersAutoConfiguration
.
class
,
ErrorMvcAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
})
protected
static
@interface
MinimalWebConfiguration
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment