25

I have configured a form-base authentication using Spring security. It works fine when I log in using the login form in my web application.

It also works with cURL:

curl --data "j_username=myname&j_password=mypassword" http://localhost:8080/test/j_spring_security_check --verbose 

However, I cannot make it works using Postman:

enter image description here

What is missing? I need to be authenticated in order to test other services.

7 Answers 7

18

You can achieve authentication/authorization in postman through various authorization types given in postman dropdown under Authorization tab.

Below is the step to use Basic Auth which by default spring security provides.

In spring security you can customize your credentials in application.properties file as given below.

spring.security.user.name=yer spring.security.user.password=galem

Postman Spring security Basic Auth Form

Sign up to request clarification or add additional context in comments.

Comments

15

Finally I managed making Postman aware of authentication by using the Postman Interceptor chrome extension

With the extension enabled (by clicking the satellite icon within the Postman App), you just need to log in using the login form of your web and then you can start using services that require authentication in Postman.

3 Comments

Does this work with the Postman Native App (not chrome app) as well?
For this to work with Postman Native App, you must install the Interceptor Bridge: learning.getpostman.com/docs/postman/sending-api-requests/…
Well, for me it worked without the interceptor. How does this also solve the problem of authentication via postman?
12

Using http.httpBasic worked for me.

http.httpBasic() .and() .authorizeRequests() .antMatchers("/api/home") .hasRole("ADMIN") .antMatchers("/api/product/*") .hasRole("ADMIN") .and() .formLogin(); 

Comments

7

For me the Postman Interceptor was not working,
So I did the following and now I can login to the server.

  1. Select POST request from dropdown and type login URL in request URL section.
  2. Select Body from tabs
  3. Enter username and password keys and values as shown in picture.
  4. And click send this will create a temp cookie in postman and be there for a session.
  5. You can also do GET request for logout URL to logout from session. Or delete the cookie from Cookies below Send button. enter image description here

2 Comments

when i send post req with form data as you described i always get 404,. the same goes with x-www-form-urlencoded .but when i choose non-body, then i get bed credentials. all works fine with web browser.
i figured it out, postman has automatic redirection turn on. turing off, solved the problem
2

When using basic auth on postman, you will set the credentials on the authorization tab. Click on Authorization, choose the type as Basic Auth, the credentials section will be displayed for you to key in the username and password.

Comments

2

http.httpBasic is working for Testing spring security with Postman.

For example, Added the override configure method from the configuration class MyConfiguration.java

@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/**").permitAll().anyRequest().authenticated() .and().formLogin().permitAll().and().logout().permitAll().and().httpBasic(); http.cors().disable().csrf().disable(); } 

Note: To access the POST, PUT, and DELETE request you need to disable the cors and csrf as per code above.

Comments

0

What worked for me is:

  1. Create POST request to given security endpoint

  2. Set j_username and j_password in the x-www-form-urlencoded tab: enter image description here

  3. Put headers as follows: enter image description here

  4. In the settings tab I disabled following redirects: enter image description here In that way when you call the request you will be able to check what was the result, for instance check the Set-Cookie Headers.

After calling this request you should be able to see the Set-Cookie and Location headers like: enter image description here

This cookie which was just set should be used for all next requests for given domain. In that way those will be authenticated.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.