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

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&

相关文章:

  • python如何做数据预处理
  • matlab求矩阵的逆、行列式、秩、转置
  • 编程基础:什么是变量
  • 【Linux笔记】nfs网络文件系统与autofs(nfsdata、autofs、autofs.conf、auto.master)
  • Linux面试题集合(6)
  • STM32外设DA实战-DAC + DMA 输出正弦波
  • 打卡Day28
  • 1.2 C++第一个程序
  • JavaScript splice() 方法
  • C语言斐波那契数列
  • 计算机视觉与深度学习 | Python实现EMD-CNN-LSTM时间序列预测(完整源码、数据、公式)
  • 【前端基础】11、CSS的属性特性(继承、层叠、元素类型、隐藏元素的四种方式)
  • 松下SMT贴片机选型与高效应用指南
  • webman用nginx代理静态json文件的异步跨域
  • 2025年数字孪生技术最新应用案例:跨领域实践与技术趋势
  • c++成员函数返回类对象引用和直接返回类对象的区别
  • java中的方法详解
  • Seata源码—5.全局事务的创建与返回处理二
  • Linux中的进程
  • Java-Collections类高效应用的全面指南
  • 人民日报和音:相信中国就是相信明天
  • 美国考虑让移民上真人秀竞逐公民权,制片人称非现实版《饥饿游戏》
  • AI赋能科学红毯,机器人与科学家在虚实之间叩问“科学精神”
  • 一条铺过11年时光的科学红毯,丈量上海科创的“长宽高”
  • 霍步刚任辽宁沈阳市委书记
  • 习近平就乌拉圭前总统穆希卡逝世向乌拉圭总统奥尔西致唁电