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、测试,发现,登录成功,并且每个角色只能访问自己认证下的规则!搞定

相关文章
|
安全 Java 网络安全
后端进阶之路——综述Spring Security认证,授权(一)
后端进阶之路——综述Spring Security认证,授权(一)
|
JavaScript 前端开发 Java
10分钟邮箱API发送邮件的操作步骤
使用10分钟邮箱API发送邮件涉及6步:获取API密钥、导入相应库、设置请求参数、发送API请求、处理响应及检查收件箱。适用于自动化邮件发送,如测试和临时通知。[≤240字符]
|
5月前
|
关系型数据库 分布式数据库 数据库
又一国赛来啦!第2届PolarDB数据库创新设计赛开放报名!|全国普通高校学科竞赛排行榜入选赛事等你来战
「2025全国大学生计算机系统能力大赛——第2届PolarDB数据库创新设计赛」已启动报名,面向全国高校本、专、硕博学生开放。赛事聚焦PolarDB-PG向量计算优化,设初赛与决赛,总奖金达25万元。旨在提升数据库系统设计与创新能力,推动产学研融合,欢迎踊跃参赛!
|
12月前
|
负载均衡 监控 安全
优化HTTP代理IP安全稳定性的关键要点
随着科技发展,越来越多企业依赖HTTP代理IP。为确保其安全稳定,建议采取以下措施:选择可靠服务商、使用HTTPS加密、定期更换IP、监控可用性、设置访问控制、使用负载均衡、配置防火墙、定期更新维护及用户教育。这些方法能有效提升代理IP的安全性和稳定性。
422 59
|
安全 Java 数据库
第3章 Spring Security 的用户认证机制(2024 最新版)(上)
第3章 Spring Security 的用户认证机制(2024 最新版)
845 0
|
安全 Java API
【Spring Security】Spring Security 认证与授权
【Spring Security】Spring Security 认证与授权
363 0
|
XML 前端开发 Java
Spring3 MVC中使用Swagger生成API文档
Spring3 MVC中使用Swagger生成API文档
402 0
|
存储 NoSQL Shell
InfluxDB的存储引擎演化过程
InfluxDB的存储引擎从LSM Tree,到mmap B+ Tree,再到TSM Tree。
7044 0
QtCreater增加自动添加注释的快捷代码
QtCreater增加自动添加注释的快捷代码
960 0
|
存储 分布式计算 DataWorks
阿里云大数据ACA及ACP复习题(291~300)
本人备考阿里云大数据考试时自行收集准备的题库,纯手工整理的,能够覆盖到今年7月份,应该是目前最新的,发成文章希望大家能一起学习,不要花冤枉钱去买题库背了,也希望大家能够顺利通关ACA和ACP考试。

热门文章

最新文章