Support GRpc metadata

This commit is contained in:
crusader 2017-08-29 14:21:39 +09:00
parent f1dde3b127
commit 73ded46dc8
4 changed files with 37 additions and 6 deletions

View File

@ -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<String> CTX_EMAIL_KEY = Context.key("email");
public static final Metadata.Key<String> METADATA_EMAIL_KEY = Metadata.Key.of("email", ASCII_STRING_MARSHALLER);
public static String getEmail() {
return CTX_EMAIL_KEY.get();
}
}

View File

@ -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?
}

View File

@ -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 <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> 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);
}
}

View File

@ -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);