Spring Cloud注册中心Eureka设置访问权限并自定义鉴权页面
原文:https://blog.csdn.net/a823007573/article/details/88971496
使用Spring Security实现鉴权
1. 导入Spring Security的jar包。
<!--spring security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2. 在配置文件中添加Spring Security相关配置
spring:
security:
# 开启认证,Spring Cloud2.0后添加jar会自动集成并开启
# basic.enabled: true
# 用户名密码
user:
name: test
password: test
并修改eureka的注册中心地址,http://用户名:密码@主机:端口/eureka/
# 注册中心地址
serviceUrl.defaultZone: http://${security.user.name}:${spring.security.user.password}@${spring.eureka.instance.hostname}:${server.port}/eureka/
每个eureka client端都要修改serviceUrl.defaultZone的地址,加上用户名密码,不然客户端都连不上eureka server了。
3. 自定义Spring Security的鉴权页面
首先准备好自定义的页面,并使用spring boot推荐的thymeleaf进行HTML页面的管理。
导包:
<!--thymeleaf模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
添加一下thymeleaf配置:
#外一层为spring:
thymeleaf:
# 指定模板位置
prefix: classpath:/views/
mode: HTML5
通过继承WebSecurityConfigurerAdapter来进行自定义页面的配置:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()//对请求进行鉴权
.antMatchers("/login")//登录页面不鉴权
.permitAll();
http.formLogin()
.loginPage("/login")//登录页面
.failureUrl("/login?error")//鉴权失败的页面
.permitAll();
http.csrf().disable();
super.configure(http);
}
}
添加登录控制器用于跳转到登录页面:
/**
* 跳转到登录页
* @return
*/
@GetMapping("/login")
public String toLogin(String error, Model model) {
//利用error来判断是否登录出错,实质上没有值,对应设置的failureUrl
if (error != null) {
model.addAttribute("errMsg", "用户名或密码错误!");
}
return "/login";
}
Spring Cloud注册中心Eureka设置访问权限并自定义鉴权页面的更多相关文章
- spring cloud 注册中心--eureka注册与发现
本文详细介绍spring cloud微服务的默认注册中心--eureka注册与发现.开发环境需要Windows系统.jdk和intellij idea.与zookeeper注册中心相比,eureka不 ...
- Spring Cloud 注册中心Eureka
一.简介 最近在看Spring Cloud微服务,接下来的时间和大家一起分享我所看到的,公司现在用的是dubbo ,之后有时间也去了解了解dubbo的源码.与dubbo相比较,Spring Cloud ...
- kubernetes部署spring cloud注册中心 Eureka
系统环境 java JDK 1.8 Docker 18.09.6 kubernetes 1.16 创建Eureka Server 1.Maven引入相应的jar 引入 SpringBoot 做基础框架 ...
- JAVA Spring Cloud 注册中心 Eureka 相关配置
转载至 https://www.cnblogs.com/fangfuhai/p/7070325.html Eureka客户端配置 1.RegistryFetchIntervalSecon ...
- Spring Cloud注册中心高可用搭建
Spring Cloud的注册中心可以由Eureka.Consul.Zookeeper.ETCD等来实现,这里推荐使用Spring Cloud Eureka来实现注册中心,它基于Netfilix的Eu ...
- spring cloud - 注册中心
服务注册与发现 这里我们会用到Spring Cloud Netflix,该项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用 ...
- Spring Cloud注册中心之Consul
Consul简介 Consul是HashiCorp公司使用Golang语言开发的一中多服务解决方案工具,相比于其他服务注册中心来说,Consul的功能更为强大,丰富,其中最基本的功能包含下面几点(翻译 ...
- Spring Cloud注册中心之Zookeeper
zookeeper可以作为分布式服务的注册中心 在服务端安装zookeeper 参考:https://www.cnblogs.com/conly/p/12267506.html 创建spring bo ...
- spring cloud 服务注册中心eureka高可用集群搭建
spring cloud 服务注册中心eureka高可用集群搭建 一,准备工作 eureka可以类比zookeeper,本文用三台机器搭建集群,也就是说要启动三个eureka注册中心 1 本文三台eu ...
随机推荐
- pose_graph的优化变量和优化函数
1.优化变量 优化变量就是vertex,也就是pose,pose-graph的优化目标就是,调整所有vertex的位置来尽量满足所有边的约束. 上述的还是表层的理解,深一步理解: pose-graph ...
- js追加html元素
jquery追加html代码,添加元素 .append() //新增仲裁申请人 $("."+inputName).append("<div class=\" ...
- 【Gamma】Scrum Meeting 8
前言 会议定点:大运村公寓 会议时间:2019/6/7 会议目的:分配任务,准备宣传 一.任务进度 组员 上周任务进度 下阶段任务 大娃 辅助做好引导录屏 优化辅助模型 二娃 撰写会议博客 撰写会议博 ...
- 【RabbitMQ学习之二】RabbitMQ四种交换机模式应用
环境 win7 rabbitmq-server-3.7.17 Erlang 22.1 一.概念1.队列队列用于临时存储消息和转发消息.队列类型有两种,即时队列和延时队列. 即时队列:队列中的消息会被立 ...
- html5 audio标签切换播放音乐的方法
html5 audio标签切换播放音乐的方法<pre><audio id="music1" preload loop="loop">&l ...
- python读取word中的段落、表、图+++++++++++Doc转换Docx
读取文本.图.表.解压信息 import docx import zipfile import os import shutil '''读取word中的文本''' def gettxt(): file ...
- Python 发送邮件 and 编辑Excel
记录一下Python 发送邮件的代码,这是半年前写的,不知道现在有什么类库的改动. 类库 import smtplib from email.mime.text import MIMEText fro ...
- .Net Core 基于 SnmpSharpNet 开发
SNMP简介(百度百科): SNMP 是专门设计用于在 IP 网络管理网络节点(服务器.工作站.路由器.交换机及HUBS等)的一种标准协议,它是一种应用层协议. SNMP 使网络管理员能够管理网络效能 ...
- react 父组件调用子组件方法、子组件调用父组件方法
我们闲话不多说,直接上代码 // 父组件 import React, {Component} from 'react'; class Parents extends Component { const ...
- Aliyun发送短信接口调用方法
aliyun新版发送短信讲的不是很清晰,初次使用一堆dll不知道用哪个,以.net为例 申请SignName与Template_code请先申请,一般两个小时能通过 一.https://help.al ...