Skip to content

Commit cb697c1

Browse files
committed
* add CRUD group
* add bumpversion
1 parent 3b3bbec commit cb697c1

File tree

12 files changed

+305
-30
lines changed

12 files changed

+305
-30
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* text=auto
2+
bumpversion.sh

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.gradle
2+
/build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
/out/
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/

README.md

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
Springboot 2.0.x oauth2 JWT
22
============================
33

4-
Sample Spring Boot 2.0.x OAuth2 JWT Authorization Server (JPA, Hibernate, PostgreSQL, Dockerize).
4+
Sample Spring Boot 2.0.x OAuth2 JWT Authorization Server **(JWT, JPA, Hibernate, PostgreSQL, Dockerize)**.
55

66
You can use this project to boostraping Authorization your own Application.
77

8-
If you feel happy **give a star** to this repository.
8+
If you feel happy **Give me a STAR** to this **repository**.
99

1010
## Features
1111

1212
* OAuth2.0 Authorization Server
1313
* OAuth2.0 Resource Server
14+
* We use [PostgreSQL](https://www.postgresql.org/)
1415
* We use [Liquibase](https://www.liquibase.org/)
1516
* We use [Gradle 4.10.2](https://gradle.org/install/)
16-
* We use Docker, to simplify understanding application flow.
17-
* We use JWT Token RSA, production ready.
17+
* We use [Docker](https://docs.docker.com/install/), to simplify understanding application flow.
18+
* We use [JWT](https://jwt.io/) Token RSA, production ready.
1819
* File upload API (Using Oauth2 Resource Server)
20+
* How to use Validation.
1921

2022
## Oauth2 user credential
2123

@@ -37,8 +39,12 @@ If you feel happy **give a star** to this repository.
3739

3840
For running on Docker instance, it's really simple.
3941

40-
* Clone this repo
41-
* Run ```docker-compose up -d```
42+
* Clone this repository.
43+
* ```bash cd springboot-oauth2-jwt```
44+
* ```bash docker network create odenktools-net```
45+
* Run ```bash docker-compose up -d```
46+
* make a cup of coffee...
47+
* You're done! application ready to test.
4248

4349
## Testing Apps
4450

@@ -156,7 +162,7 @@ curl --request POST \
156162
--header 'Authorization: Bearer {{YOUR_ACCESS_TOKEN}}' \
157163
--header 'Content-Type: application/x-www-form-urlencoded' \
158164
--header 'content-type: multipart/form-data' \
159-
--form 'file=@/opt/pictures/your-images.jpg'
165+
--form 'file=@/opt/pictures/{{IMAGE_WANT_TO_UPLOAD}}.jpg'
160166
```
161167

162168
## Build Application from source
@@ -165,14 +171,14 @@ First clone this repo. To build the sources you need to have [Gradle 4x](https:/
165171

166172
After the clone, create database
167173

168-
- For **Authorization Server**
174+
- **Authorization Server**
169175

170176
```bash
171177
su - postgres
172178
createdb auth_server
173179
```
174180

175-
- For **Resource Server**
181+
- **Resource Server**
176182

177183
```bash
178184
su - postgres
@@ -181,14 +187,14 @@ createdb file_server
181187

182188
After **create database**, build the entire project.
183189

184-
For **Authorization Server**
190+
- **Authorization Server**
185191

186192
```bash
187193
cd authorization_server
188194
gradle build
189195
```
190196

191-
For **Resource Server**
197+
- **Resource Server**
192198

193199
```bash
194200
cd resource_server
@@ -201,10 +207,14 @@ gradle build
201207
keytool -genkey -alias jwt -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore jwt.p12 -keypass odenktools123 -validity 3650
202208
```
203209

210+
paste to ```main/resources/certificate``` folder
211+
204212
```bash
205213
keytool -list -rfc --keystore jwt.p12 | openssl x509 -inform pem -pubkey
206214
```
207215

216+
paste to ```main/resources/certificate``` folder
217+
208218
## Todo List
209219

210220
- [x] Oauth2.0 JWT Token
@@ -215,17 +225,19 @@ keytool -list -rfc --keystore jwt.p12 | openssl x509 -inform pem -pubkey
215225

216226
## References
217227

218-
[Official Oauth2 Sample Code](https://oauth.net/code/)
228+
[Official - Oauth2 Sample Code](https://oauth.net/code/)
219229

220-
[Oauth Playground](https://developers.google.com/oauthplayground/)
230+
[Official - Spring Oauth2 Docs](https://projects.spring.io/spring-security-oauth/docs/oauth2.html)
221231

222-
[Spring Oauth2 Docs](https://projects.spring.io/spring-security-oauth/docs/oauth2.html)
232+
[Official - Spring Oauth2 ref](https://docs.spring.io/spring-security-oauth2-boot/docs/current/reference/html5/)
223233

224-
[Secure Server To Server](https://dzone.com/articles/build-a-spring-boot-app-with-secure-server-to-serv)
234+
[Official - Spring Oauth2 Tutorial](https://spring.io/guides/tutorials/spring-boot-oauth2/)
235+
236+
[Oauth Playground](https://developers.google.com/oauthplayground/)
225237

226-
[Tutorial Oauth2](https://spring.io/guides/tutorials/spring-boot-oauth2/)
238+
[Dzone - Build a Spring Boot App With Secure Server-to-Server Communication via OAuth 2.0](https://dzone.com/articles/build-a-spring-boot-app-with-secure-server-to-serv)
227239

228-
[Okta](https://developer.okta.com/blog/2018/04/02/client-creds-with-spring-boot)
240+
[Okta - Secure Server-to-Server Communication with Spring Boot and OAuth 2.0](https://developer.okta.com/blog/2018/04/02/client-creds-with-spring-boot)
229241

230242
# LICENSE
231243

authorization_server/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,21 @@ gradle bootRun
99

1010
#### Running as a Packaged Application
1111

12+
Build Jar
13+
1214
```bash
1315
gradle build -x test
16+
```
17+
18+
Or Build Jar without test
1419

20+
```bash
21+
gradle build -x test
22+
```
23+
24+
Execute Jar Package
25+
26+
```bash
1527
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8090,suspend=n -jar build/libs/authserver-0.1.0.jar
1628
```
1729

@@ -20,6 +32,8 @@ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8090,suspend=n -jar
2032
```bash
2133
docker build --tag odenktools/oauth2-authorization-server:0.1.0 --build-arg JAR_FILE=build/libs/authserver-0.1.0.jar .
2234
docker push odenktools/oauth2-authorization-server:0.1.0
35+
docker tag odenktools/oauth2-authorization-server:0.1.0 odenktools/oauth2-authorization-server:latest
36+
docker push odenktools/oauth2-authorization-server:latest
2337
```
2438

2539
# LICENSE

authorization_server/src/main/java/com/odenktools/authserver/controller/AdminAuth.java

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.JsonObject;
44
import com.odenktools.authserver.dto.group.GroupDto;
5+
import com.odenktools.authserver.entity.Group;
56
import com.odenktools.authserver.service.GroupService;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
@@ -13,9 +14,10 @@
1314

1415
import javax.validation.Valid;
1516
import java.security.Principal;
17+
import java.util.Optional;
1618

1719
/**
18-
* Oauth2 Admin Management.
20+
* Oauth2 Admin Api Management.
1921
*
2022
* @author Odenktools.
2123
*/
@@ -32,6 +34,12 @@ public AdminAuth(GroupService groupService) {
3234
this.groupService = groupService;
3335
}
3436

37+
/**
38+
* Check Authorize.
39+
*
40+
* @param principal Pricipal Person.
41+
* @return JsonObject.
42+
*/
3543
@GetMapping(value = "/me",
3644
produces = MediaType.APPLICATION_JSON_VALUE)
3745
public ResponseEntity<?> user(Principal principal) {
@@ -46,8 +54,56 @@ public ResponseEntity<?> user(Principal principal) {
4654
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.OK);
4755
}
4856

57+
//=========================== #START GROUP# ==============================//
58+
59+
/**
60+
* Get Group detail.
61+
*
62+
* @param id id do you want to check.
63+
* @return GroupDto
64+
*/
65+
@GetMapping(value = "/group/{id}",
66+
produces = MediaType.APPLICATION_JSON_VALUE)
67+
public ResponseEntity<?> findGroupById(@PathVariable Long id) {
68+
69+
Optional<Group> groupOptional = this.groupService.findById(id);
70+
71+
JsonObject jsonObject = new JsonObject();
72+
73+
if (!groupOptional.isPresent()) {
74+
jsonObject.addProperty("code", HttpStatus.BAD_REQUEST.value());
75+
jsonObject.addProperty("messages",
76+
String.format("Group with id ``%s`` not exist", id));
77+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(jsonObject.toString());
78+
}
79+
80+
Group group = groupOptional.get();
81+
82+
jsonObject.addProperty("code", HttpStatus.OK.value());
83+
84+
JsonObject jsonData = new JsonObject();
85+
jsonData.addProperty("named", group.getNamed());
86+
jsonData.addProperty("coded", group.getCoded());
87+
jsonData.addProperty("namedDescription", group.getNamedDescription());
88+
jsonData.addProperty("isActive", group.getIsActive());
89+
jsonData.addProperty("createdAt", group.getCreatedAt().toInstant().toString());
90+
jsonData.addProperty("updatedAt", group.getUpdatedAt() != null ?
91+
group.getUpdatedAt().toInstant().toString() : null);
92+
93+
//Add to sub-object
94+
jsonObject.add("data", jsonData);
95+
96+
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.OK);
97+
}
98+
99+
/**
100+
* Create a new Group.
101+
*
102+
* @param request GroupDto.
103+
* @return GroupDto.
104+
*/
49105
@PostMapping(
50-
value = "/create",
106+
value = "/group/create",
51107
consumes = MediaType.APPLICATION_JSON_VALUE,
52108
produces = MediaType.APPLICATION_JSON_VALUE
53109
)
@@ -75,8 +131,14 @@ public ResponseEntity<?> createGroup(@RequestBody @Valid GroupDto request) {
75131
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.OK);
76132
}
77133

134+
/**
135+
* Update existing group.
136+
*
137+
* @param request GroupDto want to update.
138+
* @return GroupDto.
139+
*/
78140
@PutMapping(
79-
value = "/update",
141+
value = "/group/update",
80142
consumes = MediaType.APPLICATION_JSON_VALUE,
81143
produces = MediaType.APPLICATION_JSON_VALUE
82144
)
@@ -103,4 +165,40 @@ public ResponseEntity<?> updateGroup(@RequestBody @Valid GroupDto request) {
103165
jsonObject.addProperty("messages", "Group was unsuccessfuly updated.");
104166
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.BAD_REQUEST);
105167
}
168+
169+
/**
170+
* Delete existing group.
171+
*
172+
* @param request Group do you want to delete.
173+
* @return JsonObject.
174+
*/
175+
@PutMapping(
176+
value = "/group/delete",
177+
consumes = MediaType.APPLICATION_JSON_VALUE,
178+
produces = MediaType.APPLICATION_JSON_VALUE
179+
)
180+
public ResponseEntity<?> removeGroup(@RequestBody @Valid GroupDto request) {
181+
182+
JsonObject jsonObject = new JsonObject();
183+
184+
if (!groupService.existById(request.getId())) {
185+
jsonObject.addProperty("code", HttpStatus.BAD_REQUEST.value());
186+
jsonObject.addProperty("messages", String.format("Group with code ``%s`` not exist",
187+
request.getCoded()));
188+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(jsonObject.toString());
189+
}
190+
191+
boolean removed = this.groupService.removeGroup(request);
192+
193+
if (removed) {
194+
jsonObject.addProperty("code", HttpStatus.OK.value());
195+
jsonObject.addProperty("messages", "Group was successfuly removed.");
196+
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.OK);
197+
}
198+
199+
jsonObject.addProperty("code", HttpStatus.BAD_REQUEST.value());
200+
jsonObject.addProperty("messages", "Group was unsuccessfuly removed.");
201+
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.BAD_REQUEST);
202+
}
203+
//=========================== #END GROUP# ==============================//
106204
}

authorization_server/src/main/java/com/odenktools/authserver/entity/OauthClientDetails.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ public class OauthClientDetails implements ClientDetails, Serializable {
3030
private static final ObjectMapper mapper = new ObjectMapper();
3131

3232
@Id
33-
@Column(nullable = false, unique = true)
33+
@Column(name = "client_id", nullable = false, unique = true)
3434
private String clientId;
3535

36-
@Column(name = "client_secret", nullable = false)
36+
@Column(name = "client_secret", nullable = false, unique = true)
3737
private String clientSecret;
3838

3939
@Column(name = "resource_ids")
4040
private String resourceIds;
4141

42-
4342
@Column(name = "scope")
4443
private String scope;
4544

@@ -64,6 +63,10 @@ public class OauthClientDetails implements ClientDetails, Serializable {
6463
@Column
6564
private String additionalInformation;
6665

66+
private Set<String> getAutoApproveScope() {
67+
return StringUtils.commaDelimitedListToSet(this.autoApproveScope);
68+
}
69+
6770
@Override
6871
public String getClientId() {
6972
return this.clientId;
@@ -133,7 +136,19 @@ public Integer getRefreshTokenValiditySeconds() {
133136

134137
@Override
135138
public boolean isAutoApprove(String scope) {
136-
return this.getAutoApproveScope().contains(scope);
139+
if (this.autoApproveScope == null) {
140+
return false;
141+
} else {
142+
Iterator scopeIterator = this.getAutoApproveScope().iterator();
143+
String auto;
144+
do {
145+
if (!scopeIterator.hasNext()) {
146+
return false;
147+
}
148+
auto = (String) scopeIterator.next();
149+
} while (!auto.equals("true") && !scope.matches(auto));
150+
return true;
151+
}
137152
}
138153

139154
@Override
@@ -146,10 +161,6 @@ public Map<String, Object> getAdditionalInformation() {
146161
}
147162
}
148163

149-
public Set<String> getAutoApproveScope() {
150-
return StringUtils.commaDelimitedListToSet(this.autoApproveScope);
151-
}
152-
153164
public void setClientId(String clientId) {
154165
this.clientId = clientId;
155166
}

0 commit comments

Comments
 (0)