216.Spring Boot+Spring Security:基于内存的认证信息_Spring Security

视频&交流平台

SpringBoot视频:http://t.cn/R3QepWG

Spring Cloud视频:http://t.cn/R3QeRZc

SpringBoot Shiro视频:http://t.cn/R3QDMbh

SpringBoot交流平台:http://t.cn/R3QDhU0

SpringBoot 2.0 SpringData和JPA视频:

http://t.cn/R1pSojf

 

说明

(1)JDK版本:1.8

(2)Spring Boot 2.0.6

(3)Spring Security 5.0.9

 

需求缘起

       上面我们简单体验了下Spring Security,但是现在只能有一个用户信息,我们这里希望可以配置多个账号信息,本节主要讲解下如何在内存中配置认证信息。

 

编码思路

       我们要在内存中初始化我们的认证信息的话,那么需要是重写WebSecurityConfigurerAdapter类中的configure方法:

configure(AuthenticationManagerBuilder auth)

然后通过auth对象的inMemoryAuthentication()方法指定认证信息:

 

auth.inMemoryAuthentication().withUser("admin").password("123456");

       综上所述,首先需要定义一个配置类WebSecurityConfig继承WebSecurityConfigurerAdapter;接着重写里面的configure方法;最后使用AuthenticationManagerBuilder构建认证信息。

 

一、基于内存的认证信息

       我们基于上一篇文章中的项目springboot2-springSecurity01进行编码。

1.1 创建一个配置类

       我们定义一个WebSecurityConfig,继承WebSecurityConfigurerAdapter,如下代码:

package com.kfit.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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(AuthenticationManagerBuilder auth) throws Exception {

        /*
         * 配置为从内存中进行加载认证信息.
         * 这里配置了两个用户 admin和user
         */
        auth.inMemoryAuthentication().withUser("admin").password("123456").roles();
        auth.inMemoryAuthentication().withUser("user").password("123456").roles();
    }
}

说明:

(1)@Configuration:注解这是一个配置类。

(2)@EnableWebSecurity:注解开启Spring Security的功能。

(3)WebSecurityConfigurerAdapter:继承WebSecurityConfigurerAdapter,并重写它的方法来设置一些web安全的细节。

(4)configure(AuthenticationManagerBuilder auth):在内存中创建了用户admin/user,密码为123456。

       如果是5.x之前的版本的话,那么到这里启动的话,就可以正常访问了,但是如果是5.x的版本的话,可以正常启动,但是在登录页面输入admin/123456账号进行访问的话,会报如下错误:

java.lang.IllegalArgumentException:There is no PasswordEncoder mapped for the id "null"

       这是因为Spring Security 5.0中新增了多种加密方式,也改变了密码的格式。

 

1.2 密码加密

       上面异常原因就是因为没有指定加密方式,那么怎么指定呐?

1.2.1 方式一:通过AuthenticationManagerBuilder指定

       在AuthenticationManagerBuilder的方法中就可以指定加密方式了,如下代码:

auth.inMemoryAuthentication()
    .passwordEncoder(new BCryptPasswordEncoder())
    .withUser("admin")
    .password(new BCryptPasswordEncoder().encode("123456"))
    .roles();

auth.inMemoryAuthentication()
    .passwordEncoder(new BCryptPasswordEncoder())
    .withUser("user")
    .password(new BCryptPasswordEncoder().encode("123456"))
    .roles();

说明:

(1)Bcrypt: bcrypt是一种跨平台的文件加密工具。bcrypt 使用的是布鲁斯·施内尔在1993年发布的 Blowfish 加密算法。由它加密的文件可在所有支持的操作系统和处理器上进行转移。它的口令必须是8至56个字符,并将在内部被转化为448位的密钥。

 

1.2.2 方式二:通过@Bean注入指定PasswordEncoder

       在WebSecurityConfig配置类中,通过@Bean注入PasswordEncoder具体的实现类,如下代码:

    @Bean  
    public PasswordEncoder passwordEncoder() {  
        return new BCryptPasswordEncoder();  
    }

       加入这个之后,需要修改configure的密码加密:

 

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        /*
         * 配置为从内存中进行加载认证信息.
         * 这里配置了两个用户 admin和user
         */
        auth.inMemoryAuthentication()
            .withUser("admin")
            .password(passwordEncoder().encode("123456"))
            .roles();

        auth.inMemoryAuthentication()
            .withUser("user")
            .password(passwordEncoder().encode("123456"))
            .roles();
}

       对于上面的两种方式,推荐使用第二种方式,这种方式一方面更容易理解,另外代码扩展性更强。

 

 

 

 

Spring Security更新视频:

Spring Boot 2.0.6 

Spring Security 5.0.9

9.动态加载角色
10.Security:原理1
11.自定义Filter
12.页面白名单和获取登录信息
13.基于URL动态权限n种方案
14.基于URL动态权限:准备工作
15.基于URL动态权限:扩展access()的SpEL表达式
16.标签sec/authorize的使用
17.获取用户信息和Session并发控制