小编Dac*_*ein的帖子

Spring Webflux Websocket安全性-基本身份验证

问题:我没有将带有Websockets的Spring Security应用于Webflux项目。

注意:我使用的是Kotlin而不是Java。

依赖:

  • Spring Boot 2.0.0

  • Spring Security 5.0.3

  • 春季WebFlux 5.0.4

重要更新:我已经提出了一个春季号的bug(3月30日)在这里和Spring安全维护者的一个表示,其不支持,但他们可以将其添加为春季安全5.1.0 M2

链接: 添加WebFlux WebSocket支持#5188

Webflux安全配置

@EnableWebFluxSecurity
class SecurityConfig
{
    @Bean
    fun configure(http: ServerHttpSecurity): SecurityWebFilterChain
    {

        return http.authorizeExchange()
            .pathMatchers("/").permitAll()
            .anyExchange().authenticated()
            .and().httpBasic()
            .and().formLogin().disable().csrf().disable()
            .build()
    }

    @Bean
    fun userDetailsService(): MapReactiveUserDetailsService
    {
        val user = User.withDefaultPasswordEncoder()
            .username("user")
            .password("pass")
            .roles("USER")
            .build()

        return MapReactiveUserDetailsService(user)
    }
}
Run Code Online (Sandbox Code Playgroud)

Webflux Websocket配置

