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
760d6ece
Commit
760d6ece
authored
Mar 02, 2015
by
Rob Winch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix Unnecessarily Adding Default Security User
Fixes gh-2567
parent
50e1f805
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
13 deletions
+106
-13
AuthenticationManagerConfiguration.java
...onfigure/security/AuthenticationManagerConfiguration.java
+50
-12
SecurityAutoConfigurationTests.java
...utoconfigure/security/SecurityAutoConfigurationTests.java
+56
-1
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/AuthenticationManagerConfiguration.java
View file @
760d6ece
...
@@ -43,6 +43,7 @@ import org.springframework.security.config.annotation.SecurityConfigurer;
...
@@ -43,6 +43,7 @@ import org.springframework.security.config.annotation.SecurityConfigurer;
import
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
;
import
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
;
import
org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration
;
import
org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration
;
import
org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter
;
import
org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter
;
import
org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
/**
/**
...
@@ -137,21 +138,58 @@ public class AuthenticationManagerConfiguration {
...
@@ -137,21 +138,58 @@ public class AuthenticationManagerConfiguration {
@Override
@Override
public
void
init
(
AuthenticationManagerBuilder
auth
)
throws
Exception
{
public
void
init
(
AuthenticationManagerBuilder
auth
)
throws
Exception
{
if
(
auth
.
isConfigured
())
{
auth
.
apply
(
new
DefaultingInMemoryUserDetailsManagerConfigurer
(
this
.
security
));
return
;
}
/**
* This is necessary to delay adding the default user.
*
* <ul>
* <li>A GlobalAuthenticationConfigurerAdapter will initialize the
* AuthenticationManagerBuilder with a Configurer which will be after any
* GlobalAuthenticationConfigurerAdapter</li>
* <li>BootDefaultingAuthenticationConfigurerAdapter will be invoked after all
* GlobalAuthenticationConfigurerAdapter, but before the Configurers that were
* added by other GlobalAuthenticationConfigurerAdapter instances</li>
* <li>BootDefaultingAuthenticationConfigurerAdapter will add
* DefaultingInMemoryUserDetailsManagerConfigurer after all Configurer instances</li>
* <li>All init methods will be invoked</li>
* <li>All configure methods will be invoked which is where the
* AuthenticationProvider instances are setup</li>
* <li>If no AuthenticationProviders were provided,
* DefaultingInMemoryUserDetailsManagerConfigurer will default the value</li>
* </ul>
*
* @author Rob Winch
*/
private
static
class
DefaultingInMemoryUserDetailsManagerConfigurer
extends
InMemoryUserDetailsManagerConfigurer
<
AuthenticationManagerBuilder
>
{
private
final
SecurityProperties
security
;
public
DefaultingInMemoryUserDetailsManagerConfigurer
(
SecurityProperties
security
)
{
this
.
security
=
security
;
}
}
User
user
=
this
.
security
.
getUser
();
@Override
if
(
user
.
isDefaultPassword
())
{
public
void
configure
(
AuthenticationManagerBuilder
auth
)
throws
Exception
{
logger
.
info
(
"\n\nUsing default security password: "
+
user
.
getPassword
()
if
(
auth
.
isConfigured
())
{
+
"\n\n"
);
return
;
}
User
user
=
this
.
security
.
getUser
();
if
(
user
.
isDefaultPassword
())
{
logger
.
info
(
"\n\nUsing default security password: "
+
user
.
getPassword
()
+
"\n"
);
}
Set
<
String
>
roles
=
new
LinkedHashSet
<
String
>(
user
.
getRole
());
withUser
(
user
.
getName
()).
password
(
user
.
getPassword
()).
roles
(
roles
.
toArray
(
new
String
[
roles
.
size
()]));
super
.
configure
(
auth
);
}
}
Set
<
String
>
roles
=
new
LinkedHashSet
<
String
>(
user
.
getRole
());
auth
.
inMemoryAuthentication
().
withUser
(
user
.
getName
())
.
password
(
user
.
getPassword
())
.
roles
(
roles
.
toArray
(
new
String
[
roles
.
size
()]));
}
}
}
}
}
}
\ No newline at end of file
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SecurityAutoConfigurationTests.java
View file @
760d6ece
/*
/*
* Copyright 2012-201
4
the original author or authors.
* Copyright 2012-201
5
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.
...
@@ -40,6 +40,7 @@ import org.springframework.security.authentication.AuthenticationManager;
...
@@ -40,6 +40,7 @@ import org.springframework.security.authentication.AuthenticationManager;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.security.authentication.TestingAuthenticationToken
;
import
org.springframework.security.authentication.TestingAuthenticationToken
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.authentication.event.AbstractAuthenticationEvent
;
import
org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent
;
import
org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent
;
import
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
;
import
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
;
import
org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter
;
import
org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter
;
...
@@ -226,6 +227,60 @@ public class SecurityAutoConfigurationTests {
...
@@ -226,6 +227,60 @@ public class SecurityAutoConfigurationTests {
assertNotNull
(
this
.
context
.
getBean
(
JpaTransactionManager
.
class
));
assertNotNull
(
this
.
context
.
getBean
(
JpaTransactionManager
.
class
));
}
}
@Test
public
void
testDefaultUsernamePassword
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigWebApplicationContext
();
this
.
context
.
setServletContext
(
new
MockServletContext
());
this
.
context
.
register
(
SecurityAutoConfiguration
.
class
,
ServerPropertiesAutoConfiguration
.
class
);
this
.
context
.
refresh
();
SecurityProperties
security
=
this
.
context
.
getBean
(
SecurityProperties
.
class
);
AuthenticationManager
manager
=
this
.
context
.
getBean
(
AuthenticationManager
.
class
);
UsernamePasswordAuthenticationToken
token
=
new
UsernamePasswordAuthenticationToken
(
security
.
getUser
().
getName
(),
security
.
getUser
().
getPassword
());
assertNotNull
(
manager
.
authenticate
(
token
));
}
@Test
public
void
testCustomAuthenticationDoesNotAuthenticateWithBootSecurityUser
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigWebApplicationContext
();
this
.
context
.
setServletContext
(
new
MockServletContext
());
this
.
context
.
register
(
AuthenticationManagerCustomizer
.
class
,
SecurityAutoConfiguration
.
class
,
ServerPropertiesAutoConfiguration
.
class
);
this
.
context
.
refresh
();
SecurityProperties
security
=
this
.
context
.
getBean
(
SecurityProperties
.
class
);
AuthenticationManager
manager
=
this
.
context
.
getBean
(
AuthenticationManager
.
class
);
UsernamePasswordAuthenticationToken
token
=
new
UsernamePasswordAuthenticationToken
(
security
.
getUser
().
getName
(),
security
.
getUser
().
getPassword
());
try
{
manager
.
authenticate
(
token
);
fail
(
"Expected Exception"
);
}
catch
(
AuthenticationException
success
)
{
}
token
=
new
UsernamePasswordAuthenticationToken
(
"foo"
,
"bar"
);
assertNotNull
(
manager
.
authenticate
(
token
));
}
private
static
final
class
AuthenticationListener
implements
ApplicationListener
<
AbstractAuthenticationEvent
>
{
private
ApplicationEvent
event
;
@Override
public
void
onApplicationEvent
(
AbstractAuthenticationEvent
event
)
{
this
.
event
=
event
;
}
}
@Configuration
@Configuration
@TestAutoConfigurationPackage
(
City
.
class
)
@TestAutoConfigurationPackage
(
City
.
class
)
protected
static
class
EntityConfiguration
{
protected
static
class
EntityConfiguration
{
...
...
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