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

SpringSecurity的配置

一、SpringSecurity的功能简单介绍

(1)简介

SpringSecurity是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理!

主要的几个类:

webSecurityConfigurerAdapter:自定义Security策略

AuthenticationManagerBuilder:自定义的认证策略

@EnableWebSecurity:开启WebSecurity模式

SpringSecurity的两个主要目标是"认证”和“授权”(访问控制)。

“认证”(Authentication)

“授权”(Authorization)

(2)SpringSecurity的主要功能

1.功能权限

2.访问权限

3.菜单权限

4.拦截器,过滤器

5.AOP:横切 配置类

二.SpringSecurity的使用

1.引入Spring Security的依赖包
<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.配置SecurityConfig类
//AOP : 拦截器
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        // super.configure(http);
        //首页所有人可以访问,功能页只有对应权限的人才能访问
        //请求授权的规则
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")          //vip1用户只可以访问该路径下的页面
                .antMatchers("/level2/**").hasRole("vip2")          //vip2用户只可以访问该路径下的页面
                .antMatchers("/level3/**").hasRole("vip3");         //vip3用户只可以访问该路径下的页面

        //没有权限默认回到登录页面,需要开启登录的页面
        http.formLogin();

        //注销,开启了注销功能,跳到首页
        //http.logout().logoutSuccessUrl("/");
        //定制登录页
        http.formLogin().loginPage("/toLogin");

        //开启记住我功能 cooke
        http.rememberMe();
    }

    //认证   springboot 2.1.X 可以直接使用
    //密码编码: passwordEncoder
    //在spring Security 5.0+ 新增了很多的加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //这些数据正常应该从数据库中读取  现在测试的是从内存中读取的数据
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())   //加了一个密码的编码规则
                .withUser("LJ").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2","vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123")).roles("vip1");

        //从数据库中获取数据
//        auth.jdbcAuthentication()
//                .dataSource(dataSource)
//                .withDefaultSchema()
//                .withUser(users.username("user").password("password").roles("USER"))
//                .withUser(users.username("admin").password("password").roles("USER","ADMIN"));
    }
}

三.前端使用SpringSecurity

1.前端使用thymeleaf框架

(1)引入thymeleaf模板的依赖
        <!-- thymeleaf模板 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>
(2)引入security-thymeleaf整合包
        <!-- security-thymeleaf整合包 -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>
(3)前端页面引入semantic-ui的框架包

2.前端页面用户用户认证和授权

如果注销失败:可能的原因是默认开启的csrf的防止网站攻击工具,可以在SecurityConfig中的加入关闭csrf:

根据登入的人的不同权限,会显示与对应权限的内容。


http://www.kler.cn/a/569687.html

相关文章:

  • AIGC(生成式AI)试用 26 -- 跟着清华教程学习 - DeepSeek与AI幻觉
  • 数字人技术再超越,TANGO 可生成与音频匹配的全身手势视频
  • Ubuntu20.04下各类常用软件及库安装汇总(联想Y9000P24款)
  • AI产品经理W1D4埋点设计数据采集
  • java关键字-instanceof
  • 具身智能(Embodied AI)的物理交互基准测试:构建真实世界的智能体评估体系
  • 奇妙跨界:将前端概念融入整数操作
  • Python 爬虫 – BeautifulSoup
  • Linux常见基本指令(二)
  • 批量提取 Word 文档中的页面
  • php序列化与反序列化
  • 迷你世界脚本世界接口:World
  • 矩阵 trick 系列 题解
  • 算法日常刷题笔记(3)
  • 【R语言】PCA主成分分析
  • 计算机毕业设计SpringBoot+Vue.js常规应急物资管理系统(源码+文档+PPT+讲解)
  • 梳理vite构建vue项目可选的配置和组件
  • 匿名内部类与Lambda表达式不理解点
  • 第34周:文献阅读
  • 基于SSM实现的bbs论坛系统功能实现九