@Configuration
class ReactiveWebSocketConfiguration
{
    @Bean
    fun webSocketMapping(handler: WebSocketHandler): HandlerMapping
    {
        val map = mapOf(Pair("/event", handler))
        val mapping = SimpleUrlHandlerMapping()
        mapping.order …
Run Code Online (Sandbox Code Playgroud)

spring-security websocket spring-webflux

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

Spring Webflux ErrorHandling - @RestControllerAdvice 与 @ExceptionHandler 或 DefaultErrorAttributes?

Spring Webflux中,异常处理的首选方式是什么?

@RestControllerAdvice 来自 Spring MVC,而 DefaultErrorAttributes 来自 Spring Webflux。

然而,在 Spring Webflux 中,有人可以使用 @RestControllerAdvice。有什么优点/缺点?

@RestControllerAdvice

@RestControllerAdvice
public class ControllerAdvice
{
    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Mono<Map<String, Object>> exceptions(Throwable e)
    {
        return Mono.just(Map.of("message", "bad"));
    }
}
Run Code Online (Sandbox Code Playgroud)

扩展 DefaultErrorAttributes

@Component
public class ErrorAttributes extends DefaultErrorAttributes
{
    @Override
    public Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace)
    {
        var ex = getError(request);

        var attributes = new LinkedHashMap<String, Object>();
        attributes.put("status", HttpStatus.BAD_REQUEST.value());
        attributes.put("message", "bad");

        return attributes;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想留在响应式世界中,所以我更倾向于 DefaultErrorAttributes(它与 Webflux 中的 DefaultErrorWebExceptionHandler 配合得很好)。但是,在 @RestControllerAdvice 中我也可以使用 …

error-handling spring exception spring-boot spring-webflux

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

Spring @Query with Lower和Wildcards

本课题涉及Spring Data JPA.

我需要一个带有Lower Function和Wildcards的(native)@Query.但是我不能让它发挥作用.

这是我正在寻找的过于简化的版本:

@Query(value = "SELECT * FROM CAR c WHERE LOWER(c.model) LIKE LOWER(%:model%)", nativeQuery = true)
List<Car> findByModelMatching( @Param("model") String model );
Run Code Online (Sandbox Code Playgroud)

更低(%:型号%) - >不工作!LOWER(:型号) - >有效!

我知道这样的查询可以很容易地重写为:

List<Car> findByModelContainingIgnoreCase( String model );
Run Code Online (Sandbox Code Playgroud)

但我不是在寻找一个命名查询.

我的问题是:如何在@Query中将LOWER(甚至UPPER)与WildCards结合起来!

干杯

java sql spring wildcard spring-data-jpa

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

浏览器的 CRUD URL 设计(非 REST)

在 Stack Overflow 上为 RESTful URL 设计提出了许多问题

仅举几例...

分层 URL 设计: 分层 RESTful URL 设计

了解 REST:动词、错误代码和身份验证:了解 REST:动词、错误代码和身份验证

所以我很了解 Restful URL Design。但是,对于非单页应用程序 (SPA) 的传统网站,浏览器的 URL 设计如何。

出于本示例的目的,让我们假设我们有一个图书数据库。让我们进一步假设我们创建了 2 个传统的 HTML 站点。

  1. 用于显示所有书籍的 HTML 表格
  2. 用于展示一本书的 HTML 表单(空白或预填书籍详细信息)

现在我们希望我们网站的用户可以使用它进行 CRUD 操作。那么下面的 URL 设计怎么样:

GET /book/show/all        // HTML Table
GET /book/show/{id}       // HTML Form pre-filled
GET /book/new             // HTML Form blank
POST /book/new            // Submit HTML Form
POST /book/update/{id}    // Submit updated HTML Form
POST /book/delete/{id}    // A Link/Button with POST …
Run Code Online (Sandbox Code Playgroud)

rest url web-services http url-design

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

Spring Webflux Reactor Netty:HTTP 请求/响应十六进制转储?

我们必须在 application.properties 中设置什么日志级别,以便在 reactor-netty 的控制台中查看带有标头和正文的完整 HTTP 请求和响应作为十六进制转储

logging.level.reactor.netty=trace
Run Code Online (Sandbox Code Playgroud)

仅显示响应 http 标头。

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Content-Length: 248
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1 ; mode=block
Referrer-Policy: no-referrer
Run Code Online (Sandbox Code Playgroud)

reactor-netty spring-webflux

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

休眠验证器:违规消息语言

我有一个测试类,我在其中测试用例如@NotNull注释的域模型

在我的测试课中,我首先要获得验证器

private static Validator validator;

@BeforeClass
public static void setup() {
    validator = Validation.buildDefaultValidatorFactory().getValidator();
}
Run Code Online (Sandbox Code Playgroud)

后来我有一个JUnit测试,我在其中测试领域模型(比如说一个人)

Set<ConstraintViolation<Person>> violations = validator.validate( aPerson );
Run Code Online (Sandbox Code Playgroud)

可以说,我想检索我做的第一条违规消息:

String violationMessage = violations.iterator().next().getMessage()
Run Code Online (Sandbox Code Playgroud)

我尚未在@NotNull批注上设置任何自定义违规消息。因此,休眠验证器将从hibernate-validator-jar中的资源包中提取默认消息。我的路径如下所示:

hibernate-validator-5.3.5.Final.jar
    - org
        - hibernate
            - validator
                ...
                ResourceBundle 'Validation Messages'
Run Code Online (Sandbox Code Playgroud)

在此资源包中,支持多种语言(英语,德语,法语,...)

@NotNull违规消息(德语)

javax.validation.constraints.NotNull.message     = darf nicht null sein
Run Code Online (Sandbox Code Playgroud)

英文@NotNull违规消息

javax.validation.constraints.NotNull.message     = may not be null
Run Code Online (Sandbox Code Playgroud)

题:

在测试时,如何强制Hibernate Validator从资源包中为违规消息选择特定的语言?现在,我收到英语违规消息。但是,在另一台机器上德语。

java junit hibernate hibernate-validator

2
推荐指数
2
解决办法
1265
查看次数

逻辑和javascript中的两个布尔数组?

在ES6中使用两个布尔数组的优雅功能解决方案是什么?

const a1 = [true, false, false]
const a2 = [true, true, false]
Run Code Online (Sandbox Code Playgroud)

应该导致:

[true, false, false]
Run Code Online (Sandbox Code Playgroud)

javascript arrays ecmascript-6

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

Spring Reactor - 等到单声道完成然后再做下一个单声道

假设我有一个 repository.save(..) 方法,它返回一个 Mono。

也可以说我有一个 repository.findByEmail(..) 它返回一个 Mono。

问题:

我希望第一个 Mono 在完成第二个 Mono 之后完成。

repository.save(..).then(repository.findByEmail(..))
Run Code Online (Sandbox Code Playgroud)

但是,这里的第二个 Mono 总是先执行?我的印象是.then(..) finishes and then plays another Mono

源代码说:
Let this {@link Mono} complete then play another Mono.

我的问题的解决方案是什么?

project-reactor spring-webflux

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

Thymeleaf:查询字符串

如何在Thymeleaf中创建URL:

/user/map?userId=1&mapId=2  
Run Code Online (Sandbox Code Playgroud)

在百里香模型中,我可以访问$ {user.id}和$ {map.id}。

我试过了:

th:href="@{'/user/map/update/?userId=' + ${user.id} + '&mapId=' + ${map.id}}"
Run Code Online (Sandbox Code Playgroud)

但它给出:

The reference to entity "mapId" must end with the ';' delimiter.
Run Code Online (Sandbox Code Playgroud)

query-string thymeleaf

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

Thymeleaf: 有条件的 th:value

问题:
${map}可以null

<input type="text" th:value="${map.name}" />
Run Code Online (Sandbox Code Playgroud)

我需要什么:
如果name不是nullth:value=name不然th:value=""

 <input type="text" th:value="${map.name != null ? map.name : ''}" />
Run Code Online (Sandbox Code Playgroud)

但我上面的代码无效

thymeleaf

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

Kotlin:数据类私有设置程序public getter

我有什么办法可以在Kotlin数据类中创建私有设置者和公共获得者?

data class Test(var attribute: String) {

    // attribute can be mutated inside this class

    // but outside only readable ?
}
Run Code Online (Sandbox Code Playgroud)

kotlin

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