# พื้นฐาน Spring Security (Spring Boot) > Spring Security, การยืนยันตัวตน, การให้สิทธิ์, SecurityFilterChain, UserDetailsService, PasswordEncoder - 25 คำถามสัมภาษณ์ - Mid-Level - [คำถามสัมภาษณ์: Spring Boot](https://sharpskill.dev/th/technologies/spring-boot/interview-questions.md) ## 1. Spring Security คืออะไร? **คำตอบ** Spring Security เป็นเฟรมเวิร์กด้านความปลอดภัยสำหรับแอปพลิเคชัน Spring ที่ให้ฟีเจอร์การยืนยันตัวตนและการให้สิทธิ์ มันปกป้องแอปพลิเคชันจากการโจมตีทั่วไป (CSRF, XSS, session fixation) และมีการผสานรวมกับกลไกการยืนยันตัวตนที่หลากหลาย (form login, HTTP Basic, OAuth2, JWT) เฟรมเวิร์กใช้ห่วงโซ่ฟิลเตอร์ (Security Filter Chain) เพื่อดักจับและประมวลผลคำขอ HTTP ก่อนที่จะไปถึง controller ## 2. ความแตกต่างระหว่างการยืนยันตัวตนและการให้สิทธิ์ใน Spring Security คืออะไร? **คำตอบ** การยืนยันตัวตนจะตรวจสอบตัวตนของผู้ใช้ (คุณเป็นใคร?) โดยทั่วไปผ่าน username/password ในขณะที่การให้สิทธิ์จะตรวจสอบสิทธิ์ (คุณทำอะไรได้บ้าง?) การยืนยันตัวตนเกิดขึ้นก่อนและสร้าง SecurityContext ที่มีข้อมูลของผู้ใช้ จากนั้นการให้สิทธิ์จะใช้ข้อมูลนี้เพื่อตัดสินว่าผู้ใช้สามารถเข้าถึงทรัพยากรเฉพาะได้หรือไม่ ตัวอย่างเช่น ผู้ใช้อาจยืนยันตัวตนสำเร็จแต่ไม่ได้รับสิทธิ์ให้เข้าถึง /admin หากบทบาทของเขาคือ USER ไม่ใช่ ADMIN ## 3. SecurityContext ใน Spring Security คืออะไร? **คำตอบ** SecurityContext เป็นออบเจ็กต์ที่บรรจุข้อมูลความปลอดภัยของผู้ใช้ที่ยืนยันตัวตนอยู่ในปัจจุบัน โดยหลักคือออบเจ็กต์ Authentication มันถูกจัดเก็บไว้ใน SecurityContextHolder และเข้าถึงได้ตลอดการประมวลผลคำขอ หลังจากยืนยันตัวตนสำเร็จ Spring Security จะสร้าง SecurityContext ที่บรรจุ Authentication พร้อมรายละเอียดของผู้ใช้ (username, authorities, credentials) บริบทนี้ช่วยให้แอปพลิเคชันสามารถตรวจสอบได้ว่าผู้ใช้ปัจจุบันคือใครและมีสิทธิ์อะไรบ้าง ## มีอีก 22 คำถาม - SecurityContextHolder จัดเก็บ SecurityContext โดยค่าเริ่มต้นอย่างไร? - อินเทอร์เฟซ UserDetails ใน Spring Security มีบทบาทอะไร? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ Spring Boot อื่นๆ - [Spring Core - IoC & DI](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-core-ioc-di.md): 22 คำถาม, Junior - [Spring Boot Auto-Configuration](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-boot-auto-configuration.md): 20 คำถาม, Junior - [Spring Boot Starters](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-boot-starters.md): 18 คำถาม, Junior - [Application Properties & YAML](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/application-properties-yml.md): 16 คำถาม, Junior - [การทำ Logging ด้วย SLF4J และ Logback](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/logging.md): 20 คำถาม, Junior - [Spring Boot DevTools](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-boot-devtools.md): 15 คำถาม, Junior - [Spring MVC Basics](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-mvc-basics.md): 20 คำถาม, Mid-Level - [Spring REST Controllers](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-rest-controllers.md): 20 คำถาม, Mid-Level - [Request & Response Handling](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/request-response-handling.md): 20 คำถาม, Mid-Level - [การจัดการข้อยกเว้น](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/exception-handling.md): 25 คำถาม, Mid-Level - [Bean Validation](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/bean-validation.md): 25 คำถาม, Mid-Level - [พื้นฐาน Spring Data JPA](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-data-jpa-basics.md): 25 คำถาม, Mid-Level - [เอนทิตี JPA และความสัมพันธ์](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/jpa-entities-relationships.md): 30 คำถาม, Mid-Level - [การสืบค้น JPA](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/jpa-queries.md): 30 คำถาม, Mid-Level - [Spring Data Repositories](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-data-repositories.md): 25 คำถาม, Mid-Level - [Spring Boot Actuator](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-boot-actuator.md): 20 คำถาม, Mid-Level - [การทดสอบหน่วยด้วย JUnit และ Mockito](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/testing-junit-mockito.md): 30 คำถาม, Mid-Level - [การทดสอบ Spring Boot](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-boot-testing.md): 30 คำถาม, Mid-Level - [Profiles และ Environment](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/profiles-environment.md): 20 คำถาม, Mid-Level - [RestTemplate และ WebClient](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/resttemplate-webclient.md): 24 คำถาม, Mid-Level - [Async และ Scheduling](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/async-scheduling.md): 25 คำถาม, Mid-Level - [Caching ด้วย Spring](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/caching.md): 25 คำถาม, Mid-Level - [Spring WebFlux (Reactive)](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-webflux.md): 25 คำถาม, Mid-Level - [ทรานแซกชัน Spring](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/transactions.md): 30 คำถาม, Senior - [การยืนยันตัวตนและการให้สิทธิ์ขั้นสูง](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/authentication-authorization.md): 30 คำถาม, Senior - [JWT และความปลอดภัยแบบ Stateless](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/jwt-stateless-security.md): 20 คำถาม, Senior - [OAuth2 และ Authorization Server](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/oauth2-authorization-server.md): 20 คำถาม, Senior - [Spring Boot และ Docker](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-boot-docker.md): 19 คำถาม, Senior - [Microservices ด้วย Spring](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/microservices-basics.md): 25 คำถาม, Senior - [Spring Cloud Config](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-cloud-config.md): 19 คำถาม, Senior - [การเพิ่มประสิทธิภาพการทำงาน](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/performance-optimization.md): 30 คำถาม, Senior - [GraalVM Native Images](https://sharpskill.dev/th/technologies/spring-boot/interview-questions/graalvm-native-images.md): 20 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/spring-boot/interview-questions/spring-security-basics