fix: 密码机制

This commit is contained in:
wangyu 2021-01-13 00:50:36 +08:00
parent 8c3048316a
commit e55c7a29bd

View File

@ -1,7 +1,6 @@
package com.flyfish.framework.handler;
import com.flyfish.framework.bean.Result;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.service.MongoUserDetailsService;
import com.flyfish.framework.transform.DataBufferTransformer;
import org.springframework.http.MediaType;
@ -44,19 +43,30 @@ public class JsonAuthenticationFailureHandler implements ServerAuthenticationFai
*/
@Override
public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) {
ServerHttpResponse response = webFilterExchange.getExchange().getResponse();
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return Mono.justOrEmpty(Optional.ofNullable(webFilterExchange.getExchange().getApplicationContext()))
.flatMap(applicationContext -> {
MongoUserDetailsService userDetailsService = applicationContext.getBean(MongoUserDetailsService.class);
Mono<User> mono = exception instanceof BadCredentialsException ? userDetailsService.error(webFilterExchange.getExchange())
: Mono.justOrEmpty(Optional.empty());
return mono.flatMap(user -> {
ServerHttpResponse response = webFilterExchange.getExchange().getResponse();
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return response.writeWith(Mono.fromCallable(() ->
dataBufferTransformer.transform(
Result.error(descriptionMap.getOrDefault(exception.getClass(), exception.getMessage()))
)));
});
if (exception instanceof BadCredentialsException) {
return userDetailsService.error(webFilterExchange.getExchange())
.flatMap(user -> write(response, exception));
}
return write(response, exception);
});
}
/**
* 写入
*
* @param response 响应
* @param exception 异常
* @return 结果
*/
private Mono<Void> write(ServerHttpResponse response, AuthenticationException exception) {
return response.writeWith(Mono.fromCallable(() ->
dataBufferTransformer.transform(
Result.error(descriptionMap.getOrDefault(exception.getClass(), exception.getMessage()))
)));
}
}