SpringSecurity认证和授权

简介: 目前,我们的测试环境,是谁都可以访问的,我们使用 Spring Security 增加上认证和授权的功能

认证和授权
目前,我们的测试环境,是谁都可以访问的,我们使用 Spring Security 增加上认证和授权的功能

1、引入 Spring Security 模块

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、编写 Spring Security 配置类

参考官网:https://spring.io/projects/spring-security

查看我们自己项目中的版本,找到对应的帮助文档:

https://docs.spring.io/spring-security/site/docs/5.3.0.RELEASE/reference/html5 #servlet-applications 8.16.4
1673574756868.png

3、编写基础配置类

package com.kuang.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity // 开启WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {

   @Override
   protected void configure(HttpSecurity http) throws Exception {
       
  }
}

4、定制请求的授权规则

@Override
protected void configure(HttpSecurity http) throws Exception {
   // 定制请求的授权规则
   // 首页所有人可以访问
   http.authorizeRequests().antMatchers("/").permitAll()
  .antMatchers("/level1/**").hasRole("vip1")
  .antMatchers("/level2/**").hasRole("vip2")
  .antMatchers("/level3/**").hasRole("vip3");
}

5、测试一下:发现除了首页都进不去了!因为我们目前没有登录的角色,因为请求需要登录的角色拥有对应的权限才可以!

6、在configure()方法中加入以下配置,开启自动配置的登录功能!

// 开启自动配置的登录功能
// /login 请求来到登录页
// /login?error 重定向到这里表示登录失败
http.formLogin();

7、测试一下:发现,没有权限的时候,会跳转到登录的页面!

1673574768223.png

8、查看刚才登录页的注释信息;

我们可以定义认证规则,重写configure(AuthenticationManagerBuilder auth)方法

//定义认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   
   //在内存中定义,也可以在jdbc中去拿....
   auth.inMemoryAuthentication()
          .withUser("kuangshen").password("123456").roles("vip2","vip3")
          .and()
          .withUser("root").password("123456").roles("vip1","vip2","vip3")
          .and()
          .withUser("guest").password("123456").roles("vip1","vip2");
}

9、测试,我们可以使用这些账号登录进行测试!发现会报错!

There is no PasswordEncoder mapped for the id “null”
1673574778623.png

10、原因,我们要将前端传过来的密码进行某种方式加密,否则就无法登录,修改代码

//定义认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   //在内存中定义,也可以在jdbc中去拿....
   //Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
   //要想我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密
   //spring security 官方推荐的是使用bcrypt加密方式。
   
   auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
          .withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
          .and()
          .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
          .and()
          .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2");
}

11、测试,发现,登录成功,并且每个角色只能访问自己认证下的规则!搞定

相关文章
|
JavaScript 前端开发 Java
10分钟邮箱API发送邮件的操作步骤
使用10分钟邮箱API发送邮件涉及6步:获取API密钥、导入相应库、设置请求参数、发送API请求、处理响应及检查收件箱。适用于自动化邮件发送,如测试和临时通知。[≤240字符]
|
8月前
|
负载均衡 监控 安全
优化HTTP代理IP安全稳定性的关键要点
随着科技发展,越来越多企业依赖HTTP代理IP。为确保其安全稳定,建议采取以下措施:选择可靠服务商、使用HTTPS加密、定期更换IP、监控可用性、设置访问控制、使用负载均衡、配置防火墙、定期更新维护及用户教育。这些方法能有效提升代理IP的安全性和稳定性。
354 59
|
安全 前端开发 网络安全
网络空间安全之一个WH的超前沿全栈技术深入学习之路(2-1):渗透测试行业术语扫盲)作者——LJS
网络空间安全之一个WH的超前沿全栈技术深入学习之路(2-1):渗透测试行业术语扫盲)作者——LJS
|
人工智能 搜索推荐 API
AI智能体研发之路-工程篇(二):Dify智能体开发平台一键部署
AI智能体研发之路-工程篇(二):Dify智能体开发平台一键部署
3634 4
xlwings 复制一个excel的一列 到另一个excel中
xlwings 复制一个excel的一列 到另一个excel中
572 0
QtCreater增加自动添加注释的快捷代码
QtCreater增加自动添加注释的快捷代码
773 0
|
存储 分布式计算 DataWorks
阿里云大数据ACA及ACP复习题(291~300)
本人备考阿里云大数据考试时自行收集准备的题库,纯手工整理的,能够覆盖到今年7月份,应该是目前最新的,发成文章希望大家能一起学习,不要花冤枉钱去买题库背了,也希望大家能够顺利通关ACA和ACP考试。
|
存储 NoSQL Shell
InfluxDB的存储引擎演化过程
InfluxDB的存储引擎从LSM Tree,到mmap B+ Tree,再到TSM Tree。
6965 0
|
存储 Rust Shell
Rust 开发命令行工具(上)(二)
Rust 开发命令行工具(上)(二)
280 0
|
XML Java 调度
Spring定时任务-任务执行和调度—官方原版
Spring定时任务-任务执行和调度—官方原版
318 0