文章目录

  • 安全管理介绍
  • 一、Spring Security
  • Spring Boot整合Spring Security实现的安全管理功能:
  • 二、MVC Security安全配置介绍
  • 如何关闭Sercurity提供的Web应用默认安全配置
  • WebSecurityConfigurerAdapter类的主要方法及说明
  • 本次用到的依赖
  • 自定义用户认证
  • 使用内存进行身份认证

安全管理介绍

实际开发中,一些应用通常要考虑到安全性问题。例如,对于一些重要的操作,有些请求需要用户验明身份后才可以执行,还有一些请求需要用户具有特定权限才可以执行。这样做的意义,不仅可以用来保护项目安全,还可以控制项目访问效果。

提示:以下是本篇文章正文内容,下面案例可供参考

一、Spring Security

•Spring Security是基于Spring生态圈的,用于提供安全访问控制解决方案的框架。

•Spring Security的安全管理有两个重要概念,分别是Authentication(认证)和Authorization(授权)。

Spring Boot整合Spring Security实现的安全管理功能:

MVC Security是Spring Boot整合Spring MVC框架搭建的Web应用的安全管理。

WebFlux Security是Spring Boot整合Spring WebFlux框架搭建的Web应用的安全管理。

OAuth2是大型项目的安全管理框架,可以实现第三方认证、单点登录等功能。

Actuator Security用于对项目的一些运行环境提供安全监控,例如Health健康信息、Info运行信息等,它主要作为系统指标供运维人员查看管理系统的运行情况。
官方默认的安全登录常见几个问题
1.只有唯一的默认登录用户user、密码随机生成且过于暴露、登录页面及错误提示页面不是我们想要的等。

二、MVC Security安全配置介绍

项目引入spring-boot-starter-security依赖启动器,MVC Security安全管理功能就会自动生效,其默认的安全配置是在SecurityAutoConfiguration和UserDetailsServiceAutoConfiguration中实现的。

•SecurityAutoConfiguration:•导入并自动化配置启动Web安全管理

•UserDetailsServiceAutoConfiguration:•用于配置用户身份信息

如何关闭Sercurity提供的Web应用默认安全配置

1.要完全关闭Security提供的Web应用默认安全配置,可以自定义WebSecurityConfigurerAdapter类型的Bean组件以及自定义UserDetailsService、AuthenticationProvider或AuthenticationManager类型的Bean组件。

2.另外,可以通过自定义WebSecurityConfigurerAdapter类型的Bean组件来覆盖默认访问规则。

WebSecurityConfigurerAdapter类的主要方法及说明

方法

描述

configure(AuthenticationManagerBuilder auth)

定制用户认证管理器来实现用户认证

configure(HttpSecurity http)

定制基于HTTP请求的用户访问控制

本次用到的依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</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>
        <!-- Spring Data JPA操作数据库  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- Redis缓存启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- JDBC数据库连接启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- MySQL数据连接驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

自定义用户认证

步骤一:创建一个config包里面的WebSecurityConfigurerAdapter配置类

@EnableWebSecurity注解是一个组合注解,主要包括@Configuration注解:声明当前类为配置类

@Import({WebSecurityConfiguration.class, SpringWebMvcImportSelector.class})注解:根据pom.xml中导入的web模板和security模板进行自动化配置

@EnableGlobalAuthentication注解:开启自定义的全局认证

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity//开启MVCSecurity安全支持(这是一个组合注解,包括了主要包括@Configuration注解、@Import({WebSecurityConfiguration.class, SpringWebMvcImportSelector.class})注解和@EnableGlobalAuthentication注解)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
使用内存进行身份认证

SecurityConfig类中重写configure(AuthenticationManagerBuilder auth)方法,并在该方法中使用内存身份认证的方式自定义了认证用户信息。定义用户认证信息时,设置了两个用户名和密码以及对应的角色信息。

@EnableWebSecurity//开启MVCSecurity安全支持(这是一个组合注解,包括了主要包括@Configuration注解、@Import({WebSecurityConfiguration.class, SpringWebMvcImportSelector.class})注解和@EnableGlobalAuthentication注解)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
          @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
           //设置密码编码器
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            //模拟测试用户
            auth.inMemoryAuthentication().passwordEncoder(encoder)
                    .withUser("user1").password(encoder.encode("123456")).roles("common")
                    .and()
                    .withUser("user2").password(encoder.encode("123456")).roles("vip");
        }}

Spring Security提供多种密码编码器,包括BcryptPasswordEncoder,Pbkdf2PasswordEncoder,ScryptPasswordEncoder等密码设置不限于本例中BcryptPasswordEncoder密码编码器
用内存可以存储多个用户信息,用.and()连接