如何在SpringBootTest中向Autowired testRestTemplate添加基本身份验证;春季启动 1.4

2024-05-09

我在 Spring Boot 1.4 之前的 OAuth 集成测试如下(更新只是为了不使用已弃用的功能):

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ApplicationConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
public class OAuth2IntegrationTest {

    @Value("${local.server.port}")
    private int port;

    private static final String CLIENT_NAME = "client";
    private static final String CLIENT_PASSWORD = "123456";

    @Test
    public void testOAuthAccessTokenIsReturned() {
        MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
        request.set("username", "user");
        request.set("password", password);
        request.set("grant_type", "password");
        @SuppressWarnings("unchecked")
        Map<String, Object> token = new TestRestTemplate(CLIENT_NAME, CLIENT_PASSWORD)
            .postForObject("http://localhost:" + port + "/oauth/token", request, Map.class);
        assertNotNull("Wrong response: " + token, token.get("access_token"));
    }
}

我现在想使用 Autowired TestRestTemplate,如此处所述http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-working-with-random-ports http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-working-with-random-ports

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
    ApplicationConfiguration.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OAuth2IntegrationTest {

    private static final String CLIENT_NAME = "client";
    private static final String CLIENT_PASSWORD = "123456";

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Test
    public void testOAuthAccessTokenIsReturned() {
        MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
        request.set("username", "user");
        request.set("password", password);
        request.set("grant_type", "password");
        @SuppressWarnings("unchecked")

        Map<String, Object> token1 = this.testRestTemplate. //how to add basic auth here
        assertNotNull("Wrong response: " + token, token.get("access_token"));
    }
}

我认为这是添加身份验证的最接近的方法:

Spring 4.0.0 使用 RestTemplate 进行基本身份验证 https://stackoverflow.com/questions/21998405/spring-4-0-0-basic-authentication-with-resttemplate

我想使用 Autowired testRestTemplate 来避免在测试中解析主机和端口。有没有办法做到这一点?


这个问题在 Spring Boot 1.4.1 中得到了修复,它有一个额外的方法

testRestTemplate.withBasicAuth(用户名,密码)

@Autowired
private TestRestTemplate testRestTemplate;

