在spring cloud config中使用spring-boot-starter-security

本文介绍了如何在Spring Cloud Config中集成Spring-Boot-Starter-Security,详细阐述了在pom.xml中添加依赖、application.yml和bootstrap.yml的配置过程,以及启动类的标记。同时,提到在configclient配置端口和安全认证信息时的注意事项,如用户名或密码错误会导致项目启动失败。最后,通过创建一个Controller验证配置是否成功,结果显示密文表明配置已生效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先是在pom需要使用的到的插件:

<!--版本1.5.10-->
<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
		<version>1.4.1.RELEASE</version>
	</dependency>

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

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

然后在application.yml中配置:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/caidonglun/spring-cloud-config-server-test.git
#          username: 
#          password: 

server:
  port: 8056

#配置security的账号密码
security:
  basic:
    enabled: true
  user:
    name: user
    password: user

在启动类上面加上@EnableConfigServer表示为configserver。

然后是configclient的pom中所需jar

<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
		<version>1.4.1.RELEASE</version>
	</dependency>

首先在application.yml中配置一下port

server:
  port: 8057

然后在bootstrap.yml中配置的内容如下;

spring: 
  cloud: 
    config: 
      uri: http://user:user@localhost:8056
      username: user
      password: user
      profile: production #匹配的配置文件是production里面是一窜密文
      label: master  #当configserver的后端存储是Git时,默认是master
  application:
    name: foobar #会在git上找和name相同的配置文件,如果找不到,就是默认的配置文件。

如果把username或password其中一个写错,项目都无法启动,启动失败,说没有profile属性。

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'profile' in value "${profile}"

然后写一个controller

@RestController
public class ConfigClientController {

	@Value("${profile}")
	private String profile;
	
	@RequestMapping("/profile")
	public String getProfile() {
		return this.profile;
		
	}
	
}

得到结果:结果里面的密文是本身加密后的。

说明配置成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值