简体   繁体   English

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

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

I tried developing a server side API.我尝试开发服务器端 API。 I am using GraphQL and want to set a cookie from the server side, but when i try to set the cookie using HttpServletResponse it gives me a NullPointerException ?我正在使用 GraphQL 并想从服务器端设置 cookie,但是当我尝试使用HttpServletResponse设置 cookie 时,它​​给了我一个NullPointerException

Here is my Response View class:这是我的响应视图类:

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;
    }
}

And here I am setting the cookie:我在这里设置 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;
}

From the code it seems you're using GraphQL SPQR.从代码看来,您正在使用 GraphQL SPQR。 I can't tell if you're using SPQR Spring Starter as well or not, so I'll just have to assume you are, since your question mentions nothing.我不知道你是否也在使用 SPQR Spring Starter,所以我只能假设你是,因为你的问题没有提到任何内容。

In recent versions, an instance of DefaultGlobalContext<NativeWebRequest> is automatically provided as GraphQL context, so it can be inject by @GraphQLRootContext :在最近的版本中, 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
}

If you want to provide a different context, register a custom bean of ServletContextFactory type.如果要提供不同的上下文,请注册ServletContextFactory类型的自定义 bean。

In case you're not using SPQR Spring Starter, you'll have to ensure you provide the applicable context yourself via ExecutionInput :如果您不使用 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();

If you are using graphql-java-kickstart / graphql-java-servlet如果您使用的是graphql-java-kickstart / graphql-java-servlet

Assuming that you can get the假设你可以得到

DataFetchingEnvironment env

then you can do this:那么你可以这样做:

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

暂无
暂无

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

相关问题 带有 GraphQL 服务器的 Spring Boot 未启动 - Spring Boot with GraphQL server not starting 如何从 java Z91F1F487C7FB768642B3EB98F683BF5 应用程序调用 GraphQL api? 是否有任何注释支持 graphQL 查询格式? - How to invoke GraphQL api from a java spring-boot application? Is there any annotation which supports graphQL query formation? 如何从服务器获取 session cookie 并将其设置到 api header 中? - how to get session cookie from server and set it into the api header? Set-Cookie标头已从响应中删除,Spring启动 - Set-Cookie header is removed from response, Spring boot 如何使用 Spring Boot 设置 JSESSION cookie 的过期日期时间 - How to set expiration date-time of JSESSION cookie with Spring Boot 在Spring Boot控制器中使用GraphQL API - Consuming GraphQL API in Spring Boot controller 如何在 Spring Boot REST API 上设置超时? - How to set a timeout on a Spring Boot REST API? 使用 webclient 在 spring 启动中调用 grapql 突变 API - using webclient to call the grapql mutation API in spring boot 如何在 Angular/Spring Boot 应用程序中接收服务器端的表单数据? - How to receive form data on the server side in an Angular/Spring Boot application? 我们如何从 GraphQL 的 RequestContextHolder 中获取 GraphQL 的 HttpServletRequest(DefaultGlobalContext)(使用 graphql-spqr-spring-boot-starter)? - How can we get HttpServletRequest(DefaultGlobalContext) for GraphQL from RequestContextHolder for GraphQL (using graphql-spqr-spring-boot-starter)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM