Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-boot-admin
Project
Overview
Details
Activity
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
openSource
spring-boot-admin
Commits
da6cce71
Commit
da6cce71
authored
Nov 17, 2014
by
Johannes Stelzer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support http-basic authorization in registration.
This allows you to protect your spring-boot-admin rest api. Supporting basic auth on management-endpoints needs more work.
parent
7eb4bafc
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
109 additions
and
11 deletions
+109
-11
LogfileMvcEndpoint.java
...de/codecentric/boot/admin/actuate/LogfileMvcEndpoint.java
+1
-0
AdminClientProperties.java
.../codecentric/boot/admin/config/AdminClientProperties.java
+2
-2
AdminProperties.java
...ava/de/codecentric/boot/admin/config/AdminProperties.java
+29
-3
SpringBootAdminClientAutoConfiguration.java
.../admin/config/SpringBootAdminClientAutoConfiguration.java
+15
-6
BasicAuthHttpRequestInterceptor.java
...ntric/boot/admin/web/BasicAuthHttpRequestInterceptor.java
+29
-0
BasicAuthHttpRequestInterceptorTest.java
...c/boot/admin/web/BasicAuthHttpRequestInterceptorTest.java
+33
-0
No files found.
spring-boot-starter-admin-client/src/main/java/de/codecentric/boot/admin/actuate/LogfileMvcEndpoint.java
View file @
da6cce71
...
...
@@ -102,6 +102,7 @@ public class LogfileMvcEndpoint implements MvcEndpoint {
Resource
file
=
new
FileSystemResource
(
logfile
);
response
.
setContentType
(
MediaType
.
TEXT_PLAIN_VALUE
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename=\""
+
file
.
getFilename
()
+
"\""
);
FileCopyUtils
.
copy
(
file
.
getInputStream
(),
response
.
getOutputStream
());
}
...
...
spring-boot-starter-admin-client/src/main/java/de/codecentric/boot/admin/config/AdminClientProperties.java
View file @
da6cce71
...
...
@@ -18,7 +18,7 @@ package de.codecentric.boot.admin.config;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
@ConfigurationProperties
(
prefix
=
"spring.boot.admin.client"
)
@ConfigurationProperties
(
prefix
=
"spring.boot.admin.client"
,
ignoreUnknownFields
=
false
)
public
class
AdminClientProperties
{
@Value
(
"http://#{T(java.net.InetAddress).localHost.canonicalHostName}:${server.port:${management.port:8080}}${management.context-path:/}"
)
...
...
@@ -29,7 +29,7 @@ public class AdminClientProperties {
/**
* @return Client-management-URL to register with. Can be overriden in case the
* reachable URL is different (e.g. Docker). Must be unique in registry.
*
reachable URL is different (e.g. Docker). Must be unique in registry.
*/
public
String
getUrl
()
{
return
url
;
...
...
spring-boot-starter-admin-client/src/main/java/de/codecentric/boot/admin/config/AdminProperties.java
View file @
da6cce71
...
...
@@ -26,19 +26,22 @@ public class AdminProperties {
private
int
period
=
10000
;
private
String
username
;
private
String
password
;
public
void
setUrl
(
String
url
)
{
this
.
url
=
url
;
}
/**
*
*
* @return the Spring Boot Admin Server's url.
*/
public
String
getUrl
()
{
return
url
;
}
/**
* @return the Spring Boot Admin Server's context path.
*/
...
...
@@ -51,7 +54,7 @@ public class AdminProperties {
}
/**
*
*
* @return the time interval (in ms) the registration is repeated.
*/
public
int
getPeriod
()
{
...
...
@@ -61,4 +64,27 @@ public class AdminProperties {
public
void
setPeriod
(
int
period
)
{
this
.
period
=
period
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
/**
* @return username for basic authentication .
*/
public
String
getUsername
()
{
return
username
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
/**
*
* @return password for basic authentication.
*/
public
String
getPassword
()
{
return
password
;
}
}
spring-boot-starter-admin-client/src/main/java/de/codecentric/boot/admin/config/SpringBootAdminClientAutoConfiguration.java
View file @
da6cce71
...
...
@@ -15,17 +15,22 @@
*/
package
de
.
codecentric
.
boot
.
admin
.
config
;
import
java.util.Arrays
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.client.ClientHttpRequestInterceptor
;
import
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
;
import
org.springframework.scheduling.config.ScheduledTaskRegistrar
;
import
org.springframework.web.client.RestTemplate
;
import
de.codecentric.boot.admin.actuate.LogfileMvcEndpoint
;
import
de.codecentric.boot.admin.services.SpringBootAdminRegistrator
;
import
de.codecentric.boot.admin.web.BasicAuthHttpRequestInterceptor
;
import
de.codecentric.boot.admin.web.SimpleCORSFilter
;
/**
...
...
@@ -41,16 +46,20 @@ public class SpringBootAdminClientAutoConfiguration {
* Task that registers the application at the spring-boot-admin application.
*/
@Bean
public
SpringBootAdminRegistrator
registrator
(
AdminProperties
adminProps
,
AdminClientProperties
clientProps
)
{
return
new
SpringBootAdminRegistrator
(
restTemplate
(
),
adminProps
,
clientProps
);
@ConditionalOnMissingBean
public
SpringBootAdminRegistrator
registrator
(
AdminProperties
adminProps
,
AdminClientProperties
clientProps
)
{
return
new
SpringBootAdminRegistrator
(
createRestTemplate
(
adminProps
),
adminProps
,
clientProps
);
}
@Bean
public
RestTemplate
restTemplate
()
{
protected
RestTemplate
createRestTemplate
(
AdminProperties
adminProps
)
{
RestTemplate
template
=
new
RestTemplate
();
template
.
getMessageConverters
().
add
(
new
MappingJackson2HttpMessageConverter
());
if
(
adminProps
.
getUsername
()
!=
null
)
{
template
.
setInterceptors
(
Arrays
.<
ClientHttpRequestInterceptor
>
asList
(
new
BasicAuthHttpRequestInterceptor
(
adminProps
.
getUsername
(),
adminProps
.
getPassword
())));
}
return
template
;
}
...
...
spring-boot-starter-admin-client/src/main/java/de/codecentric/boot/admin/web/BasicAuthHttpRequestInterceptor.java
0 → 100644
View file @
da6cce71
package
de
.
codecentric
.
boot
.
admin
.
web
;
import
java.io.IOException
;
import
java.nio.charset.StandardCharsets
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.http.client.ClientHttpRequestExecution
;
import
org.springframework.http.client.ClientHttpRequestInterceptor
;
import
org.springframework.http.client.ClientHttpResponse
;
import
com.fasterxml.jackson.core.Base64Variants
;
public
class
BasicAuthHttpRequestInterceptor
implements
ClientHttpRequestInterceptor
{
private
final
String
encodedAuth
;
public
BasicAuthHttpRequestInterceptor
(
String
username
,
String
password
)
{
String
auth
=
username
+
":"
+
password
;
encodedAuth
=
"Basic "
+
Base64Variants
.
MIME_NO_LINEFEEDS
.
encode
(
auth
.
getBytes
(
StandardCharsets
.
US_ASCII
));
}
@Override
public
ClientHttpResponse
intercept
(
HttpRequest
request
,
byte
[]
body
,
ClientHttpRequestExecution
execution
)
throws
IOException
{
request
.
getHeaders
().
add
(
"Authorization"
,
encodedAuth
);
return
execution
.
execute
(
request
,
body
);
}
}
spring-boot-starter-admin-client/src/test/java/de/codecentric/boot/admin/web/BasicAuthHttpRequestInterceptorTest.java
0 → 100644
View file @
da6cce71
package
de
.
codecentric
.
boot
.
admin
.
web
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
java.io.IOException
;
import
java.util.Collections
;
import
org.junit.Test
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.http.client.ClientHttpRequestExecution
;
import
org.springframework.http.client.ClientHttpResponse
;
import
org.springframework.mock.http.client.MockClientHttpRequest
;
import
de.codecentric.boot.admin.web.BasicAuthHttpRequestInterceptor
;
public
class
BasicAuthHttpRequestInterceptorTest
{
@Test
public
void
test
()
throws
IOException
{
BasicAuthHttpRequestInterceptor
interceptor
=
new
BasicAuthHttpRequestInterceptor
(
"admin"
,
"secret"
);
HttpRequest
request
=
new
MockClientHttpRequest
();
interceptor
.
intercept
(
request
,
(
byte
[])
null
,
new
ClientHttpRequestExecution
()
{
@Override
public
ClientHttpResponse
execute
(
HttpRequest
paramHttpRequest
,
byte
[]
paramArrayOfByte
)
throws
IOException
{
return
null
;
}
});
assertEquals
(
Collections
.
singletonList
(
"Basic YWRtaW46c2VjcmV0"
),
request
.
getHeaders
().
get
(
"Authorization"
));
}
}
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