[英]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.