有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring boot如何在web应用程序中存储多个会话

我正在尝试制作一个web应用程序,要求用户通过Reddit登录。为此,我使用JRAW,其中使用的主要对象是RedditClient

我有点困惑,应该如何为登录的用户跟踪这些多个客户端。我有一个可用的应用程序,但我想我会用错误的方式存储它们

Auth。班级

public class Auth {

    private static final String URL = "http://localhost:4200/";

    private final UUID id = UUID.randomUUID();
    private final RedditClient redditClient = getDefaultRedditClient();
    private final Credentials credentials = getWebappCreds();
    private final URL authUrl = getClientAuthURL();

    public UUID getId(){
        return id;
    }

    public URL getAuthUrl(){
        return authUrl;
    }

    public AuthStatus getOAuthStatus(){
        return redditClient.getOAuthHelper().getAuthStatus();
    }

    @JsonIgnore
    public RedditClient getRedditClient(){
        return redditClient;
    }

    @JsonIgnore
    public Credentials getWebappCreds(){
        return Credentials.webapp("<WEB_APP_ID>", "<WEB_APP_SECRET>", URL);
    }

    private URL getClientAuthURL(){
        URL url = redditClient.getOAuthHelper().getAuthorizationUrl(credentials, true, "history");
        return url;
    }

    public void auth(String state, String code) throws NetworkException, OAuthException, IllegalStateException {
        System.out.println("auth - state: " + state + ", code: " + code);
        String url = URL + "?state=" + state + "&code=" + code; 
        System.out.println("auth - url: " + url);
        auth(url);
    }

    private void auth(String redirectURL) throws NetworkException, OAuthException, IllegalStateException {
        OAuthData data = redditClient.getOAuthHelper().onUserChallenge(redirectURL, credentials);
        redditClient.authenticate(data);
    }

    private static RedditClient getDefaultRedditClient(){
        UserAgent myUserAgent = UserAgent.of("desktop", "io.rj93.reddit.search", "v0.1", "rj93");
        return new RedditClient(myUserAgent);
    }

}

AuthController。班级

package io.rj93.reddit.search.server;

import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import net.dean.jraw.http.NetworkException;
import net.dean.jraw.http.oauth.OAuthException;

@RestController
@RequestMapping("api/v1")
@CrossOrigin
public class AuthController {

    private static ConcurrentMap<String, Auth> auths = new ConcurrentHashMap<String, Auth>();

    @RequestMapping("/auth")
    public Auth getAuth(){
        Auth auth = new Auth();
        auths.put(auth.getId().toString(), auth);
        return auth;
    }

    @RequestMapping(value = "/auth", method = RequestMethod.POST)
    public Auth authenticate(@RequestBody Map<String, String> payload) throws NetworkException, OAuthException, IllegalStateException{
        Auth auth = auths.get(payload.get("id"));
        auth.auth(payload.get("state"), payload.get("code"));
        return auth;
    }

}

它的工作方式是网站在getAuth请求一个新的身份验证,这将创建一个新的reddit客户端并开始身份验证过程。这存储在一个映射中,密钥是生成的UUID,返回给用户并将所有进一步的请求发送给服务器。网站重定向到reddit以允许用户授予权限,然后重定向到我的网站,在那里要验证的值被发送到authenticate中的服务器

我应该如何储存这个?库不允许我使用预设数据实例化reddit客户端,所以我必须经历重定向到reddit的过程,等等


共 (0) 个答案