栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringSecurity学习(一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringSecurity学习(一)

认证和授权 认证解决“我是谁的问题” 什么是认证?

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

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和客户端交互(获取数据返回数据)是通过请求和响应中的字段完成的 实战 定制化登录页 新建工程
	
	   org.webjars
	   webjars-locator-core
	
	
	
	   org.webjars
	   bootstrap
	   ${bootstrap.version}
	
	
	
	   org.springframework.boot
	   spring-boot-starter-thymeleaf
	
依赖类库 安全配置

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;


@EnableWebSecurity(debug = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    
    @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
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().mvcMatchers("/public
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars
    @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;
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号