简体   繁体   中英

CAS authentication using rest assured

We use CAS (Central Authentication Service) for authentication in our application.

We have a lot of REST APIs and we need to test the rest calls. We are planning to set up a test suite which tests all the REST api's. 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. Is there a straight to pass the username and password in one shot and get authenticated and get the cookies?

Any help will be appreciated!!

You can authenticate via Rest-Assured using auth method.

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>)
 ; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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