Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
web filter 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Jan 23, 2024
1 parent 260860a commit eed58c6
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.daemawiki.global.security;

import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

public class JwtWebFilter implements WebFilter {
private final Tokenizer tokenizer;

public JwtWebFilter(Tokenizer tokenizer) {
this.tokenizer = tokenizer;
}

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return resolveToken(exchange.getRequest())
.flatMap(token -> {
if (tokenizer.verify(token)) {
Authentication authentication = tokenizer.getAuthentication(token);
return chain.filter(exchange)
.contextWrite(ReactiveSecurityContextHolder.withAuthentication(authentication));
} else {
return chain.filter(exchange);
}
});
}

private Mono<String> resolveToken(ServerHttpRequest request) {
return Mono.justOrEmpty(request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION))
.filter(authHeader -> authHeader.startsWith("Bearer "))
.map(authHeader -> authHeader.substring(7));
}

}

0 comments on commit eed58c6

Please sign in to comment.