我在Microsoft Sharepoint中有一个清单,该清单正试图通过Spring进行编辑。我已经编写了两个控制器,一个用于获取清单的状态,另一个用于更新清单。我可以获取清单的状态,但是当我要更新清单时,出现401 Unauthorized错误。当我使用从Spring服务获得的上下文formDigestValue时,在Postman中发出PATCH请求时,出现错误“ T 此页面的安全验证无效,并且可能已损坏。请使用Web浏览器的“后退”按钮再次尝试操作。” 但是,如果我在Postman中使用Sharepoint本机API获取上下文formDigestValue,并使用它进行PATCH请求,则可以成功更新清单。我无法理解为什么在使用Spring做同样的事情时输出会出现差异。
package com.example.sharepoint.client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.example.sharepoint.constants.SharepointConstants;
import com.example.sharepoint.exception.SharepointClientException;
import com.example.sharepoint.vo.ContextVO;
import com.example.sharepoint.vo.SelectiveVO;
@Service
public class SharepointClient {
private final Logger logger = LoggerFactory.getLogger(SharepointClient.class);
@Autowired
private RestTemplate restTemplate;
@Value("${vmsource.sharepoint.url}")
private String vmsourceSharepointURL;
@Value("${selectiveGet.url}")
private String selectiveGetURL;
@Value("${getContextInfo.url}")
private String getContextInfoURL;
@Value("${updateTasks.url}")
private String updateTasksURL;
@Value("${header.accept}")
private String headerAcceptValue;
@Value("${header.contentType}")
private String headerContentType;
@Value("${header.ifMatch}")
private String headerIfMatch;
/**
* This method to get the status of Checklist
*
* return the json data
*/
public SelectiveVO getStatus(String email) {
SelectiveVO resultVO = null;
try {
logger.info("selectiveGetURLB : {} ", selectiveGetURL);
String selectiveGetURL1 = selectiveGetURL + " '" +email +"'";
logger.info("selectiveGetURL : {} ", selectiveGetURL1);
resultVO = restTemplate
.exchange(selectiveGetURL1, HttpMethod.GET, getEntity(headerAcceptValue), SelectiveVO.class)
.getBody();
return resultVO;
} catch (Exception exception) {
logger.error("Caught error while getting status : {} ", exception);
throw new SharepointClientException(exception);
}
}
private HttpEntity<String> getEntity(String acceptValue) {
HttpHeaders headers = new HttpHeaders();
headers.set(SharepointConstants.ACCEPT, acceptValue);
HttpEntity<String> entity = new HttpEntity<String>(headers);
return entity;
}
// Method to get context Info
public String getContextInfo() {
ContextVO resultVO = null;
String formDigestValue ="";
try {
logger.info("contextinfoURL : {} ", getContextInfoURL);
resultVO = restTemplate
.exchange(getContextInfoURL, HttpMethod.POST, getEntity(headerAcceptValue), ContextVO.class)
.getBody();
formDigestValue = resultVO.getD().getContextWebInfo().getFormDigestValue();
logger.info("formDigestValue : {} ", formDigestValue);
return formDigestValue ;
}catch (Exception exception) {
logger.error("Caught error while getting context : {} ", exception);
throw new SharepointClientException(exception);}
}
//Method to update checklist : I am getting Id and JSON body from controller
public Object updateTasks(String id , String body) {
try {
String updateTasksURL1 = updateTasksURL + "('" + id + "')";
logger.info("updateTasksURL : {} ", updateTasksURL1);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(requestFactory);
String c=getContextInfo();
ResponseEntity<String> response = restTemplate
.exchange(updateTasksURL1, HttpMethod.PATCH, getEntity(body , headerAcceptValue,c,"PATCH","*",headerContentType), String.class);
return response ;
}catch (Exception exception) {
logger.error("Caught error while getting updating task : {} ", exception);
throw new SharepointClientException(exception);}
}
private HttpEntity<String> getEntity(String body , String acceptValue , String requestDigest , String xHttpMethod , String ifMatch , String contentType){
HttpHeaders headers = new HttpHeaders();
headers.set(SharepointConstants.ACCEPT, acceptValue);
headers.set(SharepointConstants.XREQUESTDIGEST, requestDigest);
headers.set(SharepointConstants.XHTTPMETHOD, xHttpMethod);
headers.set(SharepointConstants.IFMATCH,ifMatch);
headers.set(SharepointConstants.CONTENTTYPE, contentType);
HttpEntity<String> entity = new HttpEntity<String>(body , headers);
logger.info("headers : {} ", entity);
return entity;
}
}
添加错误日志
2018-08-05 03:19:08.890 INFO 16224 --- [nio-6464-exec-1] c.e.sharepoint.client.SharepointClient : headers : < {
"__metadata": {"type":"SP.Data.CheckListListItem"},
"Badge_x0020_Received": "Completed"
},{Accept=[application/json;odata=verbose], X-RequestDigest=[0xCEFCB58A8B97710BC668DEB315FF494FF2AC7124E3210DCC4D15342D9DBACF8C00B40A0DBE3271DBFF2E3A4C3C71EE229814F680CC06219BFDD4719745E7AA39,04 Aug 2018 21:49:08 -0000], X-HTTP-Method=[PATCH], If-Match=[*], Content-Type=[application/json;odata=verbose]}>
2018-08-05 03:19:09.702 ERROR 16224 --- [nio-6464-exec-1] c.e.sharepoint.client.SharepointClient : Caught error while getting updating task : {}
org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) ~[spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766) ~[spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:724) ~[spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680) ~[spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600) ~[spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at com.example.sharepoint.client.SharepointClient.updateTasks(SharepointClient.java:105) ~[classes!/:0.0.1-SNAPSHOT]
at com.example.sharepoint.service.SharepointService.updateTasks(SharepointService.java:35) [classes!/:0.0.1-SNAPSHOT]
at com.example.sharepoint.controller.SharepointController.updateTasks(SharepointController.java:40) [classes!/:0.0.1-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_151]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar!/:8.5.31]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_151]
2018-08-05 03:19:09.706 ERROR 16224 --- [nio-6464-exec-1] c.e.s.service.SharepointService : DirectAccessClientException while performing update in service : {}
com.example.sharepoint.exception.SharepointClientException: org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at com.example.sharepoint.client.SharepointClient.updateTasks(SharepointClient.java:110) ~[classes!/:0.0.1-SNAPSHOT]
at com.example.sharepoint.service.SharepointService.updateTasks(SharepointService.java:35) ~[classes!/:0.0.1-SNAPSHOT]
at com.example.sharepoint.controller.SharepointController.updateTasks(SharepointController.java:40) [classes!/:0.0.1-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) [spring-web-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]