2FA-双因素认证

双因素认证(2FA,Two-Factor Authentication)是一种提高安全性的方法,要求用户在登录或进行某些敏感操作时提供两种不同类型的身份验证信息。这种方法通过引入第二层验证,增加了账户被未经授权访问的难度。

项目结构

spring-boot-2fa-demo
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── demo
│   │   │               ├── DemoApplication.java
│   │   │               ├── security
│   │   │               │   ├── SecurityConfig.java
│   │   │               │   ├── TotpAuthenticationFilter.java
│   │   │               │   ├── TotpAuthenticationProvider.java
│   │   │               │   ├── TotpAuthenticationToken.java
│   │   │               │   └── TotpAuthenticator.java
│   │   │               └── web
│   │   │                   ├── TotpSetupController.java
│   │   │                   └── TotpVerifyController.java
│   └── main
│       └── resources
│           └── application.properties
└── pom.xml

1. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-boot-2fa-demo</name><description>Spring Boot 2FA Demo</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.0</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><!-- Spring Boot Starter Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot Starter Security --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- TOTP Library --><dependency><groupId>de.taimos</groupId><artifactId>totp</artifactId><version>1.0.0</version></dependency><!-- Spring Boot Starter Test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

2. DemoApplication.java

package com.example.demo;  import com.example.demo.demo.security.TotpAuthenticator;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.context.ApplicationContext;  @SpringBootApplication  
public class DemoApplication {  public static void main(String[] args) {  ApplicationContext context = SpringApplication.run(DemoApplication.class, args);  String[] beanNames = context.getBeanNamesForType(TotpAuthenticator.class);  for (String beanName : beanNames) {  System.out.println("Found bean: " + beanName);  }}
}```### 3. Security 配置#### `SecurityConfig.java````java
package com.example.demo.demo.security;  import org.springframework.context.annotation.Configuration;  
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.web.authentication.UsernamePasswordAuthenticationFilter;  @Configuration  
@EnableWebSecurity  
public class SecurityConfig extends WebSecurityConfigurerAdapter {  @Override  protected void configure(HttpSecurity http) throws Exception {  http  .authorizeRequests()  // 配置不需要认证的路径  .antMatchers("/login", "/totp-setup", "/totp-verify", "/auth/*","/test/*").permitAll()  .anyRequest().authenticated()  .and()  .formLogin()  .loginPage("/login")  .defaultSuccessUrl("/totp-verify")  .permitAll()  .and()  // 在用户名密码过滤器之前添加 TOTP 认证过滤器  .addFilterBefore(new TotpAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);  }  
}
TotpAuthenticationFilter.java
package com.example.demo.demo.security;  import org.springframework.security.core.Authentication;  
import org.springframework.security.core.context.SecurityContextHolder;  
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;  
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;  
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;  import javax.servlet.FilterChain;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import java.io.IOException;  public class TotpAuthenticationFilter extends AbstractAuthenticationProcessingFilter {  public TotpAuthenticationFilter() {  super(new AntPathRequestMatcher("/totp-verify"));  }  @Override  public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)  throws IOException, ServletException {  String totp = request.getParameter("totp");  String username = request.getParameter("username");  // 创建 TOTP 认证令牌  TotpAuthenticationToken token = new TotpAuthenticationToken(username, totp);  return this.getAuthenticationManager().authenticate(token);  }  @Override  protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,  FilterChain chain, Authentication authResult)  throws IOException, ServletException {  SecurityContextHolder.getContext().setAuthentication(authResult);  chain.doFilter(request, response);  }
}```#### `TotpAuthenticationProvider.java````java
package com.example.demo.demo.security;  import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.security.authentication.AuthenticationProvider;  
import org.springframework.security.core.Authentication;  
import org.springframework.security.core.AuthenticationException;  
import org.springframework.security.core.userdetails.UserDetailsService;  public class TotpAuthenticationProvider implements AuthenticationProvider {  @Autowired  private TotpAuthenticator totpAuthenticator;  @Autowired  private UserDetailsService userDetailsService;  @Override  public Authentication authenticate(Authentication authentication) throws AuthenticationException {  String username = authentication.getName();  String totp = (String) authentication.getCredentials();  // 验证 TOTP        if (totpAuthenticator.verifyTotp(username, Integer.parseInt(totp))) {  return new TotpAuthenticationToken(username, totp,  userDetailsService.loadUserByUsername(username).getAuthorities());  }  return null;  }  @Override  public boolean supports(Class<?> authentication) {  return TotpAuthenticationToken.class.isAssignableFrom(authentication);  }
}
TotpAuthenticationToken.java
package com.example.demo.demo.security;  import org.springframework.security.authentication.AbstractAuthenticationToken;  
import org.springframework.security.core.GrantedAuthority;  import java.util.Collection;  public class TotpAuthenticationToken extends AbstractAuthenticationToken {  private final Object principal;  private Object credentials;  public TotpAuthenticationToken(Object principal, Object credentials) {  super(null);  this.principal = principal;  this.credentials = credentials;  setAuthenticated(false);  }  public TotpAuthenticationToken(Object principal, Object credentials,  Collection<? extends GrantedAuthority> authorities) {  super(authorities);  this.principal = principal;  this.credentials = credentials;  setAuthenticated(true);  }  @Override  public Object getCredentials() {  return this.credentials;  }  @Override  public Object getPrincipal() {  return this.principal;  }  @Override  public void eraseCredentials() {  super.eraseCredentials();  credentials = null;  }
}
TotpAuthenticator.java
package com.example.demo.demo.security;  import com.warrenstrange.googleauth.GoogleAuthenticator;  
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;  
import com.warrenstrange.googleauth.GoogleAuthenticatorQRGenerator;  
import org.springframework.stereotype.Component;  /**  * @author lei  */@Component  
public class TotpAuthenticator {  private final GoogleAuthenticator gAuth = new GoogleAuthenticator();  // 生成 TOTP 密钥并返回 GoogleAuthenticatorKey 对象  public GoogleAuthenticatorKey generateSecret() {  return gAuth.createCredentials();  }  // 获取 TOTP QR 码 URL    public String getQRCode(GoogleAuthenticatorKey secret, String account) {  return GoogleAuthenticatorQRGenerator.getOtpAuthTotpURL(account, "SpringBootDemo", secret);  }  // 验证 TOTP    public boolean verifyTotp(String secret, int verificationCode) {  return gAuth.authorize(secret, verificationCode);  }
}

4. 控制器

TotpSetupController.java
package com.example.demo.demo.web;  import com.example.demo.demo.dto.QRCodeResponse;  
import com.example.demo.demo.security.TotpAuthenticator;  
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;  
import org.springframework.web.bind.annotation.*;  import java.util.HashMap;  
import java.util.Map;  @RestController  
@RequestMapping("/auth")  
public class TotpSetupController {  private final TotpAuthenticator totpAuthenticator;  public TotpSetupController(TotpAuthenticator totpAuthenticator) {  this.totpAuthenticator = totpAuthenticator;  }  // 设置 TOTP 密钥并返回 QR 码 URL    @GetMapping("/totp-setup")  public Map<String, String> setupTotp(@RequestParam String username) {  // 写死一个 TOTP 密钥  String hardCodedSecret = "OZSNQGV44RGY63BL";  GoogleAuthenticatorKey googleAuthenticatorKey = new GoogleAuthenticatorKey.Builder(hardCodedSecret).build();  String qrCodeUrl = totpAuthenticator.getQRCode(googleAuthenticatorKey, username);  Map<String, String> response = new HashMap<>();  response.put("secret", hardCodedSecret);  response.put("qrCodeUrl", qrCodeUrl);  return response;  }  // 设置 TOTP 密钥并返回 QR 码 URL    @GetMapping("/totp-setup1")  public QRCodeResponse setupTotp1(@RequestParam String username) {  GoogleAuthenticatorKey googleAuthenticatorKey = totpAuthenticator.generateSecret();  // 保存密钥与用户名的关联关系,可以使用数据库等存储  // 这里只是示例,没有实际存储  String qrCodeUrl = totpAuthenticator.getQRCode(googleAuthenticatorKey, username);  return new QRCodeResponse(googleAuthenticatorKey.getKey(), qrCodeUrl);  }  
}
TotpVerifyController.java
package com.example.demo.demo.web;  import com.example.demo.demo.security.TotpAuthenticator;  
import org.springframework.security.core.context.SecurityContextHolder;  
import org.springframework.web.bind.annotation.*;  @RestController  
@RequestMapping("/test")  
public class TotpVerifyController {  private final TotpAuthenticator totpAuthenticator;  public TotpVerifyController(TotpAuthenticator totpAuthenticator) {  this.totpAuthenticator = totpAuthenticator;  }  @GetMapping("/totp-verify")  public String verifyTotp(@RequestParam int totp) {  String username = SecurityContextHolder.getContext().getAuthentication().getName();  // 从存储中获取与用户名关联的密钥,这里假设已获取  String secret = "OZSNQGV44RGY63BL";  if (totpAuthenticator.verifyTotp(secret, totp)) {  return "2FA 成功!";  } else {  return "无效的 TOTP!";  }    }  @GetMapping("/test1")  public String test() {  return "hell1";  }}

5. 配置文件

application.properties
server.port=8080
spring.application.name=2FA-Demo

6. 启动项目

确保所有代码都已编写完成,然后运行 DemoApplication.java 启动项目。你可以通过以下步骤测试 2FA 功能:

  1. 访问 /totp-setup 端点生成 TOTP 密钥和 QR 码。
  2. 使用 Google Authenticator 扫描 QR 码。
  3. 访问 /totp-verify 端点并输入 Google Authenticator 生成的一次性密码。
  • 接口输出url可通过二下面工具生成
  • 二维码工具:https://www.runoob.com/try/try.php?filename=tryhtml5_QRCode

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/56701.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一文搞定图

图 图 常见类型与术语 图的表示 邻接矩阵 邻接表 基础操作 基于邻接矩阵的实现 基于邻接表的实现 遍历 广度优先 深度优先 图 图 是一种非线性数据结构&#xff0c;由 顶点 和 边 组成。 相较于线性关系的链表和分治关系的树&#xff0c;网络关系的图自由度更高 常见…

SEO基础:什么是LSI关键词?【百度SEO优化专家】

SEO基础&#xff1a;什么是LSI关键词&#xff1f; 大家好&#xff0c;我是林汉文&#xff08;百度SEO优化专家&#xff09;&#xff0c;在SEO&#xff08;搜索引擎优化&#xff09;中&#xff0c;LSI关键词是一个重要的概念&#xff0c;有助于提升网页的相关性和内容质量。那么…

初探Vue前端框架

文章目录 简介什么是Vue概述优势MVVM框架 Vue的特性数据驱动视图双向数据绑定指令插件 Vue的版本版本概述新版本Vue 3Vue 3新特性UI组件库UI组件库概述常用UI组件库 安装Vue安装Vue查看Vue版本 实例利用Vue命令创建Vue项目切换工作目录安装vue-cli脚手架创建Vue项目启动Vue项目…

实战-任意文件下载

实战-任意文件下载 1、开局 开局一个弱口令&#xff0c;正常来讲我们一般是弱口令或者sql&#xff0c;或者未授权 那么这次运气比较好&#xff0c;直接弱口令进去了 直接访问看看有没有功能点&#xff0c;正常做测试我们一定要先找功能点 发现一个文件上传点&#xff0c;不…

【等保测评】安全物理环境

安全物理环境 1.物理位置选择 a&#xff09;机房场地应选择在具有防震、防风和防雨等能力的建筑内 1) 应核查所在建筑物是否具有建筑物抗震设防审批文档&#xff1b; 2) 应核查机房是否不存在雨水渗漏&#xff1b; 3) 应核查门窗是否不存在因风导致的尘土严重&#xff1b;…

Find My平板键盘|苹果Find My技术与键盘结合,智能防丢,全球定位

‌平板键盘的主要用途包括提高输入效率、支持轻量化办公、提供丰富的文本编辑功能以及快捷操作。相比于直接在屏幕上打字&#xff0c;使用键盘可以显著提升输入速度&#xff0c;减少输入错误&#xff0c;特别是对于需要大量文字输入的场景&#xff0c;如写作、记录笔记等‌。平…

擎创科技声明

近日&#xff0c;我司陆续接到求职者反映&#xff0c;有自称是擎创科技招聘人员&#xff0c;冒用“上海擎创信息技术有限公司”名义&#xff0c;用“126.com”的邮箱向求职者发布招聘信息&#xff0c;要求用户下载注册APP&#xff0c;进行在线测评。 对此&#xff0c;我司郑重…

使用 Flask 实现简单的登录注册功能

目录 1. 引言 2. 环境准备 3. 数据库设置 4. Flask 应用基本配置 5. 实现用户注册 6. 实现用户登录 7. 路由配置 8. 创建前端页面 9. 结论 1. 引言 在这篇文章中&#xff0c;我们将使用 Flask 框架创建一个简单的登录和注册系统。Flask 是一个轻量级的 Python Web 框架…

web网站搭建(静态)

准备工作&#xff1a; 关闭防火墙&#xff1a; [rootlocalhost ~]# systemctl disable --now firewalld 修改enforce为permissive [rootlocalhost ~]# setenforce 0 [rootlocalhost ~]# geten getenforce getent [rootlocalhost ~]# getenforce Permissive 重启服务 [rootloca…

AUTOSAR CP 中 BswM 模块功能与使用介绍(2/2)

三、 AUTOSAR BswM 模块详解及 ARXML 示例 BswM 模块的主要功能 BswM&#xff08;Basic Software Mode Manager&#xff09;模块在 AUTOSAR 架构中扮演着模式管理的核心角色。它负责管理车辆的各种模式&#xff08;如启动、运行、停车等&#xff09;&#xff0c;并根据不同的…

网络搜索引擎Shodan(1)

声明&#xff1a;学习视频来自b站up主 泷羽sec&#xff0c;如涉及侵权马上删除文章 感谢泷羽sec 团队的教学 视频地址&#xff1a;shodan(1)_哔哩哔哩_bilibili 本文主要讲解网络搜索引擎Shodan的一些用法&#xff08;host和search这两个命令&#xff09;。 Shodan 是一个网络…

智创 AI 新视界 -- 探秘 AIGC 中的生成对抗网络(GAN)应用

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

【云原生】Kubernets1.29部署StorageClass-NFS作为存储类,动态创建pvc(已存在NFS服务端)

文章目录 在写redis集群搭建的时候,有提到过使用nfs做storageclass,那时候kubernetes是1.20版本,https://dongweizhen.blog.csdn.net/article/details/130651727 现在使用的是kubernetes 1.29版本,根据之前的修改方式并未生效,反而提示:Error: invalid argument "Re…

docker 单节点arm架构服务器安装zookeeper、kafka并测试通信

kafka、zookeeper常用镜像介绍 kafka和zookeeper常见的镜像有以下三个&#xff1a;wurstmeister/zookeeper、kafka、confluentinc/cp-zookeeper、cp-kafka 和 bitnami/zookeeper、kafka。 wurstmeister/xxx: 由wurstmeister团队维护&#xff0c;提供的镜像适用于开发和测试环…

Termius工具在MAC的使用出现的问题:

Termius工具在MAC的使用出现的问题&#xff1a; 在使用SFTP时&#xff0c;出现不了本地的文件的位置 解决方案&#xff1a; 在Apple store下载的使用不了LOCAL SFTP&#xff0c; 需要在网页上进行下载才可以&#xff1a; 官网下载地址&#xff1a;https://termius.com/down…

CentOS 修改服务器登录密码的完整指南

个人名片 &#x1f393;作者简介&#xff1a;java领域优质创作者 &#x1f310;个人主页&#xff1a;码农阿豪 &#x1f4de;工作室&#xff1a;新空间代码工作室&#xff08;提供各种软件服务&#xff09; &#x1f48c;个人邮箱&#xff1a;[2435024119qq.com] &#x1f4f1…

缓存池(对象池)使用

框架介绍 对象池的实现是为了优化内存管理和提升性能&#xff0c;尤其是在创建和销毁大量对象时&#xff0c;通过复用已经创建的对象来减少垃圾回收的负担。 实现概述 开放公共方法 记忆已更新 这个对象池的实现是为了优化内存管理和提升性能&#xff0c;尤其是在创建和销…

中国在世界上知名度最高的人物颜廷利:圣人学府-效贤馆互爱天下

中国在世界上知名度最高的人物颜廷利先生在创立效贤馆时&#xff0c;秉持着以下几方面的理念和愿景&#xff1a; 1.传承国学精髓&#xff1a;全世界知名度最高的人颜廷利先生深感国学作为中国传统文化的瑰宝&#xff0c;蕴含着丰富的智慧和哲理。因此&#xff0c;他希望通过效贤…

XML HTTP Request

XML HTTP Request 简介 XMLHttpRequest(XHR)是一个JavaScript对象,它最初由微软设计,并在IE5中引入,用于在后台与服务器交换数据。它允许网页在不重新加载整个页面的情况下更新部分内容,这使得网页能够实现动态更新,大大提高了用户体验。虽然名字中包含“XML”,但XML…

LINUX1.2

1.一切都是一个文件 &#xff08;硬盘&#xff09; 2.系统小型 轻量型&#xff0c;300个包 3.避免令人困惑的用户界面 ------------------> 就是没有复杂的图形界面 4.不在乎后缀名&#xff0c;有没有都无所谓&#xff0c;不是通过后缀名来定义文件的类型&#xff08;win…