|
16 | 16 | */ |
17 | 17 | package com.ctrip.framework.apollo.configservice; |
18 | 18 |
|
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.beans.factory.annotation.Value; |
19 | 21 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
20 | 22 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; |
21 | 23 | import org.springframework.context.annotation.Configuration; |
| 24 | +import org.springframework.core.annotation.Order; |
| 25 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 26 | +import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer; |
| 27 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 28 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
22 | 29 |
|
23 | 30 | /** |
24 | 31 | * Start Eureka Server annotations according to configuration |
|
29 | 36 | @EnableEurekaServer |
30 | 37 | @ConditionalOnProperty(name = "apollo.eureka.server.enabled", havingValue = "true", matchIfMissing = true) |
31 | 38 | public class ConfigServerEurekaServerConfigure { |
| 39 | + |
| 40 | + @Order(99) |
| 41 | + @Configuration |
| 42 | + static class EurekaServerSecurityConfigurer extends WebSecurityConfigurerAdapter { |
| 43 | + |
| 44 | + private static final String EUREKA_ROLE = "EUREKA"; |
| 45 | + |
| 46 | + @Value("${apollo.eureka.server.security.enabled:false}") |
| 47 | + private boolean eurekaSecurityEnabled; |
| 48 | + @Value("${apollo.eureka.server.security.username:}") |
| 49 | + private String username; |
| 50 | + @Value("${apollo.eureka.server.security.password:}") |
| 51 | + private String password; |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void configure(HttpSecurity http) throws Exception { |
| 55 | + http.csrf().disable(); |
| 56 | + http.httpBasic(); |
| 57 | + if (eurekaSecurityEnabled) { |
| 58 | + http.authorizeRequests() |
| 59 | + .antMatchers("/eureka/apps/**", "/eureka/instances/**", "/eureka/peerreplication/**") |
| 60 | + .hasRole(EUREKA_ROLE) |
| 61 | + .antMatchers("/**").permitAll(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Autowired |
| 66 | + public void configureEurekaUser(AuthenticationManagerBuilder auth) throws Exception { |
| 67 | + if (!eurekaSecurityEnabled) { |
| 68 | + return; |
| 69 | + } |
| 70 | + InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> configurer = auth |
| 71 | + .getConfigurer(InMemoryUserDetailsManagerConfigurer.class); |
| 72 | + if (configurer == null) { |
| 73 | + configurer = auth.inMemoryAuthentication(); |
| 74 | + } |
| 75 | + configurer.withUser(username).password(password).roles(EUREKA_ROLE); |
| 76 | + } |
| 77 | + } |
32 | 78 | } |
0 commit comments