spring boot之security
上一节的时候,我们打开了springboot的端点,有一些数据是非常敏感的,比如/shutdown。
这一节,我们要给一些敏感信息加上权限控制。
spring boot本身的security模块就很好用,需要配置的东西很少,但是对于初学者而言,会有很多坑。
一、security配置
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
引入这个依赖之后,你再访问之前的接口,都访问不了了,需要填写用户名和密码,由于我们还没有做任何配置,这里用户名默认是user,密码在springboot启动的日志里(当然也可以自定义在application配置文件中)
这种默认的方式,无论如何是不太好用的,因此我们需要自定义自己的权限配置。
2.自定义security配置类
自定义的配置类没有几行代码,只有两个方法,先说第二个,我们初始化了一个用户user在内存中,密码是admin,角色是ADMIN,当然这些东西都是可以自己定义的。没有写在配置文件中,因为我觉得那样还是不太安全。
我们对/actuator开始的url访问要求有ADMIN权限,其他的随意访问。
经过这些配置,再访问接口的时候发现,/actuator/**都需要输账号密码了,其他的还是不需要,正是我们想要的结果。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/actuator/**").access("hasRole('ADMIN')")
.antMatchers("/**").permitAll();
super.configure(http);
} @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("admin")
.roles("ADMIN");
}
}
3.设置端点接口的前缀
spring boot 1.5.4的actuator端口都是基于根目录的,这样我们发现再进行权限控制的时候,写的很麻烦,在配置文件中可以给它加一个前缀
#开启actuator端点,增加前缀
management.security.enabled=false
management.context-path=/actuator
spring boot之security的更多相关文章
- Spring Boot 的 Security 安全控制
Spring Security 是一个强大且高度可定制的身份验证和访问控制框架,完全基于 Spring 的应用程序的标准,Spring Security 为基于 Java EE 的企业应用程序提供了一 ...
- spring boot + thymeleaf +security自定义规则 的简单使用
1.前言 以前开发一直使用 springMVC模式开发 ,前端页面常使用 JSP ,现在html5淘汰了 ,要么使用html ,要么使用vue , 现在使用spring boot ,有必要总结一下 ...
- Spring Boot 配置 Security 密码加密
依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...
- Spring Boot Security配置教程
1.简介 在本文中,我们将了解Spring Boot对spring Security的支持. 简而言之,我们将专注于默认Security配置以及如何在需要时禁用或自定义它. 2.默认Security设 ...
- How to use JDBC-Authentication of Spring Boot/Spring Security with Flyway
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- Spring Boot中使用 Spring Security 构建权限系统
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,为应用系统提供声明式的安全 ...
- spring boot系列03--spring security (基于数据库)登录和权限控制(下)
(接上篇) 后台 先说一下AuthConfig.java Spring Security的主要配置文件之一 AuthConfig 1 @Configuration 2 @EnableWebSecuri ...
- Spring Boot整合Spring Security
Spring Boot对于该家族的框架支持良好,但是当中本人作为小白配置还是有一点点的小问题,这里分享一下.这个项目是使用之前发布的Spring Boot会员管理系统重新改装,将之前filter登录验 ...
- 在Spring Boot中使用Spring Security实现权限控制
丢代码地址 https://gitee.com/a247292980/spring-security 再丢pom.xml <properties> <project.build.so ...
随机推荐
- JS事件处理函数中return false到底是什么东西
在<JS DOM编程艺术>一书中,用return false来阻止事件默认行为,可是js高程3里没有这种用法,那这到底是什么呢. 先看一下知乎的一个解释 就此问题,首先要纠正两个观点: 1 ...
- PS 滤镜算法— — 表面模糊
图像的表面模糊处理,其作用是在保留图像边缘的情况下,对图像的表面进行模糊处理.在对人物皮肤处理上,比高斯模糊更有效.因为高斯模糊在使人物皮肤光洁的同时,也将一些边缘特征如脸部的眉毛.嘴唇等给模糊了,不 ...
- Python3.7环境配置
1.官网下载 https://www.python.org/ 我这是3.7.0 for windows executable installer Download Windows x86 web-ba ...
- 【003】【Java虚拟机——对象死亡的判断】
对象死亡! 垃圾收集器在对堆进行回收前,首先要做的事情就是要确定这些对象之中哪些还"存活"着, 哪些已经"死去" (即不可能再被不论什么途径使用的对象). 1) 引用计 ...
- 简明Python3教程 19.附录 FLOSS
FLOSS Free/Libre and Open Source Software, in short, FLOSS is based on the concept of a community, w ...
- 回调函数实现类似QT中信号机制
1. 定义回调接口类: class UIcallBack { public: virtual void onAppActivated() = 0; virtual void onShowMore() ...
- “TNS-03505:无法解析名称”问题解决一例
1. 问题情境 开发人员,在windows新环境ORACLEclient.配置"tnsnames.ora"后,准备连接Linux环境的ORACLE数据库,使用tnsping报TN ...
- XF 进度条和指示器
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http:/ ...
- jquery 包裹标签
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- WPF:如何为程序添加splashScreen?
原文:WPF:如何为程序添加splashScreen? 大家是否还记得在Windows Forms程序中如何实现splashScreen吗?我们一般都会使用Microsoft.VisualBasic. ...