如何将127.0.0.1重定向到localhost(Java)?

时间:2013-10-03 17:05:00

标签: java javascript spring-mvc url-rewriting tuckey-urlrewrite-filter

我正在使用Twitter测试Oauth2,根据discussions,Twitter仅允许127.0.0.1(它认为localhost无效)。

我正在使用Javascript打开弹出窗口并检测到已注册的urlI的回调:https://127.0.0.1:8144/Oauth2Callback

但是,我的JavaScript源自localhost,根据sources,两者被认为是不同的来源相同的来源'政策。

如何编写从127.0.0.1localhost的重定向?

我正在使用Java&还有tuckey-url-rewrite,所以要么会工作。


我的控制器看起来像这样:

@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>

我想:

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

2 个答案:

答案 0 :(得分:1)

检查操作系统的etc / host文件,并将127.0.0.1设置为localhost字符串。它应该默认存在,但有时它可能是一个问题。在Windows上,它出现在C:\ windows \ System \ drivers \ etc \文件夹下。

它是您的操作系统的名称解析,因此您实际上可以将IP映射到名称映射以解决主机上的问题。

答案 1 :(得分:0)

这似乎有效:

@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是一个简单的jsp,只有:

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

通过tuckey-url-rewrite可能有更好的方法。

相关问题