原文: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. shell脚本显示字体颜色

    shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要使用参数-e 格式如下: echo -e "\033[字背景颜色:文字颜色m字符串\033[0m" 例如: ec ...

  2. glyphicons-halflings-regular.ttf:1 Failed to load resource: net::ERR_FILE_NOT_FOUND

    下载替换 https://gitlab.com/mailman/mailman-website/tree/a97d6b4c5b29594004e3855f1ab1222449d0c211/conten ...

  3. python3 mqtt 添加用户名以及密码

    import paho.mqtt.client as mqtt client = mqtt.Client(client_id, transport='tcp') client.username_pw_ ...

  4. 在Linux上使用mssql-conf工具配置SQL Server 2017

    mssql-conf是在Linux上安装SQL Server 2017后的一个配置脚本.你可以使用这个实用工具设置以下参数: Agent 启用SQL Server代理 Collation 设置一个新的 ...

  5. JS中 (function(){...})()立即执行函数

    (function(){...})() (function(){...}()) 这是两种js立即执行函数的常见写法. 基本概念: 函数声明:function fname(){...}; 使用funct ...

  6. SQL2008R2下数据库修复一例

    某天访问某个数据库的时候,系统报错.连上去看了一下,服务器是SQL2008R2.由于有上次修复的经验,先使用DBCC查看数据库情况. DBCC的返回: XXXXXXXXXXX发生异常数据库 ID 7, ...

  7. 解决mybatis中#{}导致的The error may involve defaultParameterMap的问题

    今天想实现给指定表插入数据,出现了 ### Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an ...

  8. CentOS安装Hive

    1.环境和软件准备: hive版本:apache-hive-2.3.6-bin.tar.gz,下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hive ...

  9. 用 LinkedList 实现一个 java.util.Stack 栈

    用 LinkedList 实现一个 java.util.Stack 栈 import java.util.LinkedList; public class Stack<E> { priva ...

  10. 028 ElasticSearch----全文检索技术03---基础知识详解01-IK分词器和映射

    1.IK分词器 (1)安装 使用IK分词器可以实现对中文分词的效果.下载IK分词器:(Github地址:https://github.com/medcl/elasticsearch-analysis- ...