关于使用springcloud项目中引入spring-security的问题
1、在集成springcloud项目时,eureka为了安全起见(为了不让所有人能随意访问eureka,就添加了安全认证,pom文件添加了如下的依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、然后再eureka服务里面添加用户名和密码,如下
server:
port: 8761
spring:
application:
name: eureka-server
security:
basic:
enabled: true #开启基于http basic的认证
user:
name: admin #配置登录的账号是admin
password: admin #配置登录的密码是admin
eureka:
client:
#表示是否将自己注册到Eureka Server,默认为true,由于当前应用就是Eureka Server ,故而设为false( 换句话说禁止自己当作服务注册)
registerWithEureka: false
#表示是否从Eureka Server获取注册信息,默认为true.因为这是一个单节点的Eureka Server,不需要同步其他的Eureka Server的数据,故而设置为 false
fetchRegistry: false
#设置为Eureka Server的交互地址,查询服务和注册服务都需要这个地址。默认是http://localhost:8671/eureka/,多个地址用都逗号隔开
serviceUrl:
defaultZone: http://admin:admin@localhost:8761/eureka/
#defaultZone: http://localhost:8761/eureka/
3、同理,在各个微服务模块均添加上用户名密码。重启服务发现各个微服务模块不能注册到eurea注册中心里面去,此外用postman访问微服务模块的controller接口,接口返回401,权限不允许。去掉pom里面的spring-security及用户名密码设置,才能恢复正常。(目前没有找到原因,等找到原因后重新更新该博客)