[SpringBoot/Firebase] 파이어베이스 로그인 인증 토큰 사용하기
파이어베이스 로그인을 통해 들어오는 토큰 정보를 이용하여 사용자를 판별해봅시다 ~
[ Spring Security & JWT 로 Firebase Authentication Token 분석하기 ]
0. 참고 문서
🏠 https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/jwt.html
OAuth 2.0 Resource Server JWT :: Spring Security
Most Resource Server support is collected into spring-security-oauth2-resource-server. However, the support for decoding and verifying JWTs is in spring-security-oauth2-jose, meaning that both are necessary in order to have a working resource server that s
docs.spring.io
https://medium.com/comsystoreply/authentication-with-firebase-auth-and-spring-security-fcb2c1dc96d
Authentication with Firebase Auth and Spring Security
There isn’t a software developer in the world that hasn’t once in his life faced a question of authentication. Let’s try to make it easier.
medium.com
1. build.gradle
...
dependencies {
...
// spring security
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.security:spring-security-test'
// oauth
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
}
2. application.yml
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://www.googleapis.com/service_accounts/v1/jwk/securetoken%40system.gserviceaccount.com
issuer-uri: https://securetoken.google.com/${FIREBASE_APP_NAME}
3. Configuration
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((auth) -> auth
.requestMatchers("/").permitAll()
.anyRequest().authenticated()
)
.oauth2ResourceServer((oauth2) -> oauth2
.jwt(Customizer.withDefaults()));
return http.build();
}
...
}
4. Controller
✅ JWT 토큰으로 현재 로그인한 사용자 uuid 조회
- 첫 번째 방법
@GetMapping("/app/test")
public String test(Principal principal) {
return principal.getName();
}
- 두 번째 방법
@GetMapping("/app/test")
public String test() {
return SecurityContextHolder.getContext().getAuthentication().getName();
}