Cześć, jest tutaj ktoś kto używał openvidu w połączeniu z Javą jako klientem decydującym o sesji i rozdawaniu tokenów i wie jak można to sensownie zrobić, tak by rozdzielał sesję w zależności od zalogowania (w wersji demo są tylko zahardkowowani 3 użytkownicy)
public LoginController() {
users.put("publisher1", new MyUser("publisher1", "pass", OpenViduRole.PUBLISHER));
users.put("publisher2", new MyUser("publisher2", "pass", OpenViduRole.PUBLISHER));
users.put("subscriber", new MyUser("subscriber", "pass", OpenViduRole.SUBSCRIBER));
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<Object> login(@RequestBody String userPass, HttpSession httpSession) throws ParseException {
System.out.println("Logging in | {user, pass}=" + userPass);
// Retrieve params from POST body
JSONObject userPassJson = (JSONObject) new JSONParser().parse(userPass);
String user = (String) userPassJson.get("user");
String pass = (String) userPassJson.get("pass");
if (login(user, pass)) { // Correct user-pass
// Validate session and return OK
// Value stored in HttpSession allows us to identify the user in future requests
httpSession.setAttribute("loggedUser", user);
return new ResponseEntity<>(HttpStatus.OK);
} else { // Wrong user-pass
// Invalidate session and return error
httpSession.invalidate();
return new ResponseEntity<>("User/Pass incorrect", HttpStatus.UNAUTHORIZED);
}
}
//WYDAWANIE TOKENU
@RequestMapping(value = "/get-token", method = RequestMethod.POST)
public ResponseEntity<JSONObject> getToken(@RequestBody String sessionNameParam, HttpSession httpSession)
throws ParseException {
try {
checkUserLogged(httpSession);
} catch (Exception e) {
return getErrorResponse(e);
}
System.out.println("Getting a token from OpenVidu Server | {sessionName}=" + sessionNameParam);
JSONObject sessionJSON = (JSONObject) new JSONParser().parse(sessionNameParam);
// The video-call to connect
String sessionName = (String) sessionJSON.get("sessionName");
// Role associated to this user
OpenViduRole role = LoginController.users.get(httpSession.getAttribute("loggedUser")).role;
// Optional data to be passed to other users when this user connects to the
// video-call. In this case, a JSON with the value we stored in the HttpSession
// object on login
String serverData = "{\"serverData\": \"" + httpSession.getAttribute("loggedUser") + "\"}";
// Build tokenOptions object with the serverData and the role
TokenOptions tokenOptions = new TokenOptions.Builder().data(serverData).role(role).build();
JSONObject responseJson = new JSONObject();
if (this.mapSessions.get(sessionName) != null) {
// Session already exists
System.out.println("Existing session " + sessionName);
try {
// Generate a new token with the recently created tokenOptions
String token = this.mapSessions.get(sessionName).generateToken(tokenOptions);
// Update our collection storing the new token
this.mapSessionNamesTokens.get(sessionName).put(token, role);
// Prepare the response with the token
responseJson.put(0, token);
// Return the response to the client
return new ResponseEntity<>(responseJson, HttpStatus.OK);
} catch (OpenViduJavaClientException e1) {
// If internal error generate an error message and return it to client
return getErrorResponse(e1);
} catch (OpenViduHttpException e2) {
if (404 == e2.getStatus()) {
// Invalid sessionId (user left unexpectedly). Session object is not valid
// anymore. Clean collections and continue as new session
this.mapSessions.remove(sessionName);
this.mapSessionNamesTokens.remove(sessionName);
}
}
}
Chciałbym żeby przydzielał sesję wtedy kiedy ktoś wyśle poprawny token JWT i jakoś sensownie rozdzielać logikę na kanały. Jako że brakuje materiałów dotyczących Openvidu w połączeniu z Javą, zwracam się tutaj.