-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Enable login authentication for eureka #4663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,9 +16,16 @@ | |
| */ | ||
| package com.ctrip.framework.apollo.configservice; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
| import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
|
|
||
| /** | ||
| * Start Eureka Server annotations according to configuration | ||
|
|
@@ -29,4 +36,43 @@ | |
| @EnableEurekaServer | ||
| @ConditionalOnProperty(name = "apollo.eureka.server.enabled", havingValue = "true", matchIfMissing = true) | ||
| public class ConfigServerEurekaServerConfigure { | ||
|
|
||
| @Order(99) | ||
| @Configuration | ||
| static class EurekaServerSecurityConfigurer extends WebSecurityConfigurerAdapter { | ||
|
|
||
| private static final String EUREKA_ROLE = "EUREKA"; | ||
|
|
||
| @Value("${apollo.eureka.server.security.enabled:false}") | ||
| private boolean eurekaSecurityEnabled; | ||
| @Value("${apollo.eureka.server.security.username:}") | ||
| private String username; | ||
| @Value("${apollo.eureka.server.security.password:}") | ||
| private String password; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about let user can config eureka's username and password in configdb?
but the password will save in configdb
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good idea! Storing the username/password in the DB makes it consistent among multiple config services.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Anilople I changed the implementation a little and the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How it work?... Are there some methods read data the from configdb?...
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works. When I change ServerConfig in configdb then try to access http://localhost:8080/eureka/apps/APOLLO-CONFIGSERVICE it need auth as expect. |
||
|
|
||
| @Override | ||
| protected void configure(HttpSecurity http) throws Exception { | ||
| http.csrf().disable(); | ||
| http.httpBasic(); | ||
| if (eurekaSecurityEnabled) { | ||
| http.authorizeRequests() | ||
| .antMatchers("/eureka/apps/**", "/eureka/instances/**", "/eureka/peerreplication/**") | ||
| .hasRole(EUREKA_ROLE) | ||
| .antMatchers("/**").permitAll(); | ||
| } | ||
| } | ||
|
|
||
| @Autowired | ||
| public void configureEurekaUser(AuthenticationManagerBuilder auth) throws Exception { | ||
| if (!eurekaSecurityEnabled) { | ||
| return; | ||
| } | ||
| InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> configurer = auth | ||
| .getConfigurer(InMemoryUserDetailsManagerConfigurer.class); | ||
| if (configurer == null) { | ||
| configurer = auth.inMemoryAuthentication(); | ||
| } | ||
| configurer.withUser(username).password(password).roles(EUREKA_ROLE); | ||
| } | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.