1、如何在SpringBoot 中使用SpringSecurity?

本文介绍了如何构建一个SpringBoot项目,从修改pom文件开始,添加spring-boot-starter-web依赖以创建/hello资源。然后,通过引入spring-boot-starter-security依赖,整合SpringSecurity进行权限管理。系统默认生成了用户名和密码,登录成功后可以访问受保护的资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

构建 springboot 项目

修改pom文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.8</version>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

写一个 /hello 的资源

@RestController
public class HelloController {

    @GetMapping("hello")
    public String helloController(){
        return "hello world";
    }
}

项目目录
在这里插入图片描述
访问/hello
在这里插入图片描述

整合 spring security

引入依赖

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

访问/hello
在这里插入图片描述
访问需要:用户名是user,密码在控制台

2022-08-20 22:47:35.370  INFO 24344 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-08-20 22:47:35.370  INFO 24344 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 885 ms
2022-08-20 22:47:35.489  WARN 24344 --- [           main] .s.s.UserDetailsServiceAutoConfiguration : 

Using generated security password: f58b70c1-7d96-49c5-b5c3-ef5d3ea2caa0

登录成功后访问
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值