Spring Security入门

45 篇文章 9 订阅
42 篇文章 1 订阅

1. Spring Security 简介

Spring Security 是一个高度可定制的身份验证和访问控制框架,它基于 Spring 框架,并可与 Spring 全家桶无缝集成。该框架可以精确控制用户对应用程序的访问,控制用户的角色和权限等。

Spring Security 最早是由 Ben Alex 开发,2004年时首次发布。它的前身是 Acegi Security Framework。

2. 认证

2.1 登陆校验流程

在这里插入图片描述

2.2 原理初探

​ 想要知道如何实现自己的登陆流程就必须要先知道入门案例中SpringSecurity的流程。

2.2.1 SpringSecurity完整流程

​ SpringSecurity的原理其实就是一个过滤器链,内部包含了提供各种功能的过滤器。这里我们可以看看入门案例中的过滤器。
在这里插入图片描述

图中只展示了核心过滤器,其它的非核心过滤器并没有在图中展示。

  • UsernamePasswordAuthenticationFilter:负责处理我们在登陆页面填写了用户名密码后的登陆请求。入门案例的认证工作主要有它负责。
  • ExceptionTranslationFilter:处理过滤器链中抛出的任何AccessDeniedException和AuthenticationException。
  • FilterSecurityInterceptor:负责权限校验的过滤器。
2.2.2 认证流程详解

在这里插入图片描述

概念速查:

  • Authentication接口: 它的实现类,表示当前访问系统的用户,封装了用户相关信息。
  • AuthenticationManager接口:定义了认证Authentication的方法
  • UserDetailsService接口:加载用户特定数据的核心接口。里面定义了一个根据用户名查询用户信息的方法。
  • UserDetails接口:提供核心用户信息。通过UserDetailsService根据用户名获取处理的用户信息要封装成UserDetails对象返回。然后将这些信息封装到Authentication对象中。
2.2.3登录实现

在这里插入图片描述

3. Spring Security 基础

Spring Security 的核心是基于配置的安全性提供方式。这意味着开发人员要使用适当的代码来定义安全性设置,并配置 Spring Security 提供的相关组件,例如过滤器链、认证管理器等。Spring Security 提供了一些高度可定制的内置类和接口,以帮助开发人员按需定制应用程序安全性需求。

下面是在 Spring Boot 中启用 Spring Security 的简单示例:

配置文件(application.yml)

spring:
  security:
    user:
      name: user
      password: password

依赖配置

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

Web Security 配置类

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .and()
            .httpBasic(); 
    }
}

上述示例中,我们启用了 Spring Security,并对所有请求进行了基本的身份验证。如果访问应用程序时,未经身份验证,将会弹出一个默认的登录页面,并需要用户输入用户名和密码。

Spring Security 还提供了更多的配置选项,例如启用 CORS 支持、设置 CSRF 保护、配置 LDAP 连接、支持 OAuth 2.0 等。有关更多详细信息,请参阅官方文档。

4. Spring Security 实践

在实践中,Spring Security 通常与其他组件一起使用,例如 Spring MVC、Thymeleaf、JPA 等。下面是一个示例,展示如何创建一个基于 Spring MVC、Thymeleaf 和 Spring Security 的 Web 应用程序。

下面的示例功能很简单,仅包含一个页面,显示用户的消息。但是,访问该页面是需要登录的。

依赖配置

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

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

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

页面模板(message.html)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Simple Thymeleaf Example</title>
</head>
<body>
<h1>Welcome, <span th:text="${name}"></span></h1>
<p><a href="/logout" th:href="@{/logout}">Logout</a></p>
</body>
</html>

控制器

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MessageController {
    @GetMapping("/message")
    public String getMessage(Authentication authentication, Model model) {
        String name = ((User)authentication.getPrincipal()).getUsername();
        model.addAttribute("name", name);
        return "message";
    }
}

Web Security 配置类

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
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
}

