使用Spring Security实现登陆以及权限认证

本文介绍了如何使用Spring Security实现登陆认证和权限控制。首先,讲解了使用前的准备工作,包括导入依赖和配置基础类。接着,详细阐述了如何设置页面访问权限,启用Spring Security的默认登陆功能,以及自定义登陆页面。然后,通过重写configure方法定义认证规则,并实现了注销功能和'记住我'选项。最后,提及内容源于B站UP主遇见狂神说的SpringBoot教程实践。
摘要由CSDN通过智能技术生成

使用Spring Security实现登陆以及权限认证

使用之前的准备

先写一些静态页面

在这里插入图片描述

1、使用SpringSecurity之前先导入相关依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、再编写一个基础的配置类

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity // 开启WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   

   @Override
   protected void configure(HttpSecurity http) throws Exception {
   
       
  }
}

此时就可以在configure方法中配置认证与授权

设置页面的登陆认证和访问权限

3、在configure方法中设置各页面的访问权限

  //定制请求的授权规则,规定需要哪些权限才有权访问
        http.authorizeRequests().antMatchers("/").permitAll()//首页所有人都可以访问
                .antMatchers("/level1/**").hasRole("vip1")//设置vip不同等级的访问权限
            	//这些就代表用户必须拥有vip1的权限,才能够访问资源level1目录下的内容,否则无法访问
                .antMatchers("/level2/**").hasRole("vip2")
                .
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值