This repository was archived by the owner on Feb 23, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 10 files changed +47
-130
lines changed
java/com/example/securingweb Expand file tree Collapse file tree 10 files changed +47
-130
lines changed Original file line number Diff line number Diff line change 14
14
<version >0.0.1-SNAPSHOT</version >
15
15
16
16
<dependencies >
17
- <dependency >
18
- <groupId >org.springframework.boot</groupId >
19
- <artifactId >spring-boot-starter-thymeleaf</artifactId >
20
- </dependency >
21
17
<dependency >
22
18
<groupId >org.springframework.boot</groupId >
23
19
<artifactId >spring-boot-starter-web</artifactId >
24
- <exclusions >
25
- <exclusion >
26
- <groupId >org.apache.tomcat.embed</groupId >
27
- <artifactId >tomcat-embed-core</artifactId >
28
- </exclusion >
29
- <exclusion >
30
- <groupId >org.apache.tomcat.embed</groupId >
31
- <artifactId >tomcat-embed-websocket</artifactId >
32
- </exclusion >
33
- </exclusions >
34
- </dependency >
35
- <dependency >
36
- <groupId >org.apache.tomcat.experimental</groupId >
37
- <artifactId >tomcat-embed-programmatic</artifactId >
38
- <version >${tomcat.version} </version >
39
20
</dependency >
40
21
<dependency >
41
22
<groupId >org.springframework.boot</groupId >
62
43
</plugins >
63
44
</build >
64
45
65
- </project >
46
+ </project >
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 6
6
@ SpringBootApplication
7
7
public class SecuringWebApplication {
8
8
9
- public static void main (String [] args ) throws Throwable {
10
- SpringApplication .run (SecuringWebApplication .class , args );
11
- }
9
+ public static void main (String [] args ) {
10
+ SpringApplication .run (SecuringWebApplication .class , args );
11
+ }
12
12
13
13
}
Original file line number Diff line number Diff line change
1
+ package com .example .securingweb ;
2
+
3
+ import java .security .Principal ;
4
+
5
+ import org .springframework .http .MediaType ;
6
+ import org .springframework .web .bind .annotation .GetMapping ;
7
+ import org .springframework .web .bind .annotation .RequestMapping ;
8
+ import org .springframework .web .bind .annotation .RestController ;
9
+
10
+ /**
11
+ * @author Moritz Halbritter
12
+ */
13
+ @ RestController
14
+ @ RequestMapping (path = "/rest" , produces = MediaType .TEXT_PLAIN_VALUE )
15
+ public class TestRestController {
16
+ @ GetMapping ("/anonymous" )
17
+ public String anonymous () {
18
+ return "anonymous" ;
19
+ }
20
+
21
+ @ GetMapping ("/authorized" )
22
+ public String authorized (Principal principal ) {
23
+ return "authorized: " + principal .getName ();
24
+ }
25
+
26
+ @ GetMapping ("/admin" )
27
+ public String admin (Principal principal ) {
28
+ return "admin: " + principal .getName ();
29
+ }
30
+ }
Original file line number Diff line number Diff line change 4
4
import org .springframework .context .annotation .Configuration ;
5
5
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
6
6
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
7
- import org .springframework .security .config .annotation .web .configurers .LogoutConfigurer ;
8
7
import org .springframework .security .core .userdetails .User ;
9
8
import org .springframework .security .core .userdetails .UserDetails ;
10
9
import org .springframework .security .core .userdetails .UserDetailsService ;
@@ -17,16 +16,13 @@ public class WebSecurityConfig {
17
16
@ Bean
18
17
public SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
19
18
return http
20
- .authorizeRequests (authorize -> authorize
21
- .antMatchers ("/" , "/home" ).permitAll ()
22
- .antMatchers ("/admin" ).hasRole ("ADMIN" )
23
- .anyRequest ().authenticated ()
24
- )
25
- .formLogin (formLogin -> formLogin
26
- .loginPage ("/login" )
27
- .permitAll ()
28
- )
29
- .logout (LogoutConfigurer ::permitAll )
19
+ .authorizeRequests (authorize -> authorize
20
+ .mvcMatchers ("/rest/anonymous" ).permitAll ()
21
+ .mvcMatchers ("/rest/admin" ).hasRole ("ADMIN" )
22
+ .anyRequest ().authenticated ()
23
+ )
24
+ .httpBasic ()
25
+ .and ()
30
26
.build ();
31
27
}
32
28
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -3,7 +3,11 @@ source ${PWD%/*samples/*}/scripts/wait.sh
3
3
RC=0
4
4
5
5
wait_log target/native/test-output.txt " Started SecuringWebApplication" || RC=$?
6
- wait_http localhost:8080/home " Welcome" || RC=$?
7
- wait_command_output ' curl -I localhost:8080/hello' " HTTP/1.1 302" || RC=$?
6
+ wait_http localhost:8080/rest/anonymous " anonymous" || RC=$?
7
+ wait_command_output ' curl -s -u user:password localhost:8080/rest/authorized' " authorized: user" || RC=$?
8
+ wait_command_output ' curl -s -u admin:password localhost:8080/rest/admin' " admin: admin" || RC=$?
9
+ wait_command_output ' curl -s -I localhost:8080/rest/authorized' " HTTP/1.1 401" || RC=$?
10
+ wait_command_output ' curl -s -I localhost:8080/rest/admin' " HTTP/1.1 401" || RC=$?
11
+ wait_command_output ' curl -s -I -u user:password localhost:8080/rest/admin' " HTTP/1.1 403" || RC=$?
8
12
9
13
exit $RC
You can’t perform that action at this time.
0 commit comments