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:

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

相关推荐
咖啡の猫1 小时前
Python顺序结构
java·前端·python
key061 小时前
从数据安全体系逆推数据自由度的权力本质
java·开发语言
z***43841 小时前
java与mysql连接 使用mysql-connector-java连接msql
java·开发语言·mysql
白露与泡影1 小时前
大厂Java面试前复习的正确姿势(800+面试题附答案解析)
java·开发语言·面试
N***p3651 小时前
Springboot项目中线程池使用整理
java·spring boot·后端
asom221 小时前
互联网大厂Java全栈面试故事:从Spring Boot、分布式到AI业务场景深度剖析
java·spring boot·分布式·缓存·微服务·消息队列·面试经验
程序定小飞1 小时前
基于SpringBoot+Vue的常规应急物资管理系统的设计与实现
java·开发语言·vue.js·spring boot·后端·spring
t梧桐树t1 小时前
spring AI都能做什么
java·人工智能·spring
R***z1011 小时前
Spring容器初始化扩展点:ApplicationContextInitializer
java·后端·spring