不使用使用 Entity Callbacks
实现密码在保存时密码加密功能
@Autowired
PasswordEncoder passwordEncoder;
public Mono<ServerResponse> save(ServerRequest request){
return request.bodyToMono(Doctor.class)
.map(d -> {
d.setIsVerified(false);
d.setPassword(passwordEncoder.encode(d.getPassword()));
return d;
})
.flatMap(doctor -> doctorService.saveDoctor(doctor))
.flatMap(d -> ServerResponse.created(URI.create("/doctors/" + d.getId())).bodyValue(d));
}
使用 Entity Callbacks
功能实现密码加密
- 定义一个回调类实现
BeforeSaveCallback
import com.fengyun.medical.domain.Doctor;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.mapping.event.BeforeSaveCallback;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.r2dbc.core.Parameter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@Component
public class MedicalCallBack implements BeforeSaveCallback<Doctor> {
@Autowired
PasswordEncoder passwordEncoder;
@Override
public Publisher<Doctor> onBeforeSave(Doctor entity, OutboundRow row, SqlIdentifier table) {
row.put("is_verified", Parameter.from(false));
row.put("password",Parameter.from(passwordEncoder.encode(entity.getPassword())));
return Mono.just(entity);
}
}
- handler处理流程
public Mono<ServerResponse> save(ServerRequest request){
return request.bodyToMono(Doctor.class)
.flatMap(doctor -> doctorService.saveDoctor(doctor))
.flatMap(d -> ServerResponse.created(URI.create("/doctors/" + d.getId())).bodyValue(d));
}
来自为知笔记(Wiz)
标签:r2dbc,spring,springframework,Entity,passwordEncoder,org,import,data From: https://www.cnblogs.com/baiyifengyun/p/17059038.html