(十二)springboot电子商务商城之Eureka注册中心开启密码认证

爱吃甜食的少女 2021-04-08 10:07:15
Spring Boot Spring Security Eureka


Eureka 自带了一个 Web 的管理页面,方便我们查询注册到上面的实例信息,但是有一个问题:如果在实际使用中,注册中心地址有公网 IP 的话,必然能直接访问到,这样是不安全的。所以我们需要对 Eureka 进行改造,加上权限认证来保证安全性。

改造我们的 eureka-server,通过集成 Spring-Security 来进行安全认证。

在 pom.xml 中添加 Spring-Security 的依赖包,代码如下所示。

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

然后在 application.properties 中加上认证的配置信息:

spring.security.user.name=yinjihuan #用户名
spring.security.user.password=123456 #密码

增加 Security 配置类:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 关闭csrf
http.csrf().disable();
// 支持httpBasic
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}

重新启动注册中心,访问 http://localhost:8761/,此时浏览器会提示你输入用户名和密码,输入正确后才能继续访问 Eureka 提供的管理页面。

在 Eureka 开启认证后,客户端注册的配置也要加上认证的用户名和密码信息:

eureka.client.serviceUrl.defaultZone=http://zhangsan:123456@localhost:8761/eureka/

推荐布式微服务商城

版权声明
本文为[爱吃甜食的少女]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/5024958/blog/5011698

  1. Zookeeper分布式锁?
  2. Zookeeper distributed lock?
  3. 嵌入式Linux_Framebuffer_03点阵显示ASCII字符串
  4. 嵌入式Linux_Framebuffer_02字符编码
  5. Embedded Linux_ Framebuffer_ 03 dot matrix display ascii string
  6. Embedded Linux_ Framebuffer_ 02 character encoding
  7. Looking forward to new product launch of Xiaomi in spring CNMO takes you to see 11 new products in advance
  8. An inventory of the commonly used garbage collectors in Java
  9. Why is it so easy to get started with HBase?
  10. Implementation of PRC framework based on netty
  11. 2021 Java back end engineer must know knowledge - (Dubbo, distributed RPC framework)
  12. 关于spring advisor和元数据 同时来管理事务的问题
  13. How to manage transactions with spring advisor and metadata at the same time
  14. 使用Playwright对Java API实现自动视觉测试 - applitools
  15. Using playwright to implement automatic visual testing for Java API - applitools
  16. Dubbo和Spring cloud、Istio对比图
  17. Comparison of Dubbo with spring cloud and istio
  18. HttpServletRequest、通过request获得请求头、请求体等、解决中文乱码等问题
  19. Mybatis学习笔记-一对一,一对多,多对多关联查询
  20. Mybatis学习笔记-基本概念与操作
  21. HttpServletRequest, obtaining request header and request body through request, solving Chinese garbled code and other problems
  22. Mybatis learning notes - one to one, one to many, many to many association query
  23. Mybatis learning notes - basic concepts and operation
  24. Spring Cloud 升级之路 - 2020.0.x - 3. Undertow 的 accesslog 配置
  25. Spring cloud upgrade road - 2020.0. X - 3. Accesslog configuration of undertow
  26. 被Java培训机构坑骗后,我在这里找回了自信
  27. After being cheated by java training institutions, I found my confidence here
  28. Linux下安装Mysql出现的常见问题以及解决办法
  29. Common problems and solutions of installing MySQL under Linux
  30. java并发编程JUC第十二篇:AtomicInteger原子整型
  31. Java Concurrent Programming JUC Chapter 12: atomicinteger atomic integer
  32. 面经手册 · 第29篇《Spring IOC 特性有哪些,不会读不懂源码!》
  33. Chapter 29 "what are the features of spring IOC? I can't understand the source code! 》
  34. 浅析linux容器--Docker
  35. Analysis of Linux container -- docker
  36. 换种方法学操作系统,轻松入门Linux内核
  37. 浅析linux容器--Docker
  38. Another way to learn operating system, easy access to Linux kernel
  39. Analysis of Linux container -- docker
  40. 手摸手教你阅读和调试大型开源项目 ZooKeeper
  41. Hand in hand teaching you to read and debug large open source project zookeeper
  42. 基于SpringBoot 在线答题系统 含小程序!
  43. 10个优秀开源JavaScript模板引擎
  44. JavaScript基础知识及写法
  45. Based on springboot online answering system, including small procedures!
  46. 重磅!谷歌Fuchsia操作系统将支持运行Linux应用程序
  47. (四十) springcloud分布式商城之跟我学习SpringCloud-Gateway整合Eureka路由转发
  48. Spring IOC 特性有哪些,不会读不懂源码!
  49. 手摸手教你阅读和调试大型开源项目 ZooKeeper
  50. 10 excellent open source JavaScript template engines
  51. Basic knowledge and writing method of JavaScript
  52. win10安装mysql5.7
  53. Heavy weight! Google's Fuchsia operating system will support running Linux applications
  54. (40) Spring cloud distributed mall learn from me spring cloud gateway integrates Eureka routing and forwarding
  55. What are the spring IOC features? I can't understand the source code!
  56. Hand in hand teaching you to read and debug large open source project zookeeper
  57. Install mysql5.7 in win10
  58. SCIP:构造数据抽象--数据结构中队列与树的解释
  59. Spring Cloud+Nacos实现服务注册中心(Hoxton版本)
  60. rabbitmq-server的安装与升级