小编jef*_*dio的帖子

对禁用资源的操作的预期 HTTP 状态代码

给定以下操作的预期/正确 HTTP 状态代码是什么:

  • 请求语法正确(消除400
  • 用户已通过身份验证(消除401
  • 用户被授权执行操作(消除403
  • 位置/资源存在(消除404
  • 方法被实现(消除501
  • 没有服务器错误(消除5xx

资源当前被禁用,从而阻止操作以预期结果完成。用户可以更改资源的状态并重试相同的请求。有关资源为何无法执行所要求的操作的信息将包含在响应正文中。

我的想法是这409 Conflict将是最好的响应,因为用户可能会更改资源的状态并重新提交请求,但也许有更好的方法表明“您通常允许使用此方法,但资源当前处于状态这会阻止它按预期完成。”

rest api-design http

11
推荐指数
1
解决办法
7090
查看次数

应用程序中可重用的AngularJS服务

我对AngularJS很新,所以我可能会问完全错误的问题.我想要完成的是在单个应用程序中创建一个可重用的数据绑定类.下面用一个非常简单的例子演示了我想要完成的事情.

假设我想创建一个带有值和增量方法的计数器.我可以创建一个服务并在控制器中使用它,如下所示:

angular.module("fiddle", ["counter"])
.controller("MainCtrl", function($scope, counter) {
    $scope.counter = counter;
});

angular.module("counter", [])
.service("counter", function() {
    this.count = 0;
    this.increment = function(x) {
        this.count += x;
    };
});
Run Code Online (Sandbox Code Playgroud)

然后我可以显示一个视图来添加一个计数器:

<h1>Count: {{counter.count}}</h1>
<button ng-click="counter.increment(1)">Add 1</button>
<button ng-click="counter.increment(5)">Add 5</button>
<button ng-click="counter.increment(10)">Add 10</button>
Run Code Online (Sandbox Code Playgroud)

这适用于一个计数器,但如果我想在同一个控制器中有多个计数器怎么办?由于服务是单身,我不能这样做.使用Angular创建类似的东西的最佳方法是什么,因为其他服务会更复杂?谢谢!

http://jsfiddle.net/jeffaudio/ExASb/

angularjs angularjs-service

6
推荐指数
1
解决办法
1743
查看次数

如何在 CompletableFuture.get() 调用上抛出异常?

有没有办法让 mockito 在CompletableFuture.get()调用时抛出异常而不仅仅是异步方法?

例如,给定以下(不正确的)测试用例:

@Test
public void whenRunnerThrows_thenReturn5xx() throws Exception {
    when(service.runAsync(any(),any())).thenThrow(new Exception(""));

    mvc.perform(post("/test")
            .contentType(MediaType.APPLICATION_JSON)
            .content("{\"name\":\"test\"}"))
            .andExpect(status().is5xxServerError());
}
Run Code Online (Sandbox Code Playgroud)

service.runAsync()在测试期间调用when ,抛出异常,这是有道理的。但是当(Spring Boot)应用程序运行时,同样的异常只会作为ExecutionException返回的原因抛出CompletableFuture::get.

编写这样的测试以便在运行应用程序时在单元测试中同时抛出异常的正确方法是什么?

java mockito spring-boot completable-future

5
推荐指数
1
解决办法
2061
查看次数

Spring security oauth2 在 30 秒后未能通过颁发者验证

在响应式 spring webflux 服务中,我将端点配置为受 OAuth2 资源服务器保护。当我第一次启动服务器时,它会正确验证 Bearer 令牌,但大约 30 秒后,完全相同的请求开始失败并出现以下错误:

error="invalid_token"
error_description="This iss claim is not equal to the configured issuer"
error_uri="https://tools.ietf.org/html/rfc6750#section-3.1"
Run Code Online (Sandbox Code Playgroud)

我已经验证令牌是有效的,并且iss声明似乎与spring.security.oauth2.resourceserver.jwt.issuer-uri. 如果这没有正确配置,那么我将不会收到有效的请求。

经过仔细检查,我发现错误源于声明和预期 URL的URL比较,因为前 30 秒匹配,但随后不匹配。这是使用 Azure AD 提供程序端点,我已经验证 URL字符串匹配,而不是内部地址。什么可能导致这种情况,我如何在最初的 30 秒后使用有效的发行人验证令牌?谢谢。issInetAddress.getAddress()https://sts.windows.net/{{tenantId}}/

作为参考,这是我的SecurityWebFilterChain

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    return http
            .csrf().disable()
            .authorizeExchange().anyExchange().authenticated()
            .and().oauth2ResourceServer().jwt().and()
            .and().build();
}
Run Code Online (Sandbox Code Playgroud)

Gradle 实现包括:

org.springframework.boot:spring-boot-starter-security:2.1.0.RC1
org.springframework.boot:spring-boot-starter-webflux:2.1.0.RC1
org.springframework.security:spring-security-oauth2-resource-server:5.1.1.RELEASE
org.springframework.security:spring-security-oauth2-jose:5.1.1.RELEASE
Run Code Online (Sandbox Code Playgroud)

java spring-security oauth-2.0 azure-active-directory spring-webflux

4
推荐指数
1
解决办法
1847
查看次数

通过浏览器表单进行身份验证时从Azure API App获取用户信息

x-zumo-auth使用Postman 在Chrome和标头中进行身份验证时,我的API应用程序使用控制器中的以下代码成功识别用户的upn:

Runtime runtime = Runtime.FromAppSettings(Request);
EmaUserInfo user = runtime.CurrentUser;
TokenResult token = await user.GetRawTokenAsync("aad");
string upn = token.Claims[ClaimTypes.Upn];
Run Code Online (Sandbox Code Playgroud)

但是,当使用Microsoft的AzureCards控制台客户端示例中的代码(将浏览器添加到窗体窗口并捕获zumo令牌)时,我无法获取用户的aad令牌并从API应用程序中收到以下错误:

Request to https://[gateway].azurewebsites.net/api/tokens?tokenName=aad&api-version=2015-01-14 GET failed BadRequest 400 (Bad Request)
{
  "status": 400,
  "source": "https://[gateway].azurewebsites.net/api/tokens?tokenName=aad&api-version=2015-01-14",
  "message": "Microservice '[API App]' only has permissions for token ''. Can't get token 'aad'"
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,这甚至发生在我设置断点并将zumo令牌从我的测试程序复制到邮递员时.程序失败,邮递员成功返回.据我所知,他们发送完全相同的请求.我能错过什么?

编辑:我已经做了一些测试,发现Postman方法在隐身模式下完成时会产生相同的错误.这让我相信它不仅仅是x-zumo-auth需要设置的标题,而且还需要设置一些cookie以便用户正常工作.如果我生成令牌,从apiapp url中删除cookie并仅使用令牌POST请求,我也会收到错误.

c# authentication azure azure-api-apps

1
推荐指数
1
解决办法
992
查看次数