question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to login to spring secured API ?

See original GitHub issue

The developers have created the application with spring security and i need to login to that application.

These are the request mapping the application uses for further request :

@RestController
@RequestMapping("/service/billing")

Following is my code :

@Before
	public void init(){
		RestAssured.baseURI = "https://example.com";
		RestAssured.port = 8080;
		RestAssured.basePath = "/portal";
	}

@Test
	public void userAuthentication(){
given().
		auth().form(userName, password, FormAuthConfig.springSecurity().
				withLoggingEnabled(new LogConfig(captor,true))).
		expect().
		statusCode(200).body(equalTo("OK")).
		when().
		get("/service/billing");
	}

In response, i get the HTML Page as Login Page, means the application not logged in successfully with assertion error. I tried with each and everything to get out of this. Used fromAuthconfig as mentioned above, but didn’t get though.

Can anybody help me on this? I am newbie to this rest assured api.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
GheorgheSacultanucommented, Jul 26, 2020

Hello guys,

There is no news regarding this issue?

I’m also having difficulties to fill the login form for this website -> https://www.themoviedb.org/login.

This is my code:

 public String  login() {
        StringBuilder cookiesBuilder = new StringBuilder();
        RestAssured.given().log().all()

                        .auth().form("<username>", "<password>", new FormAuthConfig("/login", "username", "password").withLoggingEnabled())
                        .filter(new ResponseLoggingFilter())
                        .filter(new RequestLoggingFilter())
                        .baseUri("https://www.themoviedb.org")
                        .relaxedHTTPSValidation()
                        .when()
                        .post("/login")
                        .then()
                        .assertThat().log().all()

        .extract().cookies().forEach((s, s2) -> {
            cookiesBuilder.append(s)
                    .append("=")
                    .append(s2)
                    .append("; ");
        });
        return cookiesBuilder.toString().trim();

Also, I tried this one :

public String  login() {
        StringBuilder cookiesBuilder = new StringBuilder();
        RestAssured.given().log().all()
               .contentType(ContentType.URLENC)
               .queryParam("username", "<username>")
               .queryParam("password", "<password>")
               .header("Content-Type", "application/x-www-form-urlencoded")
                .post("https://www.themoviedb.org.login)

        .extract().cookies().forEach((s, s2) -> {
            cookiesBuilder.append(s)
                    .append("=")
                    .append(s2)
                    .append("; ");
        });
        return cookiesBuilder.toString().trim();
1reaction
doncemcommented, May 31, 2018

After numerous attempts applied these calls and worked:

RestAssured.given() // other set up methods
    .contentType(ContentType.URLENC)
    .formParam("username", "admin")
    .formParam("password", "admin")
    .post("/login")
    .then()
    .statusCode(FOUND.value());

Next steps I’ve checked location header and it was the one I expected

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial | Spring Security and Angular
In this tutorial we show some nice features of Spring Security, Spring Boot and Angular working together to provide a pleasant and secure...
Read more >
Spring Security Form Login - Baeldung
This tutorial will focus on Login with Spring Security. We're going to build on top of the previous Spring MVC example, as that's...
Read more >
Login and Registration REST API using Spring Boot, Spring ...
You will learn how to build login or sign-in and registration or signup REST API using Spring boot, Spring Security, Hibernate, MySQL database....
Read more >
How to secure REST with Spring Security - InfoWorld
Provide a UI with a button that sends a request to a back-end endpoint. · Provide a username and password field for users...
Read more >
Securing a Rest API with Spring Security - OctoPerf
Securing a Rest API with Spring Security · user-crud-api. The User crud API is responsible of storing users somewhere. · user-auth-api. The user- ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found