Skip to content

Commit 4175061

Browse files
timoteoponcepivovarit
authored andcommitted
Bael 1964 (#4881)
* Added initial code for BAEL-1964, in-memory authentication application * Switched to default security encoder instead of a specific one
1 parent cae67c0 commit 4175061

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.inmemory;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class InMemoryAuthApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(InMemoryAuthApplication.class, args);
11+
}
12+
13+
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.inmemory;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class InMemoryAuthController {
10+
11+
@GetMapping("/public/hello")
12+
public List<String> publicHello() {
13+
return Arrays.asList("Hello", "World", "from", "Public");
14+
}
15+
16+
@GetMapping("/private/hello")
17+
public List<String> privateHello() {
18+
return Arrays.asList("Hello", "World", "from", "Private");
19+
}
20+
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.inmemory;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7+
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
8+
import org.springframework.security.crypto.password.PasswordEncoder;
9+
10+
@Configuration
11+
public class InMemoryAuthWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
12+
13+
@Override
14+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
15+
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
16+
auth.inMemoryAuthentication()
17+
.passwordEncoder(encoder)
18+
.withUser("spring")
19+
.password(encoder.encode("secret"))
20+
.roles("USER");
21+
}
22+
23+
@Override
24+
protected void configure(HttpSecurity http) throws Exception {
25+
http.authorizeRequests()
26+
.antMatchers("/private/**")
27+
.authenticated()
28+
.antMatchers("/public/**")
29+
.permitAll()
30+
.and()
31+
.httpBasic();
32+
}
33+
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.inmemory;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
10+
import org.springframework.boot.test.web.client.TestRestTemplate;
11+
import org.springframework.http.HttpStatus;
12+
import org.springframework.http.ResponseEntity;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest(classes = InMemoryAuthApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
17+
public class InMemoryAuthControllerTest {
18+
19+
@Autowired
20+
private TestRestTemplate template;
21+
22+
@Test
23+
public void givenRequestOnPublicService_shouldSucceedWith200() throws Exception {
24+
ResponseEntity<String> result = template.getForEntity("/public/hello", String.class);
25+
assertEquals(HttpStatus.OK, result.getStatusCode());
26+
}
27+
28+
@Test
29+
public void givenRequestOnPrivateService_shouldFailWith401() throws Exception {
30+
ResponseEntity<String> result = template.getForEntity("/private/hello", String.class);
31+
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
32+
}
33+
34+
@Test
35+
public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
36+
ResponseEntity<String> result = template.withBasicAuth("spring", "secret")
37+
.getForEntity("/private/hello", String.class);
38+
assertEquals(HttpStatus.OK, result.getStatusCode());
39+
}
40+
41+
@Test
42+
public void givenInvalidAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
43+
ResponseEntity<String> result = template.withBasicAuth("spring", "wrong")
44+
.getForEntity("/private/hello", String.class);
45+
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
46+
}
47+
48+
}

0 commit comments

Comments
 (0)