为什么基本身份验证在restTemplate中总是出现错误403?

为什么基本身份验证在restTemplate中总是出现错误403?,rest,spring-boot,spring-security,activemq,resttemplate,Rest,Spring Boot,Spring Security,Activemq,Resttemplate,我在spring boot项目中尝试了多种方法通过RestTemplate调用get url以获取json,但每次都出现以下403错误: <200,{"request":{"mbean":"org.apache.activemq.artemis:address=%22my.queue.demo%22,broker=%22141.110.112.13%22,component=addresses,queue=%22my.queue.demo%22,routing-type=%22anyc

我在spring boot项目中尝试了多种方法通过RestTemplate调用get url以获取json,但每次都出现以下403错误:

  <200,{"request":{"mbean":"org.apache.activemq.artemis:address=%22my.queue.demo%22,broker=%22141.110.112.13%22,component=addresses,queue=%22my.queue.demo%22,routing-type=%22anycast%22,subcomponent=queues","attribute":"MessageCount","type":"read"},"error_type":"java.lang.Exception","error":"java.lang.Exception
 : Reading attribute MessageCount is forbidden for MBean
 org.apache.activemq.artemis:address=%22my.queue.demo%22,broker=%22141.110.112.13%22,component=addresses,queue=%22my.queue.demo%22,routing-type=%22anycast%22,subcomponent=queues","status":403},[Date:"Wed,
 12 Jun 2019 12:56:22 GMT", Cache-Control:"no-cache",
 Pragma:"no-cache", Access-Control-Allow-Origin:"*",
 X-Frame-Options:"SAMEORIGIN", X-XSS-Protection:"1",
 Content-Type:"text/plain;charset=utf-8", Expires:"Wed, 12 Jun 2019
 11:56:22 GMT", Transfer-Encoding:"chunked"]>
当我在基本身份验证(user=test,pass=test)中使用Postman时,它在find中起作用,但在restemplate中不起作用

这是我的配置类:

@SpringBootApplication
public class StartWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(StartWebApplication.class, args);
    }
}

@Configuration
class Appconfig{
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
我的朋友:

...
@Autowired
private RestTemplate restTemplate;

....
restTemplate.exchange(url, HttpMethod.GET, createHeaders("test", "test"), String.class)
....
HttpEntity createHeaders(String username, String password) {
        byte[] token = Base64.getEncoder().encode(
                (username + ":" + password).getBytes());
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + new String(token));
        HttpEntity<String> request = new HttpEntity<>(headers);
        return request;
    }
}
。。。
@自动连线
私有RestTemplate RestTemplate;
....
restemplate.exchange(url,HttpMethod.GET,createHeaders(“测试”,“测试”),String.class)
....
HttpEntity createHeaders(字符串用户名、字符串密码){
字节[]标记=Base64.getEncoder().encode(
(用户名+”:“+密码).getBytes());
HttpHeaders=新的HttpHeaders();
添加(“授权”、“基本”+新字符串(令牌));
HttpEntity请求=新的HttpEntity(标头);
返回请求;
}
}
您可以试试:

String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
headers.add("Authorization", "Basic " + new String(encodedAuth));
你能试试吗

String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
headers.add("Authorization", "Basic " + new String(encodedAuth));

使用
restemplate
,您可以通过以下方法避免一些
Base64
样板文件:

@Bean
RestTemplate rest() {
    RestTemplate rest = new RestTemplate();
    rest.getInterceptors().add(new BasicAuthenticationInterceptor("test", "test"));
    return rest;
}
然后你可以简单地做:

rest.getForObject(url, String.class);

不确定问题是否出在用户名和密码的基础上,但这样做会消除这种可能性。

使用
restemplate
,您可以通过以下方式避免一些
Base64
样板文件:

@Bean
RestTemplate rest() {
    RestTemplate rest = new RestTemplate();
    rest.getInterceptors().add(new BasicAuthenticationInterceptor("test", "test"));
    return rest;
}
然后你可以简单地做:

rest.getForObject(url, String.class);

不确定问题是否出在用户名和密码的基础上,但这样做将消除这种可能性。

我也尝试过这种解决方案,但我收到了相同的消息错误!我也尝试过这个解决方案,但我得到了相同的消息错误!您使用的是什么版本的Spring?我问这个问题是因为5.1引入了一个setter方法,HttpHeaders#setBasicAuth,您可能会发现它很有用。我也做了三次尝试,但我得到了相同的错误,您使用的是哪个版本的Spring?我这样问是因为5.1引入了一个setter方法,HttpHeaders#setBasicAuth,您可能会发现它很有用。我也对它进行了treid,但我得到了相同的错误BasicAuthorizationInterceptor类型不推荐使用,但我也对它进行了treid,但是我得到了相同的403错误!哎呀,我是说基本的验钞机。我已经更新了答案。那么,可能还有别的事情发生了。你能不能确认这些信用确实被添加到了请求中?你能将邮递员发送的授权头与应用程序发送的授权头进行比较吗?BasicAuthorizationInterceptor类型已被弃用,但我也对其进行了treid,但我得到了相同的403错误!哎呀,我是说基本的验钞机。我已经更新了答案。那么,可能还有别的事情发生了。你能不能确认这些信用确实被添加到了请求中?您能否将邮递员发送的授权标头与应用程序发送的授权标头进行比较?