SpringSecurity学习(一)

认证和授权

认证解决“我是谁的问题”

什么是认证?

在这里插入图片描述
认证: 验证当前访问系统的是不是本系统的用户,并且要确认具体是哪一个用户

授权解决“我能做什么”的问题

在这里插入图片描述
授权:经过认证之后判断当前用户是否有权限进行某个操作;
登录校验流程 转载于:B站,三更草堂

Filer和FilterChain

Spring Filters

  • 任何Spring Web应用本质上只是一个Servlet;
  • Security Filter在Http请求到达Controller之前过滤每一个传入的Http请求;
    在这里插入图片描述
    在这里插入图片描述

Filter Chain

在这里插入图片描述

常见的内建过滤器

过滤器名称作用
BasicAuthenticationFilter如果在请求中找到一个Basic Auth Http头,如果找到,则尝试用该头中的用户名和密码验证用户
UsernamePasswordAuthenticationFilter若在请求参数或者Post的RequestBody中找到用户名和密码,则尝试用这些值对用户进行身份验证
DefaultLoginPageGeneratingFiltler默认登录页面生成过滤器,用于生成一个登录页面,若没有明确禁用这个功能,那么就会生成一个登录页面。这就是为什么在启用SpringSecurity时候,会得到一个默认的页面的原因
DefaultLogoutPageGeneratingFilter若没有禁用该功能,则会生成一个注销页面
FilterSecurityInterceptor过滤安全拦截器,用于授权逻辑

SpringSecurity实现认证和授权的底层机制

Http

熟悉Http的请求

在这里插入图片描述

Http响应

在这里插入图片描述

Filter和客户端交互(获取数据返回数据)是通过请求和响应中的字段完成的

实战

定制化登录页

新建工程

	<dependency>
	   <groupId>org.webjars</groupId>
	   <artifactId>webjars-locator-core</artifactId>
	</dependency>
	
	<dependency>
	   <groupId>org.webjars</groupId>
	   <artifactId>bootstrap</artifactId>
	   <version>${bootstrap.version}</version>
	</dependency>
	
	<dependency>
	   <groupId>org.springframework.boot</groupId>
	   <artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

依赖类库

安全配置

SecurityConfig

package com.vleus.uaa.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author vleus
 * @date 2022年05月17日 22:45
 */
@EnableWebSecurity(debug = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 走过滤器链
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf(csrf -> csrf.disable())
                .httpBasic(Customizer.withDefaults())
                .formLogin(form -> form.loginPage("/login"))
                .authorizeRequests(req -> req.antMatchers("/api/**").authenticated());

    }

    /**
     * 设置不启用过滤器链
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().mvcMatchers("/public/**");
    }
}

路径映射类

package com.vleus.uaa.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 映射url与模板文件路径
 * @author vleus
 * @date 2022年05月22日 20:59
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 将webjar的静态资源加入到映射中
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("/webjars")
                .resourceChain(false);
        registry.setOrder(1);

    }

    /**
     * 文件路径和url进行映射
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login")
                .setViewName("login");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}

CSRF攻击和保护

在这里插入图片描述

防止收到CSRF攻击的方式
CSRF Token在这里插入图片描述
在响应设置Cookie的SameSite属性

在这里插入图片描述

Remember me功能
  • 为解决session过期后用户的直接访问问题;
  • Spring Security提供开箱即用的配置 rememberMe;
  • 原理:使用Cookie存储用户名,过期时间,以及一个Hash;

Hash:md5(用户名+过期时间+密码+key)

登录成功和失败后的处理

  • 登录成功后的处理:AuthenticationSuccessHandler;
  • 登录失败后的处理:AuthenticationFailureHandler;
  • 退出登录成功后的处理:LogoutSuccessHandler;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值