zno2

eureka server 开启认证

https://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/single/spring-cloud.html#_securing_the_eureka_server

https://docs.spring.io/spring-security/site/docs/5.2.15.RELEASE/reference/html5/

 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

 

复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication(exclude= {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}
复制代码

红字部分是要禁止自动配置,这里要实现的是根据serviceUrl 自动识别是否开启认证

 

复制代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

import cn.xs.ambi.bas.log.Log;
import cn.xs.ambi.bas.log.LogFactory;

@EnableWebSecurity
@ConditionalOnExpression("('${eureka.client.serviceUrl.defaultZone}').contains('@')")
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
    private final Log log = LogFactory.getLog(WebSecurityConfig.class);

    // http://zhangsan:123456@localhost:9002/eureka
    @Value("${eureka.client.serviceUrl.defaultZone}")
    private String serviceUrl;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        http.authorizeRequests().antMatchers("/ping").permitAll().antMatchers("/**").authenticated().and().httpBasic();
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        log.info("serviceUrl[{}]", serviceUrl);
        String pair = serviceUrl.split(",")[0].split("@")[0].split("//")[1];
        String username = pair.split(":")[0];
        String password = pair.split(":")[1];
        auth.inMemoryAuthentication()
                .passwordEncoder(org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance())
                .withUser(username).password(password).roles("USER");
    }
}
复制代码

 

注意心跳要放前面,其他都需要认证

关键代码spel表达式,这里取到属性值后判断是否是认证格式

@ConditionalOnExpression("('${eureka.client.serviceUrl.defaultZone}').contains('@')")

 

Authenticating with the Eureka Server

HTTP basic authentication is automatically added to your eureka client if one of the eureka.client.serviceUrl.defaultZone URLs has credentials embedded in it (curl style, as follows: http://user:password@localhost:8761/eureka). For more complex needs, you can create a @Bean of type DiscoveryClientOptionalArgs and inject ClientFilter instances into it, all of which is applied to the calls from the client to the server.

[Note]

Because of a limitation in Eureka, it is not possible to support per-server basic auth credentials, so only the first set that are found is used.

 

 

之后额ureka client 注册时如果用户密码不正确是无法注册的

 

这种实现仅需配置apollo 通用eureka namespace ,只需改动 eureka.client.serviceUrl.defaultZone 就可以了

 

posted on   zno2  阅读(70)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理

导航

统计信息

点击右上角即可分享
微信分享提示