@Test
public void testOAuthAccessTokenIsReturned() {
    MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
    request.set("username", USERNAME);
    request.set("password", password);
    request.set("grant_type", "password");
    @SuppressWarnings("unchecked")
    Map<String, Object> token = this.testRestTemplate.withBasicAuth(CLIENT_NAME, CLIENT_PASSWORD)
            .postForObject(SyntheticsConstants.OAUTH_ENDPOINT, request, Map.class);
    assertNotNull("Wrong response: " + token, token.get("access_token"));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在SpringBootTest中向Autowired testRestTemplate添加基本身份验证;春季启动 1.4 的相关文章

随机推荐

  • 在 Laravel 视图中使用 CSS?

    我刚刚开始学习 Laravel 并且可以掌握控制器和路由的基础知识 我的操作系统是 Mac OS X Lion 它位于 MAMP 服务器上 我的routes php代码 Route get function return View make
  • ErrorT 已弃用,但 exceptT 不适合

    我有一个一元计算 在某些时候 由于单子模式匹配 它开始需要 MonadFail 约束 我的简单解决方法是使用以下命令运行它 fmap either error id runErrorT 然而哎呀 Deprecated Use Control
  • 如何在 Angularjs 中正确将 JSON 响应解析为 ng-repeat

    我希望能够使用ng repeat为了解析我在前端的响应 我在解析具有多个项目与单个项目的响应时遇到问题ng repeat list 我能够解析 但我必须创建 2 个不同的列表ng repeat在前端进行配置并添加一些丑陋的逻辑 以便在数组长
  • 查找“与我共享”文件夹 ID(驱动器 ID)和文件 - OneDrive、Microsoft Graph、Python

    我的客户与我共享了一些onedrive文件夹 其中包含5个文件 我想找到drive Id File Id 以便我可以使用python脚本下载 脚本参考 无法从内部文件夹 OneDrive Microsoft Graph Python 下载文
  • 条件顺序仅在 init AngularJS 上执行

    如何在视图初始化时仅运行 orderBy 过滤器一次 我不希望我的列表在运行时被重新排序 li li 使用 orderBy 作为控制器中的过滤器 app controller DemoCtrl scope filter function s
  • 在多面图中用 N 注释 x 轴

    我正在尝试生成一些按治疗条件和访问次数细分的数字结果的箱线图 每个框中的观察次数都放在图下方 并且也标记了访问次数 这里有一些虚假数据可以用来说明 我举了两个我尝试过但不太有效的例子 library ggplot2 library plyr
  • 我的 apk 文件在模拟器中的位置

    我在 eclipse android 中编写了一个小程序 现在我安装并运行我的程序 它是一个 apk 现在我想知道我的 apk 文件在哪里 我什至想将它 拉 到我的系统中 是否可以 如果是这样请帮助我 如果您只想将 apk 安装在手机或类似
  • 从有符号字符转换为无符号字符然后再转换回来?

    我正在使用 JNI 并有一个 jbyte 类型的数组 其中 jbyte 表示为有符号字符 即范围从 128 到 127 jbyte 表示图像像素 对于图像处理 我们通常希望像素分量的范围为0到255 因此 我想将jbyte值转换为0到255
  • Jinja2中获取请求参数

    如何检索请求参数a在 Jinja2 模板中 http foo bar a 1 我这个答案有点晚了 但其他解决方案并没有真正考虑到您对 Flask 的使用 事实上 您将 Flask 与 Jinja2 一起使用 这使得您的情况与其他框架有点不同
  • 如果所有类不在同一个包中,Spring @autowired 不起作用

    我有四个包裹 com spring org Files HomeController java com spring org dao Files SubscriberDao java SubscriberDaoImpl java com s
  • RecyclerView 适配器的 Kotlin 泛型

    我正在尝试编写一个通用的 recyclerview 适配器 我找到了几个例子 然而 仍然无法弄清楚如何实现通用适配器 我写的代码是 open abstract class BaseAdapter
  • 如何设置 CMake 与 clang 交叉编译 Windows 上的 ARM 嵌入式系统?

    我正在尝试生成 Ninja makefile 以使用 Clang 为 ARM Cortex A5 CPU 交叉编译 C 项目 我为 CMake 创建了一个工具链文件 但似乎存在错误或缺少一些我无法找到的东西 当使用下面的工具链文件调用 CM
  • UITableView 自动调整行大小约束在 iPhone 6Plus 上神秘破坏

    我有一个自定义的 UITableViewCell 它有一个缩略图和一堆文本 行高配置为使用自动计算 tableView estimatedRowHeight 129 tableView rowHeight UITableViewAutoma
  • 为 ggplot 定义新的尺度轴变换

    我正在尝试创建一个squared使用 y 轴变换scales trans new但遇到错误 MWE data data frame x 1 10 y runif 10 z rnorm 10 10 library ggplot2 ggplot
  • 获取 Google Cloud 服务帐户的开发者密钥

    我已经启用了XML API https cloud google com storage docs interoperability并分配了一些开发者密钥 https cloud google com storage docs migrat
  • 为什么某些 Web.config 转换标记化为 SetParameters.xml,而其他则不然?

    我最近在 VS2010 中使用配置转换相当多 但我很困惑为什么有些转换直接应用于包中的 Web config 而其他转换则针对 SetParameters xml 中的令牌存储 然后在发布时应用 例如 采用具有以下连接字符串和应用程序设置的
  • JQuery 表单提交不发送帖子

    我在使用 JQuery 提交表单时遇到问题 当按下提交按钮时 表单会正确提交 但是当我尝试使用时 somebutton click function form myForm submit 我还尝试从 Chrome 控制台调用表单提交 表单未
  • 如何让你的精灵在pygame中跳跃

    目前我已经制作了一个平台游戏 可以左右移动我的角色 他从地上开始 关于如何让他跳的任何想法 因为我不明白 目前 如果我按住向上键 我的玩家精灵将连续向上移动 或者如果我按下它 我的玩家精灵将向上移动并保持向上 我想找个办法远离他 让我重新跌
  • iOS:启动图像多语言

    我有一个多语言应用程序 我的问题是启动图像 根据设备的语言使用启动图像的方法是什么 有什么东西在info plist file 解决方案是像项目中的任何其他资源一样本地化 Default png 从 Xcode 的项目列表中选择 Defau
  • 如何在SpringBootTest中向Autowired testRestTemplate添加基本身份验证;春季启动 1.4

    我在 Spring Boot 1 4 之前的 OAuth 集成测试如下 更新只是为了不使用已弃用的功能 RunWith SpringRunner class SpringBootTest classes ApplicationConfigu