春季启动中的基本身份验证
时间:2025-02-11 15:49:16 390浏览 收藏
目前golang学习网上已经有很多关于文章的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《春季启动中的基本身份验证》,也希望能帮助到大家,如果阅读完后真的对你学习文章有帮助,欢迎动动手指,评论留言并分享~
Spring Security简明身份验证指南:基于HTTP Basic的认证
Spring Security负责Spring应用程序的身份验证和授权。本文将演示使用最基本的HTTP Basic身份验证方法来保护Spring Boot API。
首先,创建一个简单的Spring Boot应用程序,仅包含Spring Web和Spring Security依赖。 我们将添加一个简单的GET请求:
package com.example.spring_basic;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@GetMapping("/hello")
public String hello(){
return "Hello World!";
}
}
运行应用程序后,Spring Security会自动启用默认配置,但此时安全性并未真正生效。我们可以通过工具(如IntelliJ HTTP客户端)发送请求进行测试,你会发现请求可以成功访问 /hello
接口。
为了增强安全性,我们需要添加自定义的Spring Security配置:
package com.example.spring_basic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import static org.springframework.security.config.Customizer.withDefaults;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.httpBasic(withDefaults());
http.authorizeHttpRequests(httpSecurity -> {
httpSecurity.requestMatchers("/users").permitAll();
httpSecurity.anyRequest().authenticated();
});
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
UserDetails user = User.builder()
.username("user")
.password(passwordEncoder().encode("password"))
.roles("user")
.build();
UserDetails admin = User.builder()
.username("admin")
.password(passwordEncoder().encode("password"))
.roles("user", "admin")
.build();
return new InMemoryUserDetailsManager(user, admin);
}
}
此配置使用httpBasic(withDefaults())
启用HTTP Basic身份验证,并定义了两个用户:"user"和"admin",密码均为"password"(已使用BCryptPasswordEncoder加密)。 /users
路径允许匿名访问,其他所有请求都需要身份验证。
现在,我们需要使用用户名和密码进行身份验证才能访问 /hello
接口:
GET http://localhost:8080/hello
Authorization: Basic dXNlcjpwYXNzd29yZA== // "user:password" 的Base64编码
工作原理:
当客户端发送请求时,BasicAuthenticationFilter
拦截请求,提取Authorization
header中的凭据。 AuthenticationManager
调用DaoAuthenticationProvider
,使用InMemoryUserDetailsManager
验证用户名和密码。 如果验证成功,则将身份验证信息添加到SecurityContext,允许访问受保护的资源。
项目源码:此处替换为实际源码链接或说明
通过以上步骤,我们成功地使用HTTP Basic身份验证保护了Spring Boot应用程序。 记住,在生产环境中,应使用更安全的身份验证方法,例如OAuth 2.0或JWT。
到这里,我们也就讲完了《春季启动中的基本身份验证》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
364 收藏
-
478 收藏
-
464 收藏
-
347 收藏
-
468 收藏
-
200 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习