SpringSecurity (一) --------- 简介与快速入门

悠悠 2024-04-03 12:53 167阅读 0赞

目录

  • 一、简介
  • 二、快速入门
      1. 准备工作
      1. 引入 SpringSecurity

一、简介

Spring Security 是 Spring 家族中的一个安全管理框架。相比与另外一个安全框架 Shiro,它提供了更丰富的功能,社区资源也比 Shiro 丰富。

一般来说中大型的项目都是使用 SpringSecurity 来做安全框架。小项目有 Shiro 的比较多,因为相比与SpringSecurity,Shiro 的上手更加的简单。

一般Web应用的需要进行认证和授权。

  • 认证:验证当前访问系统的是不是本系统的用户,并且要确认具体是哪个用户
  • 授权:经过认证后判断当前用户是否有权限进行某个操作

而认证和授权也是SpringSecurity作为安全框架的核心功能。

二、快速入门

1. 准备工作

我们先要搭建一个简单的SpringBoot工程

① 设置父工程 添加依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.5.0</version>
  5. </parent>
  6. <dependencies>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.projectlombok</groupId>
  13. <artifactId>lombok</artifactId>
  14. <optional>true</optional>
  15. </dependency>
  16. </dependencies>

② 创建启动类

  1. @SpringBootApplication
  2. public class SecurityApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SecurityApplication.class,args);
  5. }
  6. }

③ 创建Controller

  1. import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
  2. @RestController public class HelloController {
  3. @RequestMapping("/hello")
  4. public String hello(){
  5. return "hello";
  6. }
  7. }

2. 引入 SpringSecurity

在 SpringBoot 项目中使用 SpringSecurity 我们只需要引入依赖即可实现入门案例。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>

引入依赖后我们在尝试去访问之前的接口就会自动跳转到一个 SpringSecurity 的默认登陆页面,默认用户名是user,密码会输出在控制台。

必须登陆之后才能对接口进行访问。

发表评论

表情:
评论列表 (有 0 条评论,167人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringSecurity简介

    Spring Security是什么? Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可