繁体   English   中英

如何在spring boot的graphql突变中从服务器端api设置Cookie?

[英]How to set Cookie from server side api in graphql mutation in spring boot?

我尝试开发服务器端 API。 我正在使用 GraphQL 并想从服务器端设置 cookie,但是当我尝试使用HttpServletResponse设置 cookie 时,它​​给了我一个NullPointerException

这是我的响应视图类:

package org.jembi.appstore.service.graphql.api.response.view;

public class ResponseView {

    private int code;
    private String message;

    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

我在这里设置 cookie:

@GraphQLMutation(name="login")
public ResponseView login(@GraphQLArgument(name="msisdn") String msisdn, @GraphQLArgument(name="password")String password, HttpServletResponse response) {

    ResponseView responseView = new ResponseView();
    User user = new User();


    System.out.println(msisdn.length());
    Cookie cookie = null;
    if(msisdn.length()==10) {
        user.setPassword(password);
        user.setMsisdn(msisdn);

        int code = userDao.getUser(user);
        responseView.setCode(code);
        if(code==100) {
            responseView.setMessage("User Logged In Successfully");

            cookie = new Cookie("token", UUID.randomUUID().toString());
            //responseView.doPost(req, resp);
            int hour = 3600000;
            int exp = 14 * 24 * hour; //2 weeks

            cookie.setMaxAge(exp);
            //cookie.setPath("/");
            cookie.setHttpOnly(true);
            response.addCookie(cookie);
            //responseView.setCookie(cookie);

        }   
        else 
            responseView.setMessage("Invalid credentials");

    }else {
        responseView.setCode(400);
        responseView.setMessage("Bad Request");
    }
    return  responseView;
}

从代码看来,您正在使用 GraphQL SPQR。 我不知道你是否也在使用 SPQR Spring Starter,所以我只能假设你是,因为你的问题没有提到任何内容。

在最近的版本中, DefaultGlobalContext<NativeWebRequest>的实例被自动提供为 GraphQL 上下文,因此它可以通过@GraphQLRootContext注入:

@GraphQLMutation(name="login")
public ResponseView login(
     @GraphQLArgument(name="msisdn") String msisdn, 
     @GraphQLArgument(name="password")String password,
     @GraphQLRootContext DefaultGlobalContext<NativeWebRequest> context) {

    HttpServletResponse response = context.getNativeRequest().getNativeResponse(HttpServletResponse.class);
    ... //do what you need to with the raw HTTP response
}

如果要提供不同的上下文,请注册ServletContextFactory类型的自定义 bean。

如果您不使用 SPQR Spring Starter,则必须确保自己通过ExecutionInput提供适用的上下文:

ExecutionInput input = ExecutionInput.newExecutionInput()
        .query(graphQLRequest.getQuery())
        .operationName(graphQLRequest.getOperationName())
        .variables(graphQLRequest.getVariables())
        .context(response) //HttpServletResponse or whatever you need
        .build();

如果您使用的是graphql-java-kickstart / graphql-java-servlet

假设你可以得到

DataFetchingEnvironment env

那么你可以这样做:

GraphQLServletContext context = env.getContext();
context.getHttpServletResponse().addCookie(cookie);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM