webflux 如何将当前登录单声道字符串传递给 flux



我仍然是Spring boot Webflux的新手。我正在使用Casssandra作为我的数据库,下面我有一个朋友表,我想根据我的登录ID获取我所有的朋友。如何获取当前登录 ID 并将 ID 传递给助焊剂

// Here i retrieved currently login user ID which is a mono string
public Mono<String> getUserID(Mono<String> principal) {
return principal
.map(Principal::getName)
}

public static Mono<String> getUserIDFromRequest(ServerRequest request) {
return request.principal()
.cast(UsernamePasswordAuthenticationToken.class)
.map(UsernamePasswordAuthenticationToken::getName);
}
// My friends table
public class Friends {
@PrimaryKey("userid")
private long userId;
private long friendId;
private FriendObject friendObject;
private String since;
private boolean enableNotifications;
private boolean allowMessages;
// Getters and setters
}
@Repository
public interface FriendsRepository extends ReactiveCassandraRepository<Friends, Long> {
}
@Service
public class FriendshipServiceImpl implements FriendshipService {
@Override
public Mono<Friends> addFriend(Friends friends) {
return friendsRepository.save(friends);
}
}
public class FriendshipHandler {
// How to pass the Login Mono<String> to this flux Or how can i combine Mono and Flux?
@GetMapping(path="/friends/list", produces=MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Friends> getFriends(Mono<Principal> principal) {
return friendshipService.getFriends(Long.valueOf("The login ID"));
}
OR
public Mono<ServerResponse> getFriends(ServerRequest request) {
return ok().body(
friendshipService
.getFriends(Long.valueOf("The login ID")), Friends.class);
}
}

这是基本的webflux,所以如果你不能这样做,你需要阅读

@GetMapping(path="/friends", produces=MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Friends> getFriends(Mono<Principal> principal) {
return principal.map(principal -> {
return // Here you do what you need to with the principal
});
}

请阅读 ReactiveSecurityContextHolder

相关内容

  • 没有找到相关文章

最新更新