【问题标题】:contextpath is not setting in url when jsp to servelt is calling调用 jsp 到 servlet 时,未在 url 中设置 contextpath
【发布时间】:2020-01-21 04:06:36
【问题描述】:

我有一个名为

的 maven web 项目
gitlab.configuration
所以项目路径应该是
http://localhost:8080/gitlab.configuration/

有一个jsp文件,它包含“a”html标签。单击它时,我将被重定向到预定义的回调 url。这是

http://localhost:8080/gitlaboslcadaptor/oauth/callback

但正确的 url 应该是

而不是上面的 url
http://localhost:8080/gitlab.configuration/gitlaboslcadaptor/oauth/callback

我的问题是为什么它没有设置这个上下文路径?

这里我也附上了一些图片。

如果您不清楚,请查看图片以了解我所说的内容。

entryPointForOauth2caller.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>Oauth2caller</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container d-flex h-100">
    <div class="row align-self-center w-100">
        <div class="col-6 mx-auto">
            <div class="jumbotron text-center">
               <!-- <form action="https://gitlab.com/oauth/authorize?client_id=153c017c0f33d32abfb8b950983dcfce0cbe7ba49751033c0626c4f00f51ac72&redirect_uri=http://localhost:8080/gitlaboslcadaptor/oauth2/callback&response_type=code&state=941891abc&scope=api+read_user+read_registry+sudo+openid+profile+email" method="get">
                    <button class="btn btn-primary" type="submit">MakeAuthorize</button>
                </form> -->
                <a href="https://gitlab.com/oauth/authorize?client_id=153c017c0f33d32abfb8b950983dcfce0cbe7ba49751033c0626c4f00f51ac72&response_type=code&redirect_uri=http://localhost:8080/gitlaboslcadaptor/oauth2/callback&state=941891abc&scope=api+read_user+read_registry+sudo+openid+profile+email">Authorize</a>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Oauth2ProviderServlet.java(servlet 类)

package com.persistent.unite.oslc4j.gitlab.security.oauth2;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Oauth2ProviderServlet
 */

@WebServlet("/gitlaboslcadaptor/oauth2/callback/*")
public class Oauth2ProviderServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Oauth2ProviderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("init mehtod in provider");
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code = request.getParameter("code");
        String state = request.getParameter("state");
        System.out.println("call back url's code for access token: " + code);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("dopost mehtod called.");
        doGet(request, response);
    }

}

如果回调 url 被调用,那么我的 servlet doget 方法至少应该打印一些东西。但似乎我的 serlvet 根本不会打电话。

在这里我附上了我得到的图片。

点击输出后的“授权”链接后。

【问题讨论】:

    标签: java jsp servlets oauth-2.0 gitlab


    【解决方案1】:

    经过数小时的调试,我发现了问题所在。 回调 url 配置不正确。 最初是

     http://localhost:8080/gitlaboslcadaptor/oauth/callback 
    但正确的应该是
    http://localhost:8080/gitlab.configuration/gitlaboslcadaptor/oauth/callback 

    这个网址也应该正确地写在它自己的gitlab帐户中。

    【讨论】:

      猜你喜欢
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 2012-12-08
      • 1970-01-01
      相关资源
      最近更新 更多