Skip to content

Commit 8e2f059

Browse files
author
Juraj Veverka
committed
added global method security
1 parent 8c88964 commit 8e2f059

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

spring/spring-security/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ After login, each request must use same cookie JSESSIONID, because server is tra
1515
### Logout
1616
* __GET__ http://localhost:8888/services/security/logout
1717

18+
### Users, Passwords and Roles
19+
* joe / secret, ROLE_USER
20+
* jane / secret, ROLE_USER, ROLE_ADMIN
21+
* alice / secret, ROLE_PUBLIC
22+
1823
### Get protected data
19-
* __GET__ http://localhost:8888/services/data/all
24+
GET protected data for different user roles:
25+
* __GET__ http://localhost:8888/services/data/users/all (ROLE_USER, ROLE_ADMIN)
26+
* __GET__ http://localhost:8888/services/data/admins/all (ROLE_ADMIN)
2027

2128
### Get public data
22-
* __GET__ http://localhost:8888/services/public/all
29+
* __GET__ http://localhost:8888/services/public/data/all
2330

2431
### Build and run
2532
```

spring/spring-security/src/main/java/itx/examples/springboot/security/springsecurity/SpringSecurityApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
56

67
@SpringBootApplication
8+
@EnableGlobalMethodSecurity(securedEnabled = true)
79
public class SpringSecurityApplication {
810

911
public static void main(String[] args) {

spring/spring-security/src/main/java/itx/examples/springboot/security/springsecurity/rest/DataRestController.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,40 @@
66
import org.slf4j.LoggerFactory;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.http.ResponseEntity;
9+
import org.springframework.security.access.annotation.Secured;
910
import org.springframework.security.core.Authentication;
1011
import org.springframework.web.bind.annotation.GetMapping;
1112
import org.springframework.web.bind.annotation.RequestMapping;
1213
import org.springframework.web.bind.annotation.RestController;
1314

1415

1516
@RestController
16-
@RequestMapping("/services/data/")
17+
@RequestMapping("/services/data")
1718
public class DataRestController {
1819

1920
private static final Logger LOG = LoggerFactory.getLogger(DataRestController.class);
2021

2122
@Autowired
2223
private DataService dataService;
2324

24-
@GetMapping("/all")
25-
public ResponseEntity<ServerData> getData(Authentication authentication) {
25+
@Secured({"ROLE_USER", "ROLE_ADMIN"})
26+
@GetMapping("/users/all")
27+
public ResponseEntity<ServerData> getForUsersData(Authentication authentication) {
2628
LOG.info("getData: authentication={}", authentication.getName());
2729
authentication.getAuthorities().forEach(a->{
2830
LOG.info(" authority={}", a.getAuthority());
2931
});
30-
return ResponseEntity.ok().body(dataService.getSecuredData("Secured for " + authentication.getName()));
32+
return ResponseEntity.ok().body(dataService.getSecuredData("Secured for USER/ADMIN " + authentication.getName()));
33+
}
34+
35+
@Secured("ROLE_ADMIN")
36+
@GetMapping("/admins/all")
37+
public ResponseEntity<ServerData> getDataForAdmins(Authentication authentication) {
38+
LOG.info("getData: authentication={}", authentication.getName());
39+
authentication.getAuthorities().forEach(a->{
40+
LOG.info(" authority={}", a.getAuthority());
41+
});
42+
return ResponseEntity.ok().body(dataService.getSecuredData("Secured for ADMIN " + authentication.getName()));
3143
}
3244

3345
}

spring/spring-security/src/main/java/itx/examples/springboot/security/springsecurity/rest/PublicRestController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
import org.springframework.web.bind.annotation.RestController;
1212

1313
@RestController
14-
@RequestMapping("/services/public/")
14+
@RequestMapping("/services/public")
1515
public class PublicRestController {
1616

1717
private static final Logger LOG = LoggerFactory.getLogger(PublicRestController.class);
1818

1919
@Autowired
2020
private DataService dataService;
2121

22-
@GetMapping("/all")
22+
@GetMapping("/data/all")
2323
public ResponseEntity<ServerData> getData() {
2424
LOG.info("getData: ");
2525
return ResponseEntity.ok().body(dataService.getSecuredData("Public"));

spring/spring-security/src/main/java/itx/examples/springboot/security/springsecurity/services/UserAccessServiceImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public UserAccessServiceImpl() {
2424
this.users = new HashMap<>();
2525
this.users.put("joe", new UserData("joe", "secret", "ROLE_USER"));
2626
this.users.put("jane", new UserData("jane", "secret", "ROLE_ADMIN", "ROLE_USER"));
27+
this.users.put("alice", new UserData("joe", "secret", "ROLE_PUBLIC"));
2728
}
2829

2930
@Override

0 commit comments

Comments
 (0)