SpringSecurity6.x 多种登录方式配置

本文为博客 VIP 文章,开通 VIP 后可阅读全文

开通 VIP

SpringSecurity6.x变了很多写法。

在编写多种登录方式的时候,网上大多是5.x的,很多类都没了。

以下是SpringSecurity6.x多种登录方式的写法。

目录

1. 编写第一种登录-账号密码json登录方式

2. 编写第二种登录方式-手机验证码登录

3. 编写验证码登录处理器 

4. 配置SecurityConfiguration,把上边的两个 登录过滤器加到过滤器链中。


1. 编写第一种登录-账号密码json登录方式

package com.hw.mo.security.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hw.mo.captcha.config.CaptchaConfig;
import com.hw.mo.security.entity.LoginUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.io.IOException;

/**
 * @author : guanzheng
 * @date : 2023/6/26 15:17
 */
public class JsonLoginFilter extends UsernamePasswordAuthenticationFilter {

    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        String contentType = request.getContentType();

        if (MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(contentType) || MediaType.APPLICATION_JSON_UTF8_VALUE.equalsIgnoreCase(contentType)) {
            if (!request.getMethod().equals("POST")) {
                throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
            }
            String username = null;
            String password = null;
            try {
                LoginUser user = new ObjectMapper().readValue(request.getInputStream(), LoginUser.class);
                username = user.getUsername();
                username = (username != null) ? username.trim() : "";
                password = user.getPassword();
                password = (password != null) ? password : "";
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password);
            setDetails(request, authRequest);
            return this.getAuthenticationManager().authenticate(authRequest);
        }
        return super.attemptAuthentication(request,response);
    }

}

2. 编写第二种登录方式-手机验证码登录

