SpringSecurity系列——简单配置上手day4-3(源于官网5.7.2版本)

本文档详细介绍了如何在SpringBoot项目中配置SpringSecurity,从默认的安全设置到自定义过滤器链,包括拦截所有资源、放行所有资源及指定资源的授权策略。通过实例代码展示了如何实现登录拦截、资源权限控制等核心功能。
摘要由CSDN通过智能技术生成

前言

本片开启SpringSecurity入门上手实例,基于官方5.7.2的最新稳定版,springboot2.7.2版,jdk17,我对官方实例进行了一定程度上的修改,增加了其他一些实例代码

注意点

目前官方已决定淘汰使用SpringSecurityConfigurationAdapter

简单配置上手

默认SpringSecurity

1.新建项目

在这里插入图片描述

2.添加依赖

在这里插入图片描述

3.编写controller

package com.example.login.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
   
    @GetMapping("/index")
    public String login(){
   
        return "login....";
    }

    @GetMapping("/test")
    public String test(){
   
        return "test...";
    }
}

4.启动测试

在这里插入图片描述
当我们访问任意地址时就会跳转至http://localhost:8080/login
输入用户名:user
密码:如下图所示
在这里插入图片描述
进行登录
访问成功
在这里插入图片描述


拦截所有资源

效果上同第一个默认的

1.添加SpringSecurityConfig

在这里插入图片描述

package com.example.login.config;

public class SpringSecurityConfig {
   }

2.添加注解@EnableWebSecurity

package com.example.login.config;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

@EnableWebSecurity
public class SpringSecurityConfig {
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值