public class FormAuthConfig extends Object
Constructor and Description |
---|
FormAuthConfig()
Creates a new empty
FormAuthConfig . |
FormAuthConfig(String formAction,
String userNameInputTagName,
String passwordInputTagName)
Create a form auth config with a pre-defined form action, username input tag, password input tag.
|
Modifier and Type | Method and Description |
---|---|
FormAuthConfig |
and()
Syntactic sugar
|
static FormAuthConfig |
formAuthConfig()
Creates a new empty
FormAuthConfig . |
List<String> |
getAdditionalInputFieldNames() |
String |
getCsrfFieldName() |
String |
getFormAction() |
LogConfig |
getLogConfig() |
LogDetail |
getLogDetail() |
String |
getPasswordInputTagName() |
String |
getUserInputTagName() |
boolean |
hasAdditionalInputFieldNames() |
boolean |
hasCsrfFieldName() |
boolean |
hasFormAction() |
boolean |
hasPasswordInputTagName() |
boolean |
hasUserInputTagName() |
boolean |
isAutoDetectCsrfFieldName() |
boolean |
isLoggingEnabled() |
boolean |
requiresParsingOfLoginPage() |
FormAuthConfig |
sendCsrfTokenAsFormParam() |
FormAuthConfig |
sendCsrfTokenAsHeader() |
boolean |
shouldSendCsrfTokenAsFormParam() |
static FormAuthConfig |
springSecurity() |
FormAuthConfig |
withAdditionalField(String fieldName)
Include additional field when using form authentication by including input field value with the specified name.
|
FormAuthConfig |
withAdditionalFields(String firstFieldName,
String secondFieldName,
String... additionalFieldNames)
Include multiple additional fields when using form authentication by including input field values with the specified name.
|
FormAuthConfig |
withAutoDetectionOfCsrf()
Enable Cross-site request forgery (csrf) support when using form authentication by automatically trying to find the name and value of the csrf input field.
|
FormAuthConfig |
withCsrfFieldName(String fieldName)
Enable Cross-site request forgery (csrf) support when using form authentication by including the csrf value of the input field with the specified name.
|
FormAuthConfig |
withLoggingEnabled()
Enables logging with log level
LogDetail.ALL of the request made to authenticate using
form authentication. |
FormAuthConfig |
withLoggingEnabled(LogConfig logConfig)
Enables logging with log level
LogDetail.ALL of the request made to authenticate using
form authentication using the specified LogConfig . |
FormAuthConfig |
withLoggingEnabled(LogDetail logDetail)
Enables logging with the supplied logDetail of the request made to authenticate using form authentication.
|
FormAuthConfig |
withLoggingEnabled(LogDetail logDetail,
LogConfig logConfig)
Enables logging with the supplied log detail of the request made to authenticate using form authentication using the
specified
LogConfig . |
public FormAuthConfig(String formAction, String userNameInputTagName, String passwordInputTagName)
<form action="/j_spring_security_check"> <label for="j_username">Username</label> <input type="text" name="j_username" id="j_username"/> <br/> <label for="j_password">Password</label> <input type="password" name="j_password" id="j_password"/> <br/> <input type='checkbox' name='_spring_security_remember_me'/> Remember me on this computer. <br/> <input type="submit" value="Login"/> </form>This means that
formAction
should be set to /j_spring_security_check
, userNameInputTagName
should be set to j_username
and passwordInputTagName
should be set to j_password
.formAction
- The action of the formuserNameInputTagName
- The name of the username input tag in the login formpasswordInputTagName
- The name of the password input tag in the login formpublic FormAuthConfig()
FormAuthConfig
.public static FormAuthConfig springSecurity()
public FormAuthConfig withCsrfFieldName(String fieldName)
<html> <head> <title>Login</title> </head> <body> <form action="j_spring_security_check_with_csrf" method="POST"> <table> <tr> <td>User: </td> <td><input type="text" name="j_username"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="j_password"></td> </tr> <tr> <td colspan="2"><input name="submit" type="submit"/></td> </tr> </table> <input type="hidden" name="_csrf" value="8adf2ea1-b246-40aa-8e13-a85fb7914341"/> </form> </body> </html>The csrf field name is called
_csrf
.
Important: When enabling csrf support then REST Assured must always make an additional request to the server in order to
be able to include in the csrf value which will slow down the tests.fieldName
- The name of the input fieldwithAutoDetectionOfCsrf()
public FormAuthConfig withAdditionalField(String fieldName)
<html> <head> <title>Login</title> </head> <body> <form action="j_spring_security_check_with_csrf" method="POST"> <table> <tr> <td>User: </td> <td><input type="text" name="j_username"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="j_password"></td> </tr> <tr> <td colspan="2"><input name="submit" type="submit"/></td> </tr> </table> <input type="hidden" name="something" value="8adf2ea1-b246-40aa-8e13-a85fb7914341"/> </form> </body> </html>and you'd like to include the field named
something
as an additional form parameter in the request you can do like this:
given().auth().form(..., new FormAuthConfig(..).withAdditionalField("something"). ..and then REST Assured will send the form parameter
something=8adf2ea1-b246-40aa-8e13-a85fb7914341
Important: When including an additional field without specifying a value then REST Assured must always make an additional request to the server in order to
be able to figure out the field value. This will slow down the tests.fieldName
- The first field name to includepublic FormAuthConfig withAdditionalFields(String firstFieldName, String secondFieldName, String... additionalFieldNames)
withAdditionalField(String)
but for multiple fields.
Important: When including an additional field without specifying a value then REST Assured must always make an additional request to the server in order to
be able to figure out the field value. This will slow down the tests.firstFieldName
- The first additional input field to includesecondFieldName
- The second additional input field to includeadditionalFieldNames
- Additional field name to include (optional)public FormAuthConfig sendCsrfTokenAsHeader()
public FormAuthConfig sendCsrfTokenAsFormParam()
public FormAuthConfig withAutoDetectionOfCsrf()
<html> <head> <title>Login</title> </head> <body> <form action="j_spring_security_check_with_csrf" method="POST"> <table> <tr> <td>User: </td> <td><input type="text" name="j_username"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="j_password"></td> </tr> <tr> <td colspan="2"><input name="submit" type="submit"/></td> </tr> </table> <input type="hidden" name="_csrf" value="8adf2ea1-b246-40aa-8e13-a85fb7914341"/> </form> </body> </html>The csrf field name is called
_csrf
and REST Assured will autodetect its name since the field name is the only hidden
field on this page.
If auto-detection fails you can consider using withCsrfFieldName(String)
.
Important: When enabling csrf support then REST Assured must always make an additional request to the server in order to
be able to include in the csrf value which will slow down the tests.withCsrfFieldName(String)
public FormAuthConfig withLoggingEnabled()
LogDetail.ALL
of the request made to authenticate using
form authentication. Both the request and the response is logged.public FormAuthConfig withLoggingEnabled(LogDetail logDetail)
public FormAuthConfig withLoggingEnabled(LogConfig logConfig)
LogDetail.ALL
of the request made to authenticate using
form authentication using the specified LogConfig
. Both the request and the response is logged.public FormAuthConfig withLoggingEnabled(LogDetail logDetail, LogConfig logConfig)
LogConfig
. Both the request and the response is logged.public static FormAuthConfig formAuthConfig()
FormAuthConfig
.public FormAuthConfig and()
public String getFormAction()
public String getUserInputTagName()
public String getPasswordInputTagName()
null
if undefinedpublic LogConfig getLogConfig()
public boolean isLoggingEnabled()
true
if logging is enabled or false
otherwise.public LogDetail getLogDetail()
null
if undefinedpublic String getCsrfFieldName()
null
if undefinedpublic List<String> getAdditionalInputFieldNames()
public boolean hasCsrfFieldName()
true
if csrf field name is defined or false
otherwise.public boolean hasAdditionalInputFieldNames()
true
if additional input field name have been specified or false
otherwise.public boolean isAutoDetectCsrfFieldName()
true
if auto detection of csrf field name is enabled, false
otherwise.public boolean hasUserInputTagName()
true
if the user input tag name is defined or false
otherwise.public boolean hasPasswordInputTagName()
true
if the password input tag name is defined or false
otherwise.public boolean hasFormAction()
true
if the form action is defined or false
otherwise.public boolean requiresParsingOfLoginPage()
true
if the FormAuthConfig
instance contains settings that require REST Assured to make a request to the server before applying form authentication, false
otherwise.public boolean shouldSendCsrfTokenAsFormParam()
true
if the csrf token should be sent as a form param or false
if it's sent as a header.Copyright © 2010–2020. All rights reserved.