目录
项目搭建参考https://blog.csdn.net/qq_40977118/article/details/104738485
1. 引入jar包
<!--用户认证-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2. 关闭 csrf 验证
package com.spring.fisher;
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;
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//关闭csrf
http.csrf().disable();
//开启认证:URL格式登录必须是httpBasic
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
3. application.properties 配置
- 开启 basic 校验,设置登录用户名密码
#开启 basic 校验,设置登录用户名密码
security.basic.enabled=true
spring.security.user.name=root
spring.security.user.password=root
4. Eureka 客户端改造
- 跟 eureka 连接的时候要带上用户名密码
eureka.client.serviceUrl.defaultZone=http://root:root@localhost:8763/eureka/
5. 启动eureka服务端
6. 访问http://localhost:8763/,输入用户名密码
7. 启动micro-order与micro-web
8. 发起请求,查看打印