Skip to content

Commit 843fb97

Browse files
committed
correct readme, describe permissions using in project
1 parent b67549b commit 843fb97

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

security-memory-http-basic-api/readme.md

+56
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,62 @@ The userName and password is encoded in the format `username:password`. This is
2424

2525
In case of basic authentication, the username and password is only encoded with Base64, but not encrypted or hashed in any way. Hence, it can be compromised by any man in the middle. Hence, it is always recommended to authenticate rest API calls by this header over a ssl connection.
2626

27+
## Permissions
28+
29+
In this implementation, we are not limited to roles ("ROLE_ADMIN" or "ROLE_USER").
30+
31+
Here we need a distinction at the level of rules (privileges).
32+
33+
In particular, REST has the ability to read as well as write.
34+
35+
In this project create permissions for REST company:
36+
37+
```
38+
public enum Permissions {
39+
40+
COMPANY_VIEW("company#V"), // GET
41+
COMPANY_EDIT("company#E"), // PATCH or PUT
42+
COMPANY_CREATE("company#C"); // POST or DELETE
43+
44+
private final String permissionString;
45+
46+
private Permissions(String permissionString) {
47+
this.permissionString = permissionString;
48+
}
49+
50+
public String getValue() {
51+
return permissionString;
52+
}
53+
54+
}
55+
```
56+
57+
or of course you can create permissions to all CRUD operations, like this:
58+
59+
```
60+
"company#C"
61+
"company#R"
62+
"company#U"
63+
"company#D"
64+
```
65+
66+
### Set in GrantedAuthority when create User
67+
68+
```
69+
private UserDetails createAdmin(Function<String, String> encoder) {
70+
List<GrantedAuthority> adminGrantedAuthorities = new ArrayList<>();
71+
adminGrantedAuthorities.add(new SimpleGrantedAuthority(Permissions.COMPANY_VIEW.getValue()));
72+
adminGrantedAuthorities.add(new SimpleGrantedAuthority(Permissions.COMPANY_EDIT.getValue()));
73+
adminGrantedAuthorities.add(new SimpleGrantedAuthority(Permissions.COMPANY_CREATE.getValue()));
74+
75+
return User.withUsername("admin")
76+
.passwordEncoder(encoder)
77+
.password("admin")
78+
.authorities(adminGrantedAuthorities)
79+
.build();
80+
}
81+
```
82+
2783
## Testing
2884

2985
1. Testing integration test by controller without security: `CompanyControllerTest`

test-security-memory-basic-rest/readme.md

+56
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,62 @@ The userName and password is encoded in the format `username:password`. This is
2424

2525
In case of basic authentication, the username and password is only encoded with Base64, but not encrypted or hashed in any way. Hence, it can be compromised by any man in the middle. Hence, it is always recommended to authenticate rest API calls by this header over a ssl connection.
2626

27+
## Permissions
28+
29+
In this implementation, we are not limited to roles ("ROLE_ADMIN" or "ROLE_USER").
30+
31+
Here we need a distinction at the level of rules (privileges).
32+
33+
In particular, REST has the ability to read as well as write.
34+
35+
In this project create permissions for REST company:
36+
37+
```
38+
public enum Permissions {
39+
40+
COMPANY_VIEW("company#V"), // GET
41+
COMPANY_EDIT("company#E"), // PATCH or PUT
42+
COMPANY_CREATE("company#C"); // POST or DELETE
43+
44+
private final String permissionString;
45+
46+
private Permissions(String permissionString) {
47+
this.permissionString = permissionString;
48+
}
49+
50+
public String getValue() {
51+
return permissionString;
52+
}
53+
54+
}
55+
```
56+
57+
or of course you can create permissions to all CRUD operations, like this:
58+
59+
```
60+
"company#C"
61+
"company#R"
62+
"company#U"
63+
"company#D"
64+
```
65+
66+
### Set in GrantedAuthority when create User
67+
68+
```
69+
private UserDetails createAdmin(Function<String, String> encoder) {
70+
List<GrantedAuthority> adminGrantedAuthorities = new ArrayList<>();
71+
adminGrantedAuthorities.add(new SimpleGrantedAuthority(Permissions.COMPANY_VIEW.getValue()));
72+
adminGrantedAuthorities.add(new SimpleGrantedAuthority(Permissions.COMPANY_EDIT.getValue()));
73+
adminGrantedAuthorities.add(new SimpleGrantedAuthority(Permissions.COMPANY_CREATE.getValue()));
74+
75+
return User.withUsername("admin")
76+
.passwordEncoder(encoder)
77+
.password("admin")
78+
.authorities(adminGrantedAuthorities)
79+
.build();
80+
}
81+
```
82+
2783
## Testing
2884

2985
1. Testing integration test by controller without security: `CompanyControllerTest`

0 commit comments

Comments
 (0)