Spring Boot 配置 Security 密码加密
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
注入bean
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
@Bean
public BCryptPasswordEncoder encoding(){
return new BCryptPasswordEncoder();
}
}
安全配置类
authenticated()要求认证后才能访问。
如果用户没有认证的话,Spring Security的Filter将会捕获该请求,并将用户重定向到应用的登录页面。
/**
* 安全配置类
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//authorizeRequests:声明配置是权限配置
//antMatchers:路径
//permitAll:任何权限都可以访问,不需要身份认证
//anyRequest:任何请求
//authenticated:认证后才能访问
//and().csrf().disable():固定写法,表示csrf拦截失效
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
}
access(String) 如果给定的SpEL表达式计算结果为true,就允许访问
anonymous() 允许匿名用户访问
authenticated() 允许认证的用户进行访问
denyAll() 无条件拒绝所有访问
fullyAuthenticated() 如果用户是完整认证的话(不是通过Remember-me功能认证的),就允许访问
hasAuthority(String) 如果用户具备给定权限的话就允许访问
hasAnyAuthority(String…) 如果用户具备给定权限中的某一个的话,就允许访问
hasRole(String) 如果用户具备给定角色(用户组)的话,就允许访问
hasAnyRole(String…) 如果用户具有给定角色(用户组)中的一个的话,允许访问
hasIpAddress(String) 如果请求来自给定ip地址的话,就允许访问
not() 对其他访问结果求反
permitAll() 所有权限无条件允许访问
rememberMe() 如果用户是通过Remember-me功能认证的,就允许访问
密码加密与解密
@Autowired
private BCryptPasswordEncoder encoding;
public void add(Admin admin) {
//加密
admin.setPassword(encoding.encode(admin.getPassword()));
adminDao.save(admin);
}
encoding.matches(admin.getPassword(),sqlAdmain.getPassword())
Spring Boot 配置 Security 密码加密的更多相关文章
- spring boot配置druid连接池连接mysql
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Spring boot配置多个Redis数据源操作实例
原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...
- Spring Boot -- 配置切换指南
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- Spring Boot 配置优先级顺序
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- spring boot 配置注入
spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...
- spring boot配置springMVC拦截器
spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...
- spring boot配置mybatis和事务管理
spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...
- [转] Spring Boot配置多个DataSource
[From] https://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000 Sprin ...
- Spring boot 配置异步处理执行器
示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
随机推荐
- 基于SpringBoot的Swagger2快速入门
1. Springboot 集成 Swagger2 1.1 导入Swagger2 依赖 <!-- https://mvnrepository.com/artifact/io.springfox/ ...
- 牛客网多校训练第二场D Kth Minimum Clique
链接:https://ac.nowcoder.com/acm/contest/882/D来源:牛客网 Given a vertex-weighted graph with N vertices, fi ...
- Red and Black 模板题 /// BFS oj22063
题目大意: Description There is a rectangular room, covered with square tiles. Each tile is colored eithe ...
- springboot项目中使用设计模式一策略模式
策略模式: 使用常用,支付,之前做了微信支付,支付宝支付,然后另外一个同事写了一个银联支付,那么如果代码方法一起就会导致代码不是很好操作所以,采用策略模式进行,同事只需要写一个实现类,就可以了, 在协 ...
- Docker学习の更改Docker的目录
一.更改虚拟磁盘的目录 虚拟机的默认存储位置是C:\Users\Administrator\.docker\machine\machines ,后期docke镜像文件会不断增加,为了给系统盘减负,最好 ...
- vc枚举本机端口信息
关于查看本机端口信息,可能大多数人都知道在cmd下的netstat 命令,殊不知该命令在底层也是调用相关api来实现的,相关函数有:GetTcpTableGetExtendedTcpTableGetU ...
- sql (12) HAVING
HAVING 子句在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 新建表 StudentSS_id Grade Name phone1 98 小明 12345 ...
- java 堆排,优先级队列,归并排序
堆排 堆排是基于二叉树而得来的 例如:对一个数组 可以转为二叉树: 二叉树特性父节点为 i , 左叶子节点为2i+1:右叶子节点为2i+2; 步骤分解: 1. 先从第一个非叶子节点(即下 ...
- Java 多线程 - synchronized与Lock的区别
https://blog.csdn.net/qq_39521554/article/details/81130442 http://www.cnblogs.com/huangbw/p/8516024. ...
- flink提交文件出现java.io.IOException:unable to close file because the last block does not have enough number of replicas异常
当提交已经打包好的jar包时候,控制台出现以下的错误.