32.3.3 Authenticating to a Stateless Service with CAS(使用CAS向无状态服务进行身份验证)

本节介绍如何使用CAS对服务进行身份验证。换句话说,本节讨论如何设置一个客户端,该客户端使用一个使用CAS进行身份验证的服务。下一节描述如何设置无状态服务来使用CAS进行身份验证。

Configuring CAS to Obtain Proxy Granting Tickets配置证书颁发机构以获取代理授予票证

为了向无状态服务进行身份验证,应用程序需要获得代理授权票据(PGT)。本节介绍如何配置Spring Security,以便在首次[服务票证身份验证]配置后获得PGT。

第一步是在您的Spring安全配置中包含一个代理拦截器存储ProxyGrantingTicketStorage 。这用于存储由CasAuthenticationFilter获取的PGT,以便它们可以用于获取代理票证。下面显示了一个配置示例

1 <!--
2 NOTE: In a real application you should not use an in
3         memory implementation. You will also want to ensure
4         to clean up expired tickets by calling ProxyGrantingTicketStorage.cleanup()
5 -->
6 <bean id="pgtStorage" class="org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl"/>

下一步是更新CasAuthenticationProvider,以便能够获取代理票证。为此,请用Cas20ProxyTicketValidator替换Cas20ServiceTicketValidator。proxyCallbackUrl 应该设置为应用程序将接收PGT的网址。最后,配置还应该引用代理拦截器存储ProxyGrantingTicketStorage ,这样它就可以使用PGT来获取代理票证。您可以在下面找到应该进行的配置更改示例。

 1 <bean id="casAuthenticationProvider"
 2     class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
 3 ...
 4 <property name="ticketValidator">
 5     <bean class="org.jasig.cas.client.validation.Cas20ProxyTicketValidator">
 6       <constructor-arg value="https://localhost:9443/cas"/>
 7       <property name="proxyCallbackUrl" value="https://localhost:8443/cas-sample/login/cas/proxyreceptor"/>
 9       <property name="proxyGrantingTicketStorage" ref="pgtStorage"/>
10     </bean>
11 </property>
12 </bean>

最后一步是更新CasAuthenticationFilter以接受PGT,并将它们存储在代理拦截存储ProxyGrantingTicketStorage中。proxyReceptorUrl 与Cas20ProxyTicketValidator的proxyCallbackUrl 匹配非常重要。下面显示了一个配置示例。

1 <bean id="casFilter"class="org.springframework.security.cas.web.CasAuthenticationFilter">
3     ...
4     <property name="proxyGrantingTicketStorage" ref="pgtStorage"/>
5     <property name="proxyReceptorUrl" value="/login/cas/proxyreceptor"/>
6 </bean>

Calling a Stateless Service Using a Proxy Ticket使用代理票证调用无状态服务

现在,Spring Security获得了PGTs,您可以使用它们来创建代理票证,该票证可以用于向无状态服务进行身份验证。CAS示例应用程序包含了一个ProxyTicketSampleServlet中的工作示例。示例代码可以在下面找到:

 1 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 2     throws ServletException, IOException {
 3    // NOTE: The CasAuthenticationToken can also be obtained using
 4    // SecurityContextHolder.getContext().getAuthentication()
 5     final CasAuthenticationToken token = (CasAuthenticationToken) request.getUserPrincipal();
 6    // proxyTicket could be reused to make calls to the CAS service even if the
 7    // target url differs
 8    final String proxyTicket = token.getAssertion().getPrincipal().getProxyTicketFor(targetUrl);
 9 
10    // Make a remote call using the proxy ticket
11    final String serviceUrl = targetUrl+"?ticket="+URLEncoder.encode(proxyTicket, "UTF-8");
12    String proxyResponse = CommonUtils.getResponseFromServer(serviceUrl, "UTF-8");
13    ...
14 }

 

posted @ 2020-08-15 13:02  节日快乐  阅读(433)  评论(0编辑  收藏  举报