1

我正在尝试将条目发布到在 SAP ABAP 后端创建的 Odata 服务 Url。当我尝试通过 Odata 服务将数据从 java 代码发送到 SAP ABAP 系统时,我收到CSRF Token validation error。以下是 Odata Post 服务的代码片段

    ODataConsumer.Builder builder = ODataConsumers.newBuilder(URL_ODATASERVICE);
    // LOGGER.info(TAG+"Authentication values are been set");

    builder.setClientBehaviors(new BasicAuthenticationBehavior(USERNAME, PASSWORD), new SAPCSRFBehavior());

    ODataConsumer consumer = builder.build();

    OCreateRequest<OEntity> createRequest = consumer.createEntity("LogSet")
            .properties(OProperties.string("TestplanId", "111")).properties(OProperties.string("ProcessId", "222"))
            .properties(OProperties.string("Seqno", "33"));

    // Execute the OData post
    OEntity newMaterial = createRequest.execute();

SAPSCRBehaviour 类将是

public class SAPCSRFBehaviour implements JerseyClientBehavior {
private static final String CSRF_HEADER = "X-CSRF-Token";
private static final String SAP_COOKIES = "SAP_SESSIONID";
private String xsrfCookieName;
private String xsrfCookieValue;
private String xsrfTokenValue;

@Override
public ODataClientRequest transform(ODataClientRequest request) {
    if (request.getMethod().equals("GET")) {
        request = request.header(CSRF_HEADER, "Fetch");
        return request;
    } else {
        return request.header(CSRF_HEADER, xsrfTokenValue).header("Cookie", xsrfCookieName + "=" + xsrfCookieValue);
    }
}

@Override
public void modifyWebResourceFilters(final Filterable arg0) {
}

@Override
public void modifyClientFilters(final Filterable client) {
    client.addFilter(new ClientFilter() {
        @Override
        public ClientResponse handle(final ClientRequest clientRequest) throws ClientHandlerException {
            ClientResponse response = getNext().handle(clientRequest);
            List<NewCookie> cookies = response.getCookies();
            for (NewCookie cookie : cookies) {
                if (cookie.getName().startsWith(SAP_COOKIES)) {
                    xsrfCookieName = cookie.getName();
                    xsrfCookieValue = cookie.getValue();
                    break;
                }
            }
            MultivaluedMap<String, String> responseHeaders = response.getHeaders();
            xsrfTokenValue = responseHeaders.getFirst(CSRF_HEADER);
            return response;
        }
    });
}

@Override
public void modify(final ClientConfig arg0) {
}}

请建议我避免此问题的解决方案

最好的问候, 纳文

4

0 回答 0