简体   繁体   English

CAS认证使用放心

[英]CAS authentication using rest assured

We use CAS (Central Authentication Service) for authentication in our application. 我们在应用程序中使用CAS(中央身份验证服务)进行身份验证。

We have a lot of REST APIs and we need to test the rest calls. 我们有很多REST API,我们需要测试其余的调用。 We are planning to set up a test suite which tests all the REST api's. 我们计划建立一个测试套件,以测试所有REST api。 So, we thought of using Rest-assured for your suite. 因此,我们考虑为您的套房使用“保证放心”。

The problem is the authentication. 问题是身份验证。 Since we have CAS in place, for each call it redirects to the login page asking for username and password. 由于我们已经安装了CAS,因此对于每个呼叫,它都会重定向到登录页面,询问用户名和密码。 Is there a straight to pass the username and password in one shot and get authenticated and get the cookies? 是否可以一口气通过用户名和密码并通过身份验证并获取Cookie?

Any help will be appreciated!! 任何帮助将不胜感激!!

You can authenticate via Rest-Assured using auth method. 您可以使用auth方法通过Rest-Assured进行身份验证。

For example: 例如:

RestAssured.given ().auth ().basic (username, password);

Edit: 编辑:

You can also try this: 您也可以尝试以下操作:

RestAssured.given ().auth ().form (username, password);

Try this and let us know if this works. 试试这个,让我们知道是否可行。

Try Following: 尝试以下操作:

Response response = given().contentType(ContentType.JSON)
        .relaxedHTTPSValidation().when().get("<URL>");

String html = response.asString();



String sessionid = response.getCookie("JSESSIONID");
 org.jsoup.nodes.Document loginDoc = Jsoup.parse(html);
 String ltTagValue = loginDoc.select("input[name=lt]").val();
 String executionTagValue = loginDoc.select("input[name=execution]").val();

HashMap<String, Object> head = new HashMap();

 head.put("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
 head.put("Accept","text/html");
 head.put("Accept-Language" ,"en-US,en;q=0.9");


 Response response2=   given().headers(head).

 contentType(ContentType.URLENC).cookie("CASTGC","").cookie("JSESSIONID", sessionid).
 cookie("CASPRIVACY","")
 .cookie("Path","/em-cas").
 relaxedHTTPSValidation().    
 formParam("username", "<abc>").
 formParam("password", "<xyz>").
 formParam("lt", ltTagValue).
 formParam("execution", executionTagValue).
 formParam("_eventId", "submit").
 formParam("submit", "LOGIN").

 when().
 post(<URL>)
 ; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM