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:

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

相关推荐
冬夜戏雪3 小时前
【java学习日记】【2025.12.7】【7/60】
java·开发语言·学习
CC.GG3 小时前
【C++】二叉搜索树
java·c++·redis
JIngJaneIL4 小时前
基于Java非遗传承文化管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
南部余额5 小时前
踩坑与解惑:深入理解 SpringBoot 自动配置原理与配置排除机制
java·spring boot·自动配置原理·import
木鹅.6 小时前
LangChain4j
java
永远都不秃头的程序员(互关)6 小时前
Java核心技术精要:高效实践指南
java·开发语言·性能优化
CoderYanger7 小时前
动态规划算法-子序列问题(数组中不连续的一段):28.摆动序列
java·算法·leetcode·动态规划·1024程序员节
代码栈上的思考7 小时前
深入解析Spring IoC核心与关键注解
java·后端·spring
Mai Dang7 小时前
SpringBoot4用Swagger
java
geekmice8 小时前
实现一个功能:springboot项目启动将controller地址拼接打印到txt文件
java·spring boot·后端