การยืนยันตัวตนผู้ใช้ในหน่วยความจำ
นำการยืนยันตัวตนพื้นฐานไปใช้ด้วยรายละเอียดผู้ใช้ในหน่วยความจำ โดยกำหนดชื่อผู้ใช้ รหัสผ่าน และบทบาทโดยตรง
การยืนยันตัวตนผู้ใช้ในหน่วยความจำ เป็นบทเรียน Spring Security 6 & JWT Authentication ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Security 6 & JWT Authentication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to In-Memory Auth
The fastest way to add real users: in-memory authentication. You define accounts straight in code — perfect for tests and tiny apps.
Why In-Memory Authentication?
In-memory authentication shines for prototyping, learning, and small apps where you don't need users persisted to a database.
Core: UserDetailsService
The UserDetailsService interface is the core of authentication — its job is to load user data. In-memory mode loads it from accounts you define in code.
Meet UserDetails
UserDetailsService returns a UserDetails object holding the username, an always-encoded password, and the authorities that define what the user can access.
Defining a User with Builder
Use User.builder() to create a UserDetails: set the username, an encoded password, and roles. The code shows a minimal user.
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;
public class UserBuilderDemo {
public static void main(String[] args) {
// A PasswordEncoder is essential for security
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
UserDetails user = User.builder()
.username("alice")
.password(passwordEncoder.encode("secret")) // Encode the password!
.roles("USER") // Short for ROLE_USER
.build();
System.out.println("Username: " + user.getUsername());
System.out.println("Password (encoded): " + user.getPassword());
System.out.println("Authorities: " + user.getAuthorities());
}
}The InMemoryUserDetailsManager
The InMemoryUserDetailsManager is the default UserDetailsService for in-memory auth, storing your UserDetails in a simple map. Pass users to its constructor.
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
public class InMemoryManagerDemo {
public static void main(String[] args) {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
UserDetails user1 = User.builder()
.username("bob")
.password(passwordEncoder.encode("pass123"))
.roles("USER")
.build();
UserDetails admin = User.builder()
.username("admin")
.password(passwordEncoder.encode("adminpass"))
.roles("ADMIN", "USER") // Multiple roles
.build();
// Create the manager with our users
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(user1, admin);
// Try to load a user
System.out.println("Loaded user: " + manager.loadUserByUsername("bob").getUsername());
System.out.println("Loaded admin: " + manager.loadUserByUsername("admin").getUsername());
}
}Configuring the Manager Bean
In Boot, expose the InMemoryUserDetailsManager as a @Bean and Spring picks it up automatically. Just remember to also provide a PasswordEncoder bean.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configuration
public class SimpleSecurityConfig {
@Bean
public UserDetailsService inMemoryUsers(PasswordEncoder passwordEncoder) {
UserDetails user = User.builder()
.username("tester")
.password(passwordEncoder.encode("testpass"))
.roles("VIEWER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// Other HttpSecurity configurations would go here...
}Roles vs. Authorities
Use roles and Spring auto-prefixes them with ROLE_ (USER becomes ROLE_USER). For fine-grained permissions, .authorities() takes the full string as-is.
Multiple Users and Roles
Add as many in-memory users as you need, each with different roles, to test access-control scenarios — just pass more UserDetails to the manager.
Quick Check
You've learned how to set up users in memory. Which Spring Security component is specifically used to manage and retrieve these in-memory user details?
Recap: In-Memory Auth
Recap: in-memory authentication lets you define users via User.builder(), store them in InMemoryUserDetailsManager, encode passwords, and assign roles.
คำถามที่พบบ่อย
บทเรียน “การยืนยันตัวตนผู้ใช้ในหน่วยความจำ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การยืนยันตัวตนผู้ใช้ในหน่วยความจำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Security 6 & JWT Authentication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การยืนยันตัวตนผู้ใช้ในหน่วยความจำ”
นำการยืนยันตัวตนพื้นฐานไปใช้ด้วยรายละเอียดผู้ใช้ในหน่วยความจำ โดยกำหนดชื่อผู้ใช้ รหัสผ่าน และบทบาทโดยตรง คุณปฏิบัติ Spring Security 6 & JWT Authentication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Security 6 & JWT Authentication หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Security 6 & JWT Authentication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การยืนยันตัวตนผู้ใช้ในหน่วยความจำ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Security 6 & JWT Authentication นี้ได้ไหม
ได้ บทเรียน Spring Security 6 & JWT Authentication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ Spring Security 6
- การตั้งค่าโปรเจกต์และการพึ่งพา
- การยืนยันตัวตนผู้ใช้ในหน่วยความจำ
- ทำความเข้าใจสายโซ่ตัวกรองของ Spring Security