前言:在最近开发过程中 偶遇根据SpringBoot不同版本导致配置方式不一致等问题
通用:Eureka服务配置 Server端:
一、引入依赖 spring-boot-starter-security
<dependencies><!-- Eureka 声明依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- 使用 security 加密 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
</dependencies>
二、配置 application.yml
spring:security:user:name: xxxpassword: yyy
三、启动类声明 Eureka 注解
@SpringBootApplication(scanBasePackages = "config配置文件目录")
@EnableEurekaServer
public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);}
}
区别:
一、使用 SpringBoot 老版本:
配置如下:
JDK 1.8 SpringBoot 1.5.2.RELEASE SpringCloud 1.2.2.RELEASE
配置方式:继承 WebSecurityConfigurerAdapter 类 并重写 configure 方法(通过重写这个方法 可以重新自定义安全策略)
import org.springframework.context.annotation.Configuration;
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;@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {// 禁用 CSRFhttp.csrf().disable();// 如果需要禁用 CSRF,但又想确保安全性,可以考虑使用以下替代方案// http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());super.configure(http);}}
二、使用 SpringBoot 新版本
配置如下:
JDK 21 SpringBoot 3.4.1 SpringCloud 2024.0.0
配置方式:跟老版本不同的是 不需要去继承并重写 仅需单独注册一个Bean文件 通过表达式方式进行单独配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;@Configuration
public class WebSecurityConfig {@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.csrf(csrf -> csrf.disable()) // 禁用 CSRF.authorizeHttpRequests(authorize -> authorize.requestMatchers("/eureka/**").authenticated() // 对 Eureka 路径启用认证.anyRequest().permitAll() // 允许其他请求).httpBasic(httpBasic -> {}); // 新方式启用 HTTP Basicreturn http.build();}
}
测试:创建 Eureka Client 端 进行服务注册(Gateway举例)
一、引入依赖 spring-boot-starter-security
<dependencies><!-- Spring Cloud Gateway --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- Eureka Client --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- Spring Boot Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency>
</dependencies>
二、配置 application.yml(核心配置:defaultZone: http://设置的账号:设置的密码@localhost:60001/eureka/)
server:port: 60003
spring:application:name: xxxx-gateway # 服务名,将在 Eureka 中注册cloud:gateway:discovery:locator:enabled: true # 启用动态路由lower-case-service-id: trueglobal-cors: # 配置跨域cors-configurations:'[/**]':allowedOrigins: "*"allowedMethods:- GET- POST- PUT- DELETEallowedHeaders: "*"allowCredentials: trueroutes:- id: xxx-baseuri: lb://XXX-BASE # 使用服务名进行负载均衡访问predicates:- Path=/base/**eureka:client:service-url:defaultZone: http://xxx:yyy@localhost:60001/eureka/ # Eureka 服务注册中心地址register-with-eureka: true # 是否注册到 Eurekafetch-registry: true # 是否从 Eureka 拉取注册表instance:prefer-ip-address: true # 使用 IP 注册logging:level:org.springframework.cloud.gateway: DEBUG
三、启动类声明注解
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}
}
需要特别注意报错原因:
JDK、Springboot、SpringCloud 版本不兼容 启动报错 解决方式:https://spring.io/projects/spring-cloud#learn 在其中查看对应版本信息 账户密码设置错误 Client端配置错误 举例:原本配置仅用 defaultZone: http://localhost:60001/eureka/ 既可 而如果使用 Eureka加密后 则需要 defaultZone: http://xxx:yyy@localhost:60001/eureka/ 区别为 xxx:yyy@(xxx为账户 yyy为密码 中间需要使用:
冒号隔开 并在输入完成后 使用@
符号进行确认) 未配置 Config 类
成功案例如下:启动Eureka和Gateway后 通过浏览器方式进入 http://localhost:60001/eureka/ 后发现 Client端 成功注册既可