1

我需要保留用户的令牌,如 play 的“记住我”,但我无法在 mac 上使用 safari、chrome 设置 cookie(换句话说,我无法获取名为“userToken”的 cookie)。我应该怎么办?我使用 play!frameworkv1.2.4

public class Security extends Secure.Security {

static boolean authenticate(String username, String password) {
    String token = "";
    //to get API token
    MultivaluedMap postParams = new MultivaluedMapImpl();
    postParams.add("login[email]", username);;
    MultivaluedMap queryParams = new MultivaluedMapImpl();
    queryParams = new MultivaluedMapImpl();
    queryParams.add("path_info", "login");
    String url = Play.configuration.getProperty("mctokenendpoint");

    Client client = new Client();
    client.setFollowRedirects(true);
    WebResource resource = client.resource(url);
    try {
        String resultJSON = resource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON)
                .post(String.class, postParams);
        Map result;
        if (resultJSON != null) {
            try {
                result = new ObjectMapper().readValue(resultJSON, Map.class);
                token = result.get("id").toString() + "-" + result.get("token").toString();
                session.put("loginToken", token);
                //keep token
                rememberToken(token);
                return true;
            } catch (Exception e) {
                Logger.error(e.toString(), e);
                return false;
            }
        }
    } catch (Exception e) {
        Logger.error(e.toString(), e);
        return false;
    }
    return false;
}

/**
 * remember token
 * @param token
 */
@Util
public static void rememberToken(String token) {
    if (token != null && !token.equals("")) {
        Date expiration = new Date();
        String duration = "1mn"; // maybe make this override-able
        if(duration.indexOf("d") != -1 && duration.indexOf("d")+1 == duration.length() ) {
            duration = String.valueOf(24*Long.parseLong(duration.substring(0, duration.indexOf("d"))));
        }
        expiration.setTime(expiration.getTime() + Time.parseDuration(duration)*1000);
        response.setCookie("userToken", Crypto.sign(token + "_" + expiration.getTime()) + "_" + token + "_"
                + expiration.getTime(), duration);
    }
}

}


@With(Secure.class)
public class ApplicationController extends Controller {

@Before
public static void commBefore() {
    Http.Cookie remember = request.cookies.get("rememberme");

    if (remember != null) {
        Http.Cookie userToken = request.cookies.get("userToken");

        if (userToken != null) {
            int firstIndex = userToken.value.indexOf("_");
            int lastIndex = userToken.value.lastIndexOf("_");
            if (lastIndex > firstIndex) {
                String token = userToken.value.substring(firstIndex + 1, lastIndex);
                String time = userToken.value.substring(lastIndex + 1);
                Date expirationDate = new Date(Long.parseLong(time)); 
                Date now = new Date();
                if (expirationDate == null || expirationDate.before(now)) {
                    try {

                        Secure.logout();
                    } catch (Throwable e) {
                        Logger.error(e.toString(), e);
                        error();
                    }
                }
                if (session.get("loginToken") != null) {
                    if (!session.get("loginToken").equals(token)) {
                        //remove old token
                        session.remove("loginToken");
                        //set new token
                        session.put("loginToken", token);
                    }
                } else {
                    session.put("loginToken", token);
                }
            }
        } else {
            try {
                Secure.logout();
            } catch (Throwable e) {
                Logger.error(e.toString(), e);
                error();
            }
        }
    } 


}
}

这些代码无法在mac上完美运行,但我不知道,请帮助我

4

0 回答 0