Skip to content

Commit 0dfaae0

Browse files
refactor : separate the standalone API from the caller (client)
1 parent 4656b17 commit 0dfaae0

File tree

199 files changed

+2585
-1293
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+2585
-1293
lines changed

.gitignore

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/.docker/ssl/*
2-
!/.docker/ssl/.gitkeep
1+
/client/.docker/ssl/*
2+
!/client/.docker/ssl/.gitkeep
33

44
/target/
5+
/client/target/
56

67
# docker
78
/.docker/env/*
@@ -18,28 +19,37 @@
1819
/.springBeans
1920
/.sts4-cache
2021

22+
/client/.apt_generated
23+
/client/.classpath
24+
/client/.factorypath
25+
/client/.project
26+
/client/.settings
27+
/client/.springBeans
28+
/client/.sts4-cache
29+
2130
# Mac
2231
/.DS_Store
23-
32+
/client/.DS_Store
2433

2534
### IntelliJ IDEA ###
2635
/.idea
2736
/*.iws
2837
/*.iml
2938
/*.ipr
3039

40+
/client/.idea
41+
/client/*.iws
42+
/client/*.iml
43+
/client/*.ipr
44+
3145
# logs
3246
/logs/*
3347
!/logs/.gitkeep
3448

49+
/client/logs/*
50+
!/client/logs/.gitkeep
51+
3552
# files
3653
/files/*
3754
!/files/.gitkeep
3855

39-
# .properties
40-
#/src/main/resources/application.properties
41-
42-
# logback
43-
#/src/main/resources/logback-spring.xml
44-
45-

README.md

+64-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
* Separated UserDetails implementation for Admin and Customer roles.
1818
* Integration with spring-security-oauth2-authorization-server.
1919
* Provide MySQL DDL, which consists of oauth\_access\_token, oauth\_refresh\_token and oauth\_client\_details, which is tables in Security 5. As I mean to migrate current security system to Security 6, I haven't changed them to the ``authorization`` table indicated in https://github.com/spring-projects/spring-authorization-server.
20-
* Application of Spring Rest Docs.
20+
* Application of Spring Rest Docs.
21+
2122
## Dependencies
2223
2324
| Category | Dependencies |
@@ -28,12 +29,72 @@
2829
| Package-Manager | Maven 3.6.3 (mvnw, Dockerfile) |
2930
| RDBMS | Mysql 8.0.17 |
3031
31-
## Implementation
32+
## Run the App
3233
3334
#### Import the SQL file in the ``mysql`` folder.
3435
35-
#### The API information is found on ``http://localhost:8505/docs/api-app.html``, managed by Spring Rest Doc
36+
#### Install Maven
37+
```shell
38+
mvnw clean install
39+
cd client
40+
mvnw clean install
41+
```
42+
- Run the client module by running ``SpringSecurityOauth2PasswordJpaImplApplication`` in the client.
43+
- The API information is found on ``http://localhost:8370/docs/api-app.html``, managed by Spring Rest Doc
44+
- In case you use IntelliJ, I recommend creating an empty project and importing the API (root) module and client module separately.
45+
- The client module definitely consumes the API module, but not vice versa.
3646
47+
## Implementation of the Api
48+
### The implementation method is shown in the client source code.
49+
50+
- **Registration**
51+
- As the Api consumes JPA, adding it to Beans is required.
52+
53+
```java
54+
55+
// ADD 'com.patternknife.securityhelper.oauth2.api'
56+
@SpringBootApplication(scanBasePackages = {"com.patternknife.securityhelper.oauth2.client", "com.patternknife.securityhelper.oauth2.api"})
57+
public class SpringSecurityOauth2PasswordJpaImplApplication {
58+
59+
public static void main(String[] args) {
60+
SpringApplication.run(SpringSecurityOauth2PasswordJpaImplApplication.class, args);
61+
}
62+
63+
}
64+
```
65+
66+
```java
67+
@Configuration
68+
// ADD 'com.patternknife.securityhelper.oauth2.api.config.security'
69+
@EnableJpaRepositories(
70+
basePackages = {"com.patternknife.securityhelper.oauth2.client.domain",
71+
"com.patternknife.securityhelper.oauth2.client.config.securityimpl",
72+
"com.patternknife.securityhelper.oauth2.api.config.security"},
73+
entityManagerFactoryRef = "commonEntityManagerFactory",
74+
transactionManagerRef= "commonTransactionManager"
75+
)
76+
public class CommonDataSourceConfiguration {
77+
78+
79+
// ADD 'com.patternknife.securityhelper.oauth2.api.config.security'
80+
@Primary
81+
@Bean(name = "commonEntityManagerFactory")
82+
public LocalContainerEntityManagerFactoryBean commonEntityManagerFactory(EntityManagerFactoryBuilder builder) {
83+
return builder
84+
.dataSource(commonDataSource())
85+
.packages("com.patternknife.securityhelper.oauth2.client.domain",
86+
"com.patternknife.securityhelper.oauth2.api.config.security")
87+
.persistenceUnit("commonEntityManager")
88+
.build();
89+
}
90+
91+
}
92+
```
93+
94+
- **Implementation**
95+
- In fact, the only mandatory settings are 'CustomAuthenticationProvider' and 'CustomUserDetailsServiceFactory'. The rest depend on your specific situation.
96+
97+
![img.png](reference/doc/img.png)
3798
3899
39100
### Running this App with Docker
File renamed without changes.
File renamed without changes.

client/.gitignore

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/.docker/ssl/*
2+
!/.docker/ssl/.gitkeep
3+
4+
/target/
5+
6+
# docker
7+
/.docker/env/*
8+
!/.docker/env/.gitkeep
9+
/.docker/ssh/*
10+
!/.docker/ssh/.gitkeep
11+
12+
### STS ###
13+
/.apt_generated
14+
/.classpath
15+
/.factorypath
16+
/.project
17+
/.settings
18+
/.springBeans
19+
/.sts4-cache
20+
21+
# Mac
22+
/.DS_Store
23+
24+
25+
### IntelliJ IDEA ###
26+
/.idea
27+
/*.iws
28+
/*.iml
29+
/*.ipr
30+
31+
# logs
32+
/logs/*
33+
!/logs/.gitkeep
34+
35+
# files
36+
/files/*
37+
!/files/.gitkeep
38+
39+
# .properties
40+
#/src/main/resources/application.properties
41+
42+
# logback
43+
#/src/main/resources/logback-spring.xml
44+
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2007-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import java.net.*;
17+
import java.io.*;
18+
import java.nio.channels.*;
19+
import java.util.Properties;
20+
21+
public class MavenWrapperDownloader {
22+
23+
private static final String WRAPPER_VERSION = "0.5.6";
24+
/**
25+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
26+
*/
27+
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
28+
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
29+
30+
/**
31+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
32+
* use instead of the default one.
33+
*/
34+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
35+
".mvn/wrapper/maven-wrapper.properties";
36+
37+
/**
38+
* Path where the maven-wrapper.jar will be saved to.
39+
*/
40+
private static final String MAVEN_WRAPPER_JAR_PATH =
41+
".mvn/wrapper/maven-wrapper.jar";
42+
43+
/**
44+
* Name of the property which should be used to override the default download url for the wrapper.
45+
*/
46+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
47+
48+
public static void main(String args[]) {
49+
System.out.println("- Downloader started");
50+
File baseDirectory = new File(args[0]);
51+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
52+
53+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
54+
// wrapperUrl parameter.
55+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
56+
String url = DEFAULT_DOWNLOAD_URL;
57+
if(mavenWrapperPropertyFile.exists()) {
58+
FileInputStream mavenWrapperPropertyFileInputStream = null;
59+
try {
60+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
61+
Properties mavenWrapperProperties = new Properties();
62+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
63+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
64+
} catch (IOException e) {
65+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
66+
} finally {
67+
try {
68+
if(mavenWrapperPropertyFileInputStream != null) {
69+
mavenWrapperPropertyFileInputStream.close();
70+
}
71+
} catch (IOException e) {
72+
// Ignore ...
73+
}
74+
}
75+
}
76+
System.out.println("- Downloading from: " + url);
77+
78+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
79+
if(!outputFile.getParentFile().exists()) {
80+
if(!outputFile.getParentFile().mkdirs()) {
81+
System.out.println(
82+
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
83+
}
84+
}
85+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
86+
try {
87+
downloadFileFromURL(url, outputFile);
88+
System.out.println("Done");
89+
System.exit(0);
90+
} catch (Throwable e) {
91+
System.out.println("- Error downloading");
92+
e.printStackTrace();
93+
System.exit(1);
94+
}
95+
}
96+
97+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
98+
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
99+
String username = System.getenv("MVNW_USERNAME");
100+
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
101+
Authenticator.setDefault(new Authenticator() {
102+
@Override
103+
protected PasswordAuthentication getPasswordAuthentication() {
104+
return new PasswordAuthentication(username, password);
105+
}
106+
});
107+
}
108+
URL website = new URL(urlString);
109+
ReadableByteChannel rbc;
110+
rbc = Channels.newChannel(website.openStream());
111+
FileOutputStream fos = new FileOutputStream(destination);
112+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
113+
fos.close();
114+
rbc.close();
115+
}
116+
117+
}

