java在Spring Security中使用CAS时如何对客户端服务进行身份验证?
我已经创建了CAS服务器和CAS客户端应用程序。基本上,服务应用程序的身份验证是使用服务票证和代理票证验证完成的。在这种情况下,我首先通过验证服务票证来验证特定用户,然后通过使用服务票证生成代理票证来授权特定应用程序的用户。在这种情况下,我能够在服务应用程序中成功验证服务和代理票证。我面临的问题是,一旦我验证了用户,我就可以使用下面的代码段通过断言对象检索用户信息
Assertion assertion = serviceTicketValidator.validate(serviceTicket);
这里serviceTicektValidator
对象是我通过实现Cas20ServiceTicketValidator
valdation函数编写的一个类。这里断言对象设置为SecurityContextHolder
,如下所示
CasAssertionAuthenticationToken token = new CasAssertionAuthenticationToken(assertion, serviceTicket);
SecurityContextHolder.getContext().setAuthentication(token);
通过这种方式,我将设置身份验证对象。但是事实是一旦请求被改变或者考虑一个新的请求,在下面的代码段的^ {CD4}过滤器类中,将清除^ {< CD5> }对象。因此,一旦触发了一个新请求,已经存储的身份验证细节就会从SecurityContextHolder
对象中消失。因此,每次请求都必须设置身份验证详细信息,因为特定的服务或代理票证只能验证一次。在这里,我采取的方法是保留会话中生成的Assertion
对象和票证,然后在这个类的开头(或者实际上我在过滤器中执行所有这些操作),我检查这些值是否在会话中,并且在每次新请求时CasAssertionAuthenticationToken
都被设置为SecurityContextHolder
HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
SecurityContext contextBeforeChainExecution = repo.loadContext(holder);
try {
SecurityContextHolder.setContext(contextBeforeChainExecution);
chain.doFilter(holder.getRequest(), holder.getResponse());
} finally {
SecurityContext contextAfterChainExecution = SecurityContextHolder.getContext();
// Crucial removal of SecurityContextHolder contents - do this before anything else.
SecurityContextHolder.clearContext();
repo.saveContext(contextAfterChainExecution, holder.getRequest(), holder.getResponse());
request.removeAttribute(FILTER_APPLIED);
if (debug) {
logger.debug("SecurityContextHolder now cleared, as request processing completed");
}
}
应用程序正在以预期的方式工作,但我想知道是否有更好的方法通过在Spring过滤器或任何其他选项中使用高级技术来实现这一点。我还考虑了票证和身份验证信息的本地存储,但我希望在Spring应用程序中实现这一点,即使不使用此会话存储机制。我想知道这是否可能
共 (0) 个答案