上述配置中,我们允许未经身份验证的用户访问应用程序的主页和首页。如果用户试图访问需要验证的资源,则会弹出一个默认的登录页面。如果用户未登录,则会重定向到自定义的登录页面:/login

自定义登录页面

为了创建一个自定义的登录页面,我们需要在模板文件中创建一个类似于以下代码的表单:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Login Page</title>
</head>
<body>
    <div th:if="${param.error}">
        Invalid username or password.
    </div>
    <div th:if="${param.logout}">
        You have been logged out.
    </div>
    <form th:action="@{/login}" method="post">
        <div>
            <label for="username">Username:</label>
            <input type="text" id="username" name="username" />
        </div>
        <div>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password" />
        </div>
        <div>
            <button type="submit">Login</button>
        </div>
    </form>
</body>
</html>

在上述代码中,我们使用了 Thymeleaf 模板引擎来创建一个简单的登录表单。如果用户提供的凭据无效,则会显示错误消息。否则,用户将被成功重定向到之前尝试访问的受保护的资源。

安全注解

在 Spring Security 中,我们可以使用注解来标记需要进行身份验证或授权的方法。下面是一个示例:

import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {
    @GetMapping("/home")
    @Secured("ROLE_USER")
    public String home() {
        return "home";
    }
}

在上述示例中,我们使用 @Secured 注解来标记 HomeController 中的 home 方法,以指示只允许拥有 ROLE_USER 权限的用户访问该页面。

小结

本文介绍了 Spring Security 的基础知识和实践方法。Spring Security 提供了丰富的功能和定制性,可以帮助您构建更加安全的应用程序。如果您想了解更多信息,请查看官方文档,里面提供了更多例子和配置选项。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Security是一个用于身份验证和授权的框架,在Spring项目中提供了一套强大的安全性解决方案。以下是你入门Spring Security的步骤: 1. 添加Spring Security依赖:在你的项目中,通过Maven或Gradle添加Spring Security的依赖。例如,在Maven中,你可以添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 配置Spring Security:创建一个配置类来配置Spring Security。这个配置类需要继承`WebSecurityConfigurerAdapter`类,并覆盖`configure`方法。例如,你可以创建一个类叫做`SecurityConfig`: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 允许公共访问的URL .anyRequest().authenticated() // 其他URL需要身份验证 .and() .formLogin() // 启用表单登录 .loginPage("/login") // 自定义登录页面URL .permitAll() .and() .logout() // 启用注销 .permitAll(); } } ``` 上述配置中,我们定义了哪些URL是公开访问的,哪些URL需要身份验证,以及自定义了登录和注销的相关配置。 3. 创建用户服务:在上面的配置类中,你需要定义一个用户服务来获取用户的身份验证信息。这可以通过实现`UserDetailsService`接口来完成。你可以创建一个类叫做`UserService`来实现这个接口,并重写`loadUserByUsername`方法: ```java @Service public class UserService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 从数据库或其他数据源中获取用户信息 // 然后返回一个实现了UserDetails接口的类,代表用户的身份验证信息 // 例如,你可以使用Spring Security提供的User类 return User.builder() .username(username) .password("password") .roles("USER") .build(); } } ``` 上述代码中,我们简单地返回了一个固定的用户信息,实际应用中你需要从数据库或其他数据源中获取真实的用户信息。 4. 配置密码编码器:为了安全起见,你需要对用户密码进行编码。在上述的配置类中,通过重写`configure`方法来配置密码编码器。例如: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserService userService; @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userService).passwordEncoder(passwordEncoder()); } // 其他配置... } ``` 上述代码中,我们使用了`BCryptPasswordEncoder`来对密码进行编码。 这些是入门Spring Security的基本步骤。当你完成了上述配置后,你的应用程序将需要进行身份验证,并且可以通过URL保护来限制访问。你可以根据需要进一步自定义和扩展Spring Security的功能。希望这能帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沙漠真有鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值