CAS:从 servlet 中以编程方式调用 Web 服务

CAS: Invoking a webservice programmatically from within a servlet

我在 2 台不同的服务器上有两个应用程序 运行(一个在 tomcat 上,另一个在 JBoss 上)。这两个应用程序都连接到同一个 CAS 服务器以进行身份​​验证。现在这个 CAS 服务器也驻留在同一个 JBoss 中。

Say:
App-1 --- is on tomcat and CASified
App-2 --- is on JBoss and CASified
CAS   --- is on JBoss

现在我正在从浏览器调用 App-1 的 url。出现CAS登录页面,提供username/password后,请求成功进入App-1的servlet。从这个 servlet 代码中,我试图调用驻留在 App-2 中的 Web 服务。

Note: I use axis2 for this webservice and the axis2.war is also CASified
      to the same CAS server for authentication.

无论我做什么,我都无法使这个网络服务调用正常工作。有办法实现吗?

Note: If I call the CAS REST api with a hardcoded username/password, I am 
getting the TGT, through which I am able to get the Service Ticket, with 
which I am able to invoke that web-service. But I do not want to login again 
with a hard-coded username or password. My webservice invocation should 
happen with the already logged-in user only. 

当您调用 App-2 上的 Web 服务时,您会得到会话 cookie 吗?这应该是您继续访问而无需在每次调用时重新验证的机制。如果您没有取回 cookie,则没有办法在每次不进行身份验证的情况下继续访问(即服务器无法记住它是您,并且它应该信任消息的其余部分)。

这可以通过使用 CAS 代理功能来实现。 link https://wiki.jasig.org/display/CAS/Proxy+CAS+Walkthrough 有点帮助。但无法理解从哪里开始。 首先从 http://downloads.jasig.org/cas-clients/ 中获取 CAS 客户端 jar。在我的例子中,我拿了 cas-client-core-3.3.3.jar 罐子。我已将此 jar 包含在我的应用程序 war 中。 在我的应用程序的 web.xml 中,我包含了以下 3 个 CAS 过滤器。

<!-- CAS Filters -->
<filter>
    <filter-name>CAS Validation Filter</filter-name>
    <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
    <init-param>
        <param-name>casServerUrlPrefix</param-name>
        <param-value>https://cas-hostname.domainname:port/cas</param-value>
    </init-param>
    <init-param>
        <param-name>serverName</param-name>
        <param-value>https://app-hostname.domainname:port</param-value>
    </init-param>
    <init-param>
        <param-name>proxyCallbackUrl</param-name>
        <param-value>https://app-hostname.domainname:port/app/ticket</param-value>
    </init-param>
    <init-param>
        <param-name>proxyReceptorUrl</param-name>
        <param-value>/app/ticket</param-value>
    </init-param>
</filter>


<filter>
    <filter-name>CAS Authentication Filter</filter-name>
    <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
    <init-param>
        <param-name>casServerLoginUrl</param-name>
        <param-value>https://cas-hostname.domainname:port/cas/login</param-value>
    </init-param>
    <init-param>
        <param-name>serverName</param-name>
        <param-value>https://app-hostname.domainname:port</param-value>
    </init-param>
</filter>


<filter>
    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
    <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>

  <!-- filter mappings -->
<filter-mapping>
    <filter-name>CAS Validation Filter</filter-name>
    <url-pattern>/app/*</url-pattern>
    <url-pattern>/ticket</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>CAS Authentication Filter</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

Note-1: The order of filter mapping should be as mentioned above. First CAS Validation filter mapping should come, followed by CAS Authentication Filter and last the CAS HttpServletRequest Wrapper filter.

Note-2: The URL pattern /ticket which is basically your proxy callback url need not be mentioned in the last two filters.

一旦 CAS 客户端 jar 包含在网络应用程序中并且 web.xml 配置了这些过滤器,那么所有的 http 请求都会通过这些过滤器。

因此,一旦您的 http 请求进入您的 servlet,您就可以调用以下代码片段来获取代理票:

String proxyTicket = ((AttributePrincipal) req.getUserPrincipal())
                .getProxyTicketFor(webservice_url);

req 是 HttpServletRequest 对象,AttributePrincipal 是 class,它出现在 cas-client-core-3.3.3.jar

然后可以将此 proxyTicket 作为查询字符串附加到您的网络服务 URL,如下所示:

https://myother-webservice-app.com/ws/myData?ticket=<proxyTicket>

构建完 URL 后,您就可以通过编程方式调用 Web 服务。

希望这对您有所帮助。