client/.mvn/wrapper/maven-wrapper.jar

49.5 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

client/Dockerfile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM maven:3.6.3-jdk-8-slim AS build
2+
3+
ARG PROJECT_ROOT_IN_CONTAINER
4+
5+
# In the ./src/main/resources folder, 1) application.properties, 2) logback-spring.xml and 3) [the filename of 'server.ssl.key-store' in your properties] should be located.
6+
COPY ./ $PROJECT_ROOT_IN_CONTAINER
7+
USER root
8+
WORKDIR $PROJECT_ROOT_IN_CONTAINER
9+
10+
RUN --mount=type=cache,target=/root/.m2 mvn -f $PROJECT_ROOT_IN_CONTAINER/pom.xml clean install
11+
12+
FROM openjdk:17-alpine
13+
14+
ARG PROJECT_ROOT_IN_CONTAINER
15+
ARG FILE_STORAGE_ROOT_IN_CONTAINER
16+
ARG JVM_XMS
17+
ARG JVM_XMX
18+
19+
COPY --from=build $PROJECT_ROOT_IN_CONTAINER/ $PROJECT_ROOT_IN_CONTAINER
20+
21+
USER root
22+
WORKDIR $PROJECT_ROOT_IN_CONTAINER
23+
24+
RUN cp $PROJECT_ROOT_IN_CONTAINER/target/*.jar /app.jar
25+
26+
RUN ln -s $PROJECT_ROOT_IN_CONTAINER/.docker/entrypoint/run-app.sh /run-app.sh
27+
28+
RUN apk --no-cache add curl bash fontconfig ttf-dejavu
29+
30+
ENV PROJECT_ROOT_IN_CONTAINER=$PROJECT_ROOT_IN_CONTAINER
31+
ENV FILE_STORAGE_ROOT_IN_CONTAINER=$FILE_STORAGE_ROOT_IN_CONTAINER
32+
ENV JVM_XMS=$JVM_XMS
33+
ENV JVM_XMX=$JVM_XMX
34+
ENV RESOURCES_TYPE=$RESOURCES_TYPE
35+
36+
ENTRYPOINT sh /run-app.sh $PROJECT_ROOT_IN_CONTAINER $FILE_STORAGE_ROOT_IN_CONTAINER $JVM_XMS $JVM_XMX && /bin/sh

client/deploy.bat

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
set local_maven_repo="C:\Users\Andrew Kang\.m2\repository\com\patternknife\securityhelper\oauth2\spring-security-oauth2-password-jpa-implementation"
3+
mvnw.cmd -DaltDeploymentRepository=snapshot-repo::default::file://%local_maven_repo%/snapshots clean deploy

client/deploy.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
local_maven_repo='/mnt/c/Users/Andrew\sKang/.m2/repository/com/patternknife/securityhelper/oauth2/spring-security-oauth2-password-jpa-implementation'
3+
mvn -DaltDeploymentRepository=snapshot-repo::default::file://${local_maven_repo}/snapshots clean deploy
4+

0 commit comments

Comments
 (0)