BAEL-5674: basic auth with Postman

This commit is contained in:
emanueltrandafir1993 2022-09-11 15:18:37 +02:00
parent 536866c40b
commit 6773b9b3e8
4 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,62 @@
{
"variables": [],
"info": {
"name": "Baeldung - Basic Auth",
"_postman_id": "a1e733bc-15ca-3c5d-7fdb-d119518ed3eb",
"description": "",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "using authorization tab",
"request": {
"url": "http://localhost:8080/postman-test",
"method": "GET",
"header": [
{
"key": "admin",
"value": "baeldung",
"description": "",
"disabled": true
},
{
"key": "Authorization",
"value": "Basic YWRtaW46YmFlbGR1bmc=",
"description": ""
}
],
"body": {},
"description": ""
},
"response": []
},
{
"name": "using header tab",
"request": {
"url": "http://localhost:8080/postman-test",
"method": "GET",
"header": [
{
"key": "Authorization",
"value": "Basic YWRtaW46YmFlbGR1bmc=",
"description": ""
}
],
"body": {},
"description": ""
},
"response": []
},
{
"name": "using interceptor",
"request": {
"url": "http://localhost:8080/postman-test",
"method": "GET",
"header": [],
"body": {},
"description": ""
},
"response": []
}
]
}

View File

@ -0,0 +1,12 @@
package com.baeldung.postman.basic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PostmanBasicAuthApplication {
public static void main(String[] args) {
SpringApplication.run(PostmanBasicAuthApplication.class, args);
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.postman.basic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class PostmanBasicAuthConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf()
.disable()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password("{noop}baeldung2")
.roles("USER");
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.postman.basic;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PostmanBasicAuthController {
@GetMapping("postman-test")
public String test() {
return "request was authorized!";
}
}