سؤال

I am testing Oauth2 with Twitter, according to discussions Twitter only allows 127.0.0.1 (it considers localhost invalid).

I am using Javascript to open a pop-up window and detect the callback to the urlI registered: https://127.0.0.1:8144/Oauth2Callback

However, my JavaScript originates from localhost, and according to sources the two are considered different for the 'same origin' policy.

How can I write a redirect from 127.0.0.1 to localhost ?

I am using Java & also tuckey-url-rewrite, so either would work.


My Controller looks like this:

@RequestMapping("/oauth2callback")
@ResponseBody
public String callback() {
    return "oauth2callback";
}

<rule>
    <name>Twitter Hack</name>
<condition name="host" operator="equal"> ??? 127.0.0.1 ??? /condition>
    <from>(.*)</from>
    <to type="redirect"> ??? localhost ???</to> 
</rule>

I want:

twitter --(callback)--> 127 --(redirect)--> localhost

هل كانت مفيدة؟

المحلول 2

This seems to work:

@RequestMapping("/oauth2callback")
public String callback(HttpServletRequest request) {

    /*
     * Terrible hack for testing Twitter Oauth on 'localhost'
     */
    if (request.getServerName().contains("127.0.0.1")){
        int port = request.getServerPort();
        String query = (request.getQueryString() == null) ? "" : "?" +request.getQueryString();
        String url = "redirect:https://localhost:" + port +"/oauth2callback" + query;
        return url;
    }

    return "account/oauth2callback";
}

account/oauth2callback is a simple jsp with only :

<body>
    <p>oauth2callback</p>
</body>

There's probably a better way to do this through tuckey-url-rewrite.

نصائح أخرى

check your OS's etc/host file and set the 127.0.0.1 to localhost string. It should be there by default, but sometimes it can be an issue. On windows, this is present under C:\windows\System\drivers\etc\ folder.

Its the name resolution for your OS, so you can actually put an IP to name mapping for resolving issues on your host.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top