fix: 密码机制

This commit is contained in:
wangyu 2021-01-13 00:50:36 +08:00
parent 8c3048316a
commit e55c7a29bd
1 changed files with 21 additions and 11 deletions

View File

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