如何在JAXRSClientFactoryBean或JAXRSClientFactory中添加标题

时间:2018-10-18 06:22:22

标签: spring security jax-rs jhipster basic-authentication

我试图通过以下方式使用JAXRSClientFactoryBean或JAXRSClientFactory:

  

JAXRSClientFactoryBean示例

Map<String, String> headerMap = new HashMap<String, String>();
headerMap.put("X-XSRF-TOKEN", "dummy_token");
headerMap.put("Cookie", "XSRF-TOKEN=dummy_token");
headerMap.put("Content-Type", "application/x-www-form-urlencoded");
headerMap.put("Accept", "application/json, text/plain, */*");

headerMap.put("Connection", "keep-alive");
headerMap.put("Host", "localhost.fr:8080");
headerMap.put("Referer", "http://localhost:8080/");

JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setHeaders(headerMap);
bean.setResourceClass(SampleService.class);
bean.setAddress("http://localhost:8080/");
bean.setUsername("test");
bean.setPassword("test");
bean.setInheritHeaders(true);
SampleService test1 = bean.create(SampleService.class);
System.out.println(test1.getUser(1l));
  

JAXRSClientFactory示例

SampleService sampleService = JAXRSClientFactory.create("http://localhost:8080/", SampleService.class, "test", "test", null);

System.out.println(sampleService.getUser(1l));
  

SampleService

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.http.ResponseEntity;

    @Path("/api")
    public interface SampleService {
      @GET
      @Path("/users/{id}")
      @Produces(MediaType.APPLICATION_JSON)
      @Consumes(MediaType.APPLICATION_JSON)
      public ResponseEntity<XUser> getUser(@PathParam("id") Long id);    
    }

在两种情况下,我都遇到以下异常

Exception in thread "main" javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.apache.cxf.jaxrs.client.AbstractClient.convertToWebApplicationException(AbstractClient.java:505)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.checkResponse(ClientProxyImpl.java:319)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.handleResponse(ClientProxyImpl.java:827)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:789)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:230)
at com.sun.proxy.$Proxy13.getXUser(Unknown Source)
at com.exadatum.xcatalyst.xstudio.publisher.AuthenticatorJAXRS.main(AuthenticatorJAXRS.java:55)

因为我在春季使用了SecurityConfiguration类进行认证,例如loginProcessingUrl("/api/authentication")

因此遇到此问题的任何人都请帮忙。 欢迎任何指点/建议

1 个答案:

答案 0 :(得分:0)

也许我想很明显,但是看起来用户确实未被授权:)。

您可以确保您的SecurityConfiguration类可以访问您的用户信息(用户名:test /密码:test)吗?出于测试目的,您可以使用inMemoryAuthentication()设置全局安全性定义。