当前位置: 首页 > article >正文

SpringSecurity授权、认证

引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity5</artifactId><version>3.0.4.RELEASE</version>
</dependency>

配置类SecurityConfig

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {//授权@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/fail").permitAll().antMatchers("/level1/**").hasRole("vip1").antMatchers("/level2/**").hasRole("vip2").antMatchers("/level3/**").hasRole("vip3");http.formLogin().defaultSuccessUrl("/index")  //登录认证成功后默认转跳的路径.failureForwardUrl("/fail");//http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/toLogin").loginProcessingUrl("/login"); //.loginPage定制登录跳转页//http.logout().deleteCookies("remove").invalidateHttpSession(true);http.csrf().disable(); //关闭csrf功能http.logout().logoutSuccessUrl("/"); //开启注销功能,注销成功跳转到首页http.rememberMe().rememberMeParameter("remember"); //开启记住我功能}//认证@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("ls").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");}
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/extras/spring-security"><head><meta charset&
http://www.lryc.cn/news/2383534.html

相关文章:

  • 蓝桥杯19682 完全背包
  • DeepSeek源码解构:从MoE架构到MLA的工程化实现
  • leetcode 3355. 零数组变换 I 中等
  • 【VLNs篇】02:NavGPT-在视觉与语言导航中使用大型语言模型进行显式推理
  • (T_T),不小心删掉RabbitMQ配置文件数据库及如何恢复
  • 创建react工程并集成tailwindcss
  • TDengine 安全部署配置建议
  • Axure全链路交互设计:快速提升实现能力(基础交互+高级交互)
  • 为什么wifi有信号却连接不上?
  • 蓝桥杯框架-LED蜂鸣器继电器
  • uniapp-商城-64-后台 商品列表(商品修改---页面跳转,深浅copy应用,递归调用等)
  • Dify的大语言模型(LLM) AI 应用开发平台-本地部署
  • 使用教程:8x16模拟开关阵列可级联XY脚双向导通自动化接线
  • 移动端前端调试调研纪实:从痛点出发,到 WebDebugX 的方案落地
  • 8 种快速易用的Python Matplotlib数据可视化方法
  • 【android bluetooth 协议分析 02】【bluetooth hal 层详解 3】【高通蓝牙hal主要流程介绍-上】
  • C# 深入理解类(实例构造函数)
  • RabbitMQ——消息确认
  • 测试W5500的第2步_使用ioLibrary库创建TCP客户端
  • 深度学习之用CelebA_Spoof数据集搭建一个活体检测-训练好的模型用MNN来推理
  • 【Java】泛型在 Java 中是怎样实现的?
  • 开源安全大模型Foundation-Sec-8B实操
  • 【JavaWeb】MySQL
  • 微信小游戏流量主广告自动化浏览功能案例5
  • 【C++ Primer 学习札记】函数传参问题
  • 软件的技术架构、应用架构、业务架构、数据架构、部署架构
  • CSS 文字样式全解析:从基础排版到视觉层次设计
  • 【高德开放平台-注册安全分析报告】
  • [特殊字符] React Fiber架构与Vue设计哲学撕逼实录
  • RabbitMQ的简介