原文: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设置访问权限并自定义鉴权页面的更多相关文章

  1. spring cloud 注册中心--eureka注册与发现

    本文详细介绍spring cloud微服务的默认注册中心--eureka注册与发现.开发环境需要Windows系统.jdk和intellij idea.与zookeeper注册中心相比,eureka不 ...

  2. Spring Cloud 注册中心Eureka

    一.简介 最近在看Spring Cloud微服务,接下来的时间和大家一起分享我所看到的,公司现在用的是dubbo ,之后有时间也去了解了解dubbo的源码.与dubbo相比较,Spring Cloud ...

  3. kubernetes部署spring cloud注册中心 Eureka

    系统环境 java JDK 1.8 Docker 18.09.6 kubernetes 1.16 创建Eureka Server 1.Maven引入相应的jar 引入 SpringBoot 做基础框架 ...

  4. JAVA Spring Cloud 注册中心 Eureka 相关配置

    转载至  https://www.cnblogs.com/fangfuhai/p/7070325.html Eureka客户端配置       1.RegistryFetchIntervalSecon ...

  5. Spring Cloud注册中心高可用搭建

    Spring Cloud的注册中心可以由Eureka.Consul.Zookeeper.ETCD等来实现,这里推荐使用Spring Cloud Eureka来实现注册中心,它基于Netfilix的Eu ...

  6. spring cloud - 注册中心

    服务注册与发现 这里我们会用到Spring Cloud Netflix,该项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用 ...

  7. Spring Cloud注册中心之Consul

    Consul简介 Consul是HashiCorp公司使用Golang语言开发的一中多服务解决方案工具,相比于其他服务注册中心来说,Consul的功能更为强大,丰富,其中最基本的功能包含下面几点(翻译 ...

  8. Spring Cloud注册中心之Zookeeper

    zookeeper可以作为分布式服务的注册中心 在服务端安装zookeeper 参考:https://www.cnblogs.com/conly/p/12267506.html 创建spring bo ...

  9. spring cloud 服务注册中心eureka高可用集群搭建

    spring cloud 服务注册中心eureka高可用集群搭建 一,准备工作 eureka可以类比zookeeper,本文用三台机器搭建集群,也就是说要启动三个eureka注册中心 1 本文三台eu ...

随机推荐

  1. Linux stty命令

    stty是linux下改变和打印终端设置的常用命令. 一.参数: 1.打印终端行设置 -a,--all   以人可读的方式打印所有当前设置:-a参数比单独的stty命令输出的终端信息更详细 -g,-- ...

  2. Linux笔记本合上屏幕不待机

    Linux笔记本合上屏幕不待机[]# vim /etc/systemd/logind.conf# This file is part of systemd.## systemd is free sof ...

  3. Django RestFramework中UpdateAPIView类使用

    修改数据 from django.conf.urls import url from .api import workflow,workflowline urlpatterns = [ url(r'^ ...

  4. cad问题小百科 持续更新

    一些浩辰的问题移步去: 浩辰问题        (浩辰可能和桌子具有相同的问题,所以这篇你可能还是要看 cad2007遇到了这种情况 安装问题安装CAD出现C++2005问题的解决方法,出现此问题,原 ...

  5. A Philosophy of Software Design

    关于复杂性,尚无统一的定义,从不同的角度可以给出不同的答案.可以用数量来度量,比如芯片集成的电子器件越多越复杂(不一定对):按层次性[2]度量,复杂度在于层次的递归性和不可分解性.在信息论中,使用熵来 ...

  6. 小心!做 UI 自动化一定要跨过这些坑

    一 .引子 UI自动化,在移动互联网时代的今天,一直都是在各大测试社区最为火爆的一个TOPIC.甚至在测试同行面前一提起自动化,大家就会自然而然的问:“恩,你们是用的什么框架?appium?还是rob ...

  7. c++11 enum class用法

    使用过的一个enum class的用法:enum class EModel : int    {        Model1, Model2, Model3, Other = 4,    }; EMo ...

  8. pycharm社区办找不到View-->Tool Windows->DataBase的解决办法

    File->Settings->Plugins->Database Navigator 安装

  9. 适配器(Adapter)模式

    适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的一些其他名称:变压器模式.转换器模式.包装(Wrapper)模式.适 ...

  10. Markdown_word_chain_test2222

    # Flutter ![Flutter logo][]   [![Gitter Channel][]][Gitter badge] [![Build Status - Cirrus][]][Build ...