我想从独立应用程序进行 Cas 身份验证,但无法从服务器获取票证。任何人都可以为我提供将票证作为字符串返回的方法的示例代码,以便我可以将其用于身份验证。如您所见,唯一的参数应该是来自服务器的 URL。多数民众赞成在我还没有(我知道 casToken 初始化为空,它不工作)。
protected String getCasTicket(String serviceUrl) {
String casToken = null;
if (casToken == null){
logger.error("Failed to get CAS-Token!");
}else{
logger.info("Got CAS-Token successful!");
}
return casToken;
}
最佳答案
public class CasAuthenticationServlet extends HttpServlet {
...
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// NOTE: The CasAuthenticationToken can also be obtained using
// SecurityContextHolder.getContext().getAuthentication()
final CasAuthenticationToken token = (CasAuthenticationToken) request.getUserPrincipal();
// proxyTicket could be reused to make calls to the CAS service even if the
// target url differs
final String proxyTicket = token.getAssertion().getPrincipal().getProxyTicketFor(targetUrl);
// Make a remote call using the proxy ticket
final String serviceUrl = targetUrl+"?ticket="+URLEncoder.encode(proxyTicket, "UTF-8");
String proxyResponse = CommonUtils.getResponseFromServer(serviceUrl, "UTF-8");
...
}
CasAuthenticationProvider
构造一个 CasAuthenticationToken
,包括 TicketResponse
和 GrantedAuthoritys 中包含的详细信息。
然后控制返回到 CasAuthenticationFilter
,它将创建的 CasAuthenticationToken
放在安全上下文中。
案例示例:https://docs.spring.io/spring-security/site/docs/4.2.x/reference/html/sample-apps.html#cas-sample
编辑:
请引用https://www.javaworld.com/article/3313114/what-is-a-java-servlet-request-handling-for-java-web-applications.html用于创建 servlet
关于java - 如何从服务器获取 CasToken?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57216354/