SpringSecurity多端登录实现方案 七、SpringSecurity多端登录实现方案 1、自定义AbstractAuthenticationProcessingFilter(仿UsernamePasswordAuthenticationFilter) public class MyAuthenticationFilter extends AbstractAuthenticationProcessingFilter { public static final String SPRING_SECURITY_FORM_USERNAME 阅读详情

相关推荐

SpringBoot3.x + SpringSecurity6.x之登录用户认证

本文介绍了在SpringBoot3.x项目中集成SpringSecurity6.x实现用户认证的完整流程。主要内容包括:1. 引入SpringSecurity起步依赖后的自动防护机制;2. 配置类实现请求拦截放行和表单登录认证;3. 用户实体实现UserDetails接口;4. 自定义UserDetailsService加载用户信息;5. 配置认证管理器关联密码编码器;6. 分析SpringSecurity认证流程和关键组件;7. 实现自定义登录页与安全认证的集成。文章通过代码示例详细演示了如何从零开始构建

u011529483的博客 869

Spring Security 6.x 系列【1】基础篇之概述及入门案例

Spring组织提供的一个开源安全框架,目前最新的版本为6.0.2,基于Spring开发,所以非常适合在中使用。提供认证、授权、抵御常见攻击等功能,将认证与授权分离,并提供了扩展点。对保护命令式(Spring MVC)和响应式()应用程序有一流的支持,是保护基于Spring开发的应用程序的事实标准。认证为身份验证提供了全面的支持,身份验证是验证进行访问的主体身份。常用方法是要求用户输入用户名和密码。一旦执行身份验证,就知道身份并可以执行授权。授权授权指的是验证某个用户是否有权限执行某个操作。

记录知识、锤炼自我 1万+

新版SpringSecurity5.x使用与配置

了解SpringSecurity,并进行简单使用与配置

Alphamilk的博客 4182

springsecurity5.7.x和springsecurity6.x配置文件对比

SecurityConfig配置文件

程序猿的傻白甜 1239

SpringSecurity配置多种登录方式

SpringSecurity配置多种登陆方式

qq_53318060的博客 4406

SpringBoot + Spring Security多种登录方式:账号+微信网页授权登录

一、概述实现账号用户名+微信网页授权登录集成在Spring Security的思路,最重要的一点是要实现微信登录通过Spring Security安全框架时,不需要验证账号、密码。二、准备工作要实现该功能,首先需要掌握Spring Security框架和微信扫码登录接口相关技术,如果对这两块还不太熟悉,可以参考:1、Springboot + Spring Security实现前后端分离登录认证及权...

Java知音 6111

SpringSecurity6.x使用教程

SpringSecurity目前支持的版本如下图所示,可以看到5.x的版本过几年就不会再维护了,6.x将成为主流。

wsb_2526的博客 2370

SpringSecurity 6.X新版】自定义过滤器登录方式

适用于5.7.5及6.X版本,以密码账号登录方式通过过滤器验证方式实现

qq_25746193的博客 4729

springboot3.x+springsecurity6.x多种方式登录验证

最新的 Spring Security 5.7 及以上版本,更新了不少内容,之前的 WebSecurityConfigurerAdapter 已经被废弃了,而且,要同时实现用户名密码登录、手机验证码登录、邮箱、微信小程序等登录方式,跟之前的配置方式都会有所不同。

程序猿的傻白甜 2190

SpringSecurity6 学习

SpringSecurity6介绍,讲解基本的配置过滤链 和 对应的用户配置,流程讲解。

screamn的博客 1588

SpringBoot3.x + SpringSecurity6.x + Redis + Mybatis-plus前后端分离验证

想要完整了解SpringSecurity框架强烈推荐以下视频,使用SpringSecurity6.x的话以我的代码为准。1.没有携带token的api访问请求会被拒绝,并且重定向到安全框架的login接口进行登录验证2.在login接口中,安全框架会获取请求中包含用户名、密码的参数,经过UsernamePasswordAuthenticationFilter过滤器进行认证。

MuziSantree的博客 2148

SpringBoot3.x + SpringSecurity6.x之session管理

摘要:本文介绍了Spring Boot 3.x与Spring Security 6.x集成中的Session管理机制。重点讲解了Session的工作原理、Spring Security的默认会话配置,以及如何自定义会话管理策略。文章详细展示了如何配置会话并发控制(单点登录)、会话固定攻击防护等安全机制,并通过测试验证了配置效果。配置内容包括最大会话数限制、会话超时处理、会话迁移等安全策略,为开发者提供了完整的会话管理实现方案。

u011529483的博客 741

Spring Security多种登录方式

目录不用过滤器(使用json传参方式登录)SecurityConfig配置账号密码手机号验证码使用使用过滤器实现(使用form表单传参方式登录) 不用过滤器(使用json传参方式登录) SecurityConfig配置 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.http

dnf9906的博客 5799

Spring Security 中定义多种登录方式,比如邮箱、手机验证码登录

自定义认证方式总体来说还是很简单的;需要从头重写的也就三个类,然后就是将其配置Spring Security 中。如果需要再自定义手机号验证码登录,按照上述流程再走一遍就行。其实也是 copy 一份,然后进行小幅度的修改即可。

xxxzzzqqq_的博客 4300

Spring Security实现多种登录方式

依赖导入<parent></parent>1.自定义MyUsernamePasswordFilter/*** 10:30*/@Slf4j// 自定拦截路由} else {// 获取登录表单= null?= null?@Nullable@Nullable/*** 获取登录用户信息* @return*/try {throw new NullPointerException("获取不到登录信息");

eugene03的博客 1833

SpringSecurity自定义多重登录方式

前后分离项目,SpringSecurity自定义多重登录方式,通过自定义AuthenticationProvider实现,同时自定义过滤器进行登录验证

头发茂密的假程序猿 7052

SpringSecurity 6 快速入门

SpringSecurity 6Spring 全家桶中属于安全权限的一类框架产品,同时它对于 Spring 框架的兼容性以及高定制性以及开源等优点,使得有些企业或个人开发者都会使用该框架

qq_46216299的博客 2709

SpringSecurity6从入门到上天系列第二篇:搭建SpringSecurity6的入门级别程序!

本文是SpringSecurity6从入门到上天系列第二篇,详细介绍了从零搭建一个基于SpringBootde SpringSecurity6的入门级别程序!

七叔的博客 1万+
上一篇: springboot3过滤器HandlerInterceptor
下一篇: mongoTemplate存入数据库时,时间会少8小时
泉城打码师
博客等级 码龄11年 13粉丝 7原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值