From 73ded46dc8cb31b80f9c1f851a5995ffef571a36 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 29 Aug 2017 14:21:39 +0900 Subject: [PATCH] Support GRpc metadata --- .../overflow/commons/model/SessionMetadata.java | 15 +++++++++++++++ .../module/member/service/MemberService.java | 5 ++++- .../overflow/proxy/ProxyServerInterceptor.java | 15 +++++++++++++++ .../com/loafle/overflow/proxy/ServiceProxy.java | 8 +++----- 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/loafle/overflow/commons/model/SessionMetadata.java create mode 100644 src/main/java/com/loafle/overflow/proxy/ProxyServerInterceptor.java diff --git a/src/main/java/com/loafle/overflow/commons/model/SessionMetadata.java b/src/main/java/com/loafle/overflow/commons/model/SessionMetadata.java new file mode 100644 index 0000000..e285db6 --- /dev/null +++ b/src/main/java/com/loafle/overflow/commons/model/SessionMetadata.java @@ -0,0 +1,15 @@ +package com.loafle.overflow.commons.model; + +import io.grpc.Context; +import io.grpc.Metadata; + +import static io.grpc.Metadata.ASCII_STRING_MARSHALLER; + +public class SessionMetadata { + public static final Context.Key CTX_EMAIL_KEY = Context.key("email"); + public static final Metadata.Key METADATA_EMAIL_KEY = Metadata.Key.of("email", ASCII_STRING_MARSHALLER); + + public static String getEmail() { + return CTX_EMAIL_KEY.get(); + } +} diff --git a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java index c93db1c..4f8281f 100644 --- a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java +++ b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java @@ -1,5 +1,6 @@ package com.loafle.overflow.module.member.service; +import com.loafle.overflow.commons.model.SessionMetadata; import com.loafle.overflow.module.email.service.EmailAuthService; import com.loafle.overflow.module.member.dao.MemberDAO; import com.loafle.overflow.module.member.exception.*; @@ -68,6 +69,7 @@ public class MemberService { } public Member modify(Member member, String pw) { + String email = SessionMetadata.getEmail(); Member preMember = this.memberDAO.findByEmail(member.getEmail()); @@ -87,7 +89,6 @@ public class MemberService { } public Member confirmPw(String signinId, String signinPw) { - Member preMember = this.memberDAO.findByEmail(signinId); String encodePw = passwordEncoder.encode(signinPw); @@ -125,6 +126,8 @@ public class MemberService { } public void withdrawal(Member member) { + String email = SessionMetadata.getEmail(); + // Todo DB delete? } diff --git a/src/main/java/com/loafle/overflow/proxy/ProxyServerInterceptor.java b/src/main/java/com/loafle/overflow/proxy/ProxyServerInterceptor.java new file mode 100644 index 0000000..815634e --- /dev/null +++ b/src/main/java/com/loafle/overflow/proxy/ProxyServerInterceptor.java @@ -0,0 +1,15 @@ +package com.loafle.overflow.proxy; + +import com.loafle.overflow.commons.model.SessionMetadata; +import io.grpc.*; + +public class ProxyServerInterceptor implements ServerInterceptor { + + @Override + public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, ServerCallHandler next) { + String email = headers.get(SessionMetadata.METADATA_EMAIL_KEY); + Context ctx = Context.current().withValue(SessionMetadata.CTX_EMAIL_KEY, email); + + return Contexts.interceptCall(ctx, call, headers, next); + } +} diff --git a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java index a56594d..d7fbb22 100644 --- a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java +++ b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java @@ -5,10 +5,7 @@ import com.loafle.overflow.api.ServerInput; import com.loafle.overflow.api.ServerOutput; import com.loafle.overflow.commons.exception.OverflowException; import com.loafle.overflow.commons.exception.OverflowRuntimeException; -import io.grpc.ServerBuilder; -import io.grpc.Status; -import io.grpc.StatusException; -import io.grpc.StatusRuntimeException; +import io.grpc.*; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -27,9 +24,10 @@ public class ServiceProxy { public void start(int port) throws IOException { ctx = new AnnotationConfigApplicationContext("com.loafle.overflow"); + ProxyServerInterceptor proxyServerInterceptor = new ProxyServerInterceptor(); server = ServerBuilder.forPort(port) - .addService(new ServiceImpl(new ServiceInvoker(ctx))) + .addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor)) .build() .start(); logger.info("Server started, listening on " + port);