小编Jav*_*Net的帖子

SpringCloudGateway - 记录传入请求 url 和相应的路由 URI

我是 Spring Cloud Gateway 的新手,我想要的是将传入请求记录到相应的路由 url,例如,如果我有以下路由配置:

      - id: route1
        uri: http://localhost:8585/
        predicates:
        - Path=/foo/**
        filters:
        - RewritePath=/foo/(?<segment>.*), /$\{segment}
Run Code Online (Sandbox Code Playgroud)

然后对于“ http://localhost:8080/foo/route1 ”的传入请求,应打印以下日志消息。

“传入请求 url ' http://localhost:8080/foo/route1 ' 被路由到 ' http://localhost:8585/route1 '”

有什么简单的方法可以实现这一点,或者我可以通过设置日志级别来实现这一点。

你能帮忙吗

spring spring-boot spring-cloud spring-cloud-gateway

8
推荐指数
2
解决办法
1万
查看次数

禁止 Spring Cloud Gateway 中未经身份验证的请求

我在 spring 云网关中实现了自定义预过滤器,它允许经过身份验证的请求通过下游过程。我想要的是,如果请求未经身份验证,则返回 401 UNAUTHORIZE 状态的响应并停止下游处理。能不能实现这个spring cloud gateway。

请帮忙。

我的过滤器代码如下

public class ValidUserFilter implements GatewayFilterFactory {

  @Override
  public GatewayFilter apply(Object config) {
    return (exchange, chain) -> {
      ServerHttpRequest request = exchange.getRequest();

      if (isValidRequest(request)) {
        // Allow processing
      } else {
      // set UNAUTHORIZED 401 response and stop the processing
      }

      return chain.filter(exchange);
    };
  }
}
Run Code Online (Sandbox Code Playgroud)

和配置如下:

  - id: myroute
            uri: http://localhost:8080/bar
            predicates:
            - Path=/foo/**
            filters:
            - ValidUserFilter
Run Code Online (Sandbox Code Playgroud)

spring spring-boot spring-cloud spring-cloud-gateway

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

Angular5 - 设置从 spring 表单登录返回的 cookie

我正在尝试向使用 angular5 和 spring boot 1.5.8 构建的应用程序添加 spring 安全性。我尝试添加的身份验证机制是 spring boot 的 formLogin。

我通过休息调用而不是通过默认表单操作对用户进行身份验证。登录工作正常,但随后的休息调用失败。我正在使用 cookie 'ON' 进行此休息调用,以便它将 cookie 发送到 spring 应用程序,但请求仍然失败。原因是 angular 没有设置从身份验证返回的响应 cookie。

一旦rest成功,在auth.service.ts的登录方法中设置cookie。

如何对从 Spring Security 返回的 cookie 进行角度设置,请帮助....

这是代码:

登录.component.html

<form name="form-signin" (ngSubmit)="login()" #f="ngForm" novalidate>
  <div class="form-group" >
    <label for="username">Username</label>
    <input type="text" class="form-control" id="username" name="username" [(ngModel)]="user.username" />
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" id="password" name="password" [(ngModel)]="user.password" />        
  </div>
  </div>
  <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</button>
</form>
Run Code Online (Sandbox Code Playgroud)

登录.component.ts

export class LoginComponent implements OnInit { …
Run Code Online (Sandbox Code Playgroud)

spring spring-security spring-boot angular

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

将百分比值舍入到最接近的数字

我想将字符串格式的数字转换为带一个小数点的百分比值。以下是我的示例输入和预期输出。

预期成绩:

"0.0195" => "2.0%"

"0.0401" => "4.0%"

我知道这可能是一个简单的问题,但我无法使用 java API 找到确切的解决方案,我尝试了 RoundingMode 枚举下存在的所有舍入模式,但没有舍入模式给出我的预期结果。你能帮忙吗,我可能遗漏了一些东西。


import java.math.RoundingMode;
import java.text.NumberFormat;

public class RoundingModeExample {
    public static void main(String[] args) {
        System.out.println(formatPercentage("0.0195") + "(expected 2.0%)");
        System.out.println(formatPercentage("0.0401") + "(expected 4.0%)");
    }

    private static String formatPercentage(String number) {
        String formattedValue = "";
        NumberFormat numberFormat = NumberFormat.getPercentInstance();
        numberFormat.setMinimumFractionDigits(1);
        numberFormat.setRoundingMode(RoundingMode.HALF_UP);
        try {
            formattedValue = numberFormat.format(Double.valueOf(number));
        } catch (NumberFormatException e) {
            formattedValue = number;
        }
        return formattedValue;
    }


}
Run Code Online (Sandbox Code Playgroud)

上述程序的输出:

1.9%(预期2.0%)

4.0%(预期4.0%)

java

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