Spring Security 介绍

  1. Spring Security 是基于 Spring 的身份认证(Authentication)和用户授权(Authorization)框架,提供了一套 Web 应用安全性的完整解决方案。其中核心技术使用了 Servlet 过滤器、IOC 和 AOP 等。
  2. 什么是身份认证
    身份认证指的是用户去访问系统资源时,系统要求验证用户的身份信息,用户身份合法才访问对应资源。
    常见的身份认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。
  3. 什么是用户授权
    当身份认证通过后,去访问系统的资源,系统会判断用户是否拥有访问该资源的权限,只允许访问有权限的系统资源,没有权限的资源将无法访问,这个过程叫用户授权。
    比如 会员管理模块有增删改查功能,有的用户只能进行查询,而有的用户可以进行修改、删除。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限

SpringSecurity身份认证方式:

  • HttpBasic 是弹窗认证方式。
  • HttpForm 是表单认证方式。

创建安全配置类SpringSecurityConfig并且继承WebSecurityConfigurerAdapter作为安全控制中心, 用于实现身份认证与授权配置功能,此类需要加上@EnableWebSecurity证明 启动springSecurity过滤链功能,需要重写两个方法

/**
   身份认证过滤器
2. 认证信息提供方式(用户名,密码,当前用户的资源权限)
3. 可采的内存存储方式,也可采用数据库方式等。
*/
configure(AuthenticationManagerBuilder auth);

/**
 资源权限配置(过滤器链)
4. 拦截哪些资源
5. 资源所对应的角色权限
 3. 定义认证方式 httpbasic  httpform
6. 定制登录页面,登录请求地址,错误处理方式
7. 自定义 springsecurity 过滤器等
*/
configure(HttpSecurity http)

 

1.HttpBasic认证方式:

/**
     * 资源权限配置(过滤器链):
     * 1、被拦截的资源
     * 2、资源所对应的角色权限
     * 3、定义认证方式:httpBasic 、httpForm
     * 4、定制登录页面、登录请求地址、错误处理方式
     * 5、自定义 spring security 过滤器
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic()//采用httpBasic 认证方式
                .and()
                .authorizeRequests()//认证请求
                .anyRequest().authenticated();// 所有进入应用的HTTP请求都要进行认证
    }

1.使用HttpBasic 认证方式

1.1使用security默认的账号和密码,账号为user 密码  springboot启动日志中的Using generated security password: 9be0927b-b0bb-4253-92a6-d57954e5da87

 

 

 

1..2,使用内存自定义用户名和密码,

Logger logger= LoggerFactory.getLogger(SpringSecurityConfig.class);


    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        // 加密存储   明文+随机盐值
        return  new BCryptPasswordEncoder();
    }


    /**
     * 认证管理器:
     * 1、认证信息提供方式(用户名、密码、当前用户的资源权限)
     * 2、可采用内存存储方式,也可能采用数据库方式等
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 存储的密码必须是加密后的 不然会报错:There is no PasswordEncoder mapped for the id "null"
        //auth.inMemoryAuthentication().withUser("zcc").password("123").authorities("ADMIN");
        String password=bCryptPasswordEncoder().encode("123");
        logger.info("加密后的密码:"+password);
        auth.inMemoryAuthentication().withUser("zcc").password(password).authorities("ADMIN");
    }

 

2.使用HttpForm 表单认证方式

 /**
     * 资源权限配置(过滤器链):
     * 1、被拦截的资源
     * 2、资源所对应的角色权限
     * 3、定义认证方式:httpBasic 、httpForm
     * 4、定制登录页面、登录请求地址、错误处理方式
     * 5、自定义 spring security 过滤器
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //http.httpBasic()//采用httpBasic 认证方式
        http.formLogin()
                .and()
                .authorizeRequests()//认证请求
                .anyRequest().authenticated();// 所有进入应用的HTTP请求都要进行认证
    }

 

 

 

 

3.完整安全配置类代码:

package com.ytkj.security.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 *  安全配置类作为安全控制中心, 用于实现身份认证与授权配置功能
 */
@Configuration
@EnableWebSecurity //启动 SpringSecurity 过滤器链功能
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    Logger logger= LoggerFactory.getLogger(SpringSecurityConfig.class);


    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        // 加密存储   明文+随机盐值
        return  new BCryptPasswordEncoder();
    }


    /**
     * 认证管理器:
     * 1、认证信息提供方式(用户名、密码、当前用户的资源权限)
     * 2、可采用内存存储方式,也可能采用数据库方式等
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //基于内存存储认证信息 存储的密码必须是加密后的 不然会报错:There is no PasswordEncoder mapped for the id "null"
        //auth.inMemoryAuthentication().withUser("zcc").password("123").authorities("ADMIN");
        String password=bCryptPasswordEncoder().encode("123");
        logger.info("加密后的密码:"+password);
        auth.inMemoryAuthentication().withUser("zcc").password(password).authorities("ADMIN");
    }

    /**
     * 资源权限配置(过滤器链):
     * 1、被拦截的资源
     * 2、资源所对应的角色权限
     * 3、定义认证方式:httpBasic 、httpForm
     * 4、定制登录页面、登录请求地址、错误处理方式
     * 5、自定义 spring security 过滤器
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //http.httpBasic()//采用httpBasic 认证方式
        http.formLogin()
                .and()
                .authorizeRequests()//认证请求
                .anyRequest().authenticated();// 所有进入应用的HTTP请求都要进行认证
    }
}

 

底层源码实现:

  spring security采用过滤器链实现认证与授权

    1.UsernamePasswordAuthenticationFilter :请求为/login,方式为post  请求中包含用户名密码则进行认证,认证成功标记认证成功,否则进入下一个认证过滤器(HTTP form请求方式)

        

 

         

 

         

    2.BasicAuthenticationFilter :请求头有basic开头的信息,base64解码后认证,认证成功则标记认证成功,否则进入下一认证过滤器(前提是使用了httpbasic认证方式,如果使用HTTP form认证方式则不会进入此过滤器)

         

 

         

    3.其他认证过滤器

    4.ExceptionTranslationFilter :捕获异常处理后续处理

        

 

 

    5.FilterSecurityInterceptor : 认证通过后,根据资源权限配置来判断当前请求是否可以访问对应资源。(1 2 3 属于用户认证,只属于第一道门槛,这里会根据 config 中配置的对应权限再次进行鉴权,如果没有对应的可访问资源 那就会抛出异常 4 )

    

 完整代码地址:https://gitee.com/zhechaochao/security-parent.git

posted on 2020-04-01 23:22  西门夜说  阅读(1286)  评论(0编辑  收藏  举报