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

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.xdnf.cn/news/6905.html

相关文章:

  • 数据库blog1_信息(数据)的处理与效率提升
  • Java 应用如何实现 HTTPS:加密数据传输的实用指南
  • liunx常用命令总结
  • RT Thread FinSH(msh)调度逻辑
  • mysql中4种扫描方式和聚簇索引非聚簇索引【爽文一篇】
  • 2025年EB SCI2区TOP,多策略改进黑翅鸢算法MBKA+空调系统RC参数辨识与负载聚合分析,深度解析+性能实测
  • Java面向对象基础学习笔记
  • Kafka 生产者工作流程详解
  • RAG与微调:企业知识库落地的技术选型
  • Axure元件动作四:设置选中
  • 【RabbitMQ】整合 SpringBoot,实现工作队列、发布/订阅、路由和通配符模式
  • Vue.js 教学第三章:模板语法精讲,插值与 v-bind 指令
  • 养生精要:五大维度打造健康生活
  • day33-网络编程
  • java中的运算符
  • C/C++之内存管理
  • Python爬虫-爬取百度指数之人群兴趣分布数据,进行数据分析
  • [Java][Leetcode simple] 13. 罗马数字转整数
  • 目标检测工作原理:从滑动窗口到Haar特征检测的完整实现
  • 使用Python和`python-docx`库复制Word文档样式
  • 相机Camera日志分析之十一:高通相机Camx hal预览1帧logcat日志process_capture_result详解
  • 时间序列预测从入门到精通:基础知识
  • Linux 的 UDP 网络编程 -- 回显服务器,翻译服务器
  • QT6 源(105)篇二:阅读与注释 QAction,给出源代码
  • ECMAScript 2018(ES2018):异步编程与正则表达式的深度进化
  • 系统架构-大数据架构设计
  • 【沉浸式求职学习day42】【算法题:滑动窗口】
  • 信号量的使用场景
  • MATLAB安装常见问题及解决办法
  • MySql进阶学习