Java 使用rest-assured的CAS认证
我们在应用程序中使用CAS(中央身份验证服务)进行身份验证 我们有很多RESTAPI,我们需要测试REST调用。我们计划建立一个测试套件来测试所有RESTAPI。因此,我们考虑在您的套件中使用Rest assured 问题在于身份验证。因为我们有CAS,所以每次呼叫都会重定向到登录页面,询问用户名和密码。是否有一个直接的方法可以一次性传递用户名和密码,并获得身份验证和cookiesJava 使用rest-assured的CAS认证,java,cas,rest-assured,Java,Cas,Rest Assured,我们在应用程序中使用CAS(中央身份验证服务)进行身份验证 我们有很多RESTAPI,我们需要测试REST调用。我们计划建立一个测试套件来测试所有RESTAPI。因此,我们考虑在您的套件中使用Rest assured 问题在于身份验证。因为我们有CAS,所以每次呼叫都会重定向到登录页面,询问用户名和密码。是否有一个直接的方法可以一次性传递用户名和密码,并获得身份验证和cookies 任何帮助都将不胜感激 您可以通过Rest-Assured使用auth方法进行身份验证 例如: RestAssure
任何帮助都将不胜感激 您可以通过Rest-Assured使用auth方法进行身份验证 例如:
RestAssured.given ().auth ().basic (username, password);
编辑:
您也可以尝试以下方法:
RestAssured.given ().auth ().form (username, password);
试试这个,让我们知道它是否有效。试试以下方法:
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>)
;
Response-Response=given().contentType(contentType.JSON)
.relaxedHTTPSValidation().when().get(“”);
字符串html=response.asString();
字符串sessionid=response.getCookie(“JSESSIONID”);
org.jsoup.nodes.Document loginDoc=jsoup.parse(html);
字符串ltTagValue=loginDoc.select(“输入[name=lt]”).val();
String executionTagValue=loginDoc.select(“输入[name=execution]”).val();
HashMap head=新的HashMap();
head.put(“用户代理”、“Mozilla/5.0(Windows NT 6.1;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/59.0.3071.115 Safari/537.36”);
head.put(“接受”、“文本/html”);
head.put(“接受语言”,“en-US,en;q=0.9”);
Response response2=给定().头(head)。
contentType(contentType.URLENC).cookie(“CASTGC”,即“”).cookie(“JSESSIONID”,即sessionid)。
cookie(“CASPRIVACY”一词)
.cookie(“路径”,“em-cas”)。
relaxedHTTPSValidation()。
formParam(“用户名”,“用户名”)。
formParam(“密码”,“密码”)。
formParam(“lt”,ltTagValue)。
formParam(“执行”,executionTagValue)。
formParam(“事件ID”、“提交”)。
formParam(“提交”、“登录”)。
when()。
post()
;
否。以前尝试过此操作。它不起作用。谢谢你的建议!!