1

play の「remember me」のようにユーザーのトークンを保持する必要がありますが、Mac の Safari、chrome で Cookie を設定できません (つまり、「userToken」という名前の Cookie を取得できません)。私は何をすべきか?私は play!framework を使っていますv1.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