问题:我没有将带有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 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 中我也可以使用 …
本课题涉及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结合起来!
干杯
在 Stack Overflow 上为 RESTful URL 设计提出了许多问题
仅举几例...
分层 URL 设计: 分层 RESTful URL 设计
了解 REST:动词、错误代码和身份验证:了解 REST:动词、错误代码和身份验证
所以我很了解 Restful URL Design。但是,对于非单页应用程序 (SPA) 的传统网站,浏览器的 URL 设计如何。
出于本示例的目的,让我们假设我们有一个图书数据库。让我们进一步假设我们创建了 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) 我们必须在 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) 我有一个测试类,我在其中测试用例如@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从资源包中为违规消息选择特定的语言?现在,我收到英语违规消息。但是,在另一台机器上德语。
在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) 假设我有一个 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.
我的问题的解决方案是什么?
如何在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) 问题:
${map}可以null。
<input type="text" th:value="${map.name}" />
Run Code Online (Sandbox Code Playgroud)
我需要什么:
如果name不是null则th:value=name不然th:value=""
<input type="text" th:value="${map.name != null ? map.name : ''}" />
Run Code Online (Sandbox Code Playgroud)
但我上面的代码无效
我有什么办法可以在Kotlin数据类中创建私有设置者和公共获得者?
data class Test(var attribute: String) {
// attribute can be mutated inside this class
// but outside only readable ?
}
Run Code Online (Sandbox Code Playgroud) java ×2
spring ×2
thymeleaf ×2
arrays ×1
ecmascript-6 ×1
exception ×1
hibernate ×1
http ×1
javascript ×1
junit ×1
kotlin ×1
query-string ×1
rest ×1
spring-boot ×1
sql ×1
url ×1
url-design ×1
web-services ×1
websocket ×1
wildcard ×1