variphx commited on
Commit
b58280e
·
0 Parent(s):
Files changed (29) hide show
  1. .gitattributes +2 -0
  2. .gitignore +34 -0
  3. .mvn/wrapper/maven-wrapper.properties +19 -0
  4. mvnw +259 -0
  5. mvnw.cmd +149 -0
  6. pom.xml +193 -0
  7. src/main/java/com/example/app/AppApplication.java +13 -0
  8. src/main/java/com/example/app/configurations/DataSourceConfiguration.java +17 -0
  9. src/main/java/com/example/app/configurations/PasswordEncoderConfiguration.java +14 -0
  10. src/main/java/com/example/app/configurations/SecurityConfiguration.java +64 -0
  11. src/main/java/com/example/app/controllers/UserAccountController.java +30 -0
  12. src/main/java/com/example/app/controllers/UserAuthController.java +38 -0
  13. src/main/java/com/example/app/dtos/JwtRequestDto.java +11 -0
  14. src/main/java/com/example/app/dtos/UserAccountCreationDto.java +13 -0
  15. src/main/java/com/example/app/dtos/UserAccountDto.java +14 -0
  16. src/main/java/com/example/app/entities/Role.java +31 -0
  17. src/main/java/com/example/app/entities/UserAccount.java +40 -0
  18. src/main/java/com/example/app/filters/JwtAuthFilter.java +51 -0
  19. src/main/java/com/example/app/mappers/JwtRequestMapper.java +20 -0
  20. src/main/java/com/example/app/mappers/Mapper.java +7 -0
  21. src/main/java/com/example/app/mappers/UserAccountCreationMapper.java +32 -0
  22. src/main/java/com/example/app/mappers/UserAccountMapper.java +20 -0
  23. src/main/java/com/example/app/misc/JwtRequest.java +13 -0
  24. src/main/java/com/example/app/repositories/UserAccountRepository.java +11 -0
  25. src/main/java/com/example/app/services/JwtService.java +77 -0
  26. src/main/java/com/example/app/services/UserAccountDetails.java +60 -0
  27. src/main/java/com/example/app/services/UserAccountService.java +36 -0
  28. src/main/resources/application.properties +5 -0
  29. src/test/java/com/example/app/AppApplicationTests.java +13 -0
.gitattributes ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ /mvnw text eol=lf
2
+ *.cmd text eol=crlf
.gitignore ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .envrc
2
+ HELP.md
3
+ target/
4
+ .mvn/wrapper/maven-wrapper.jar
5
+ !**/src/main/**/target/
6
+ !**/src/test/**/target/
7
+
8
+ ### STS ###
9
+ .apt_generated
10
+ .classpath
11
+ .factorypath
12
+ .project
13
+ .settings
14
+ .springBeans
15
+ .sts4-cache
16
+
17
+ ### IntelliJ IDEA ###
18
+ .idea
19
+ *.iws
20
+ *.iml
21
+ *.ipr
22
+
23
+ ### NetBeans ###
24
+ /nbproject/private/
25
+ /nbbuild/
26
+ /dist/
27
+ /nbdist/
28
+ /.nb-gradle/
29
+ build/
30
+ !**/src/main/**/build/
31
+ !**/src/test/**/build/
32
+
33
+ ### VS Code ###
34
+ .vscode/
.mvn/wrapper/maven-wrapper.properties ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+ wrapperVersion=3.3.2
18
+ distributionType=only-script
19
+ distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip
mvnw ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ # ----------------------------------------------------------------------------
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ # ----------------------------------------------------------------------------
20
+
21
+ # ----------------------------------------------------------------------------
22
+ # Apache Maven Wrapper startup batch script, version 3.3.2
23
+ #
24
+ # Optional ENV vars
25
+ # -----------------
26
+ # JAVA_HOME - location of a JDK home dir, required when download maven via java source
27
+ # MVNW_REPOURL - repo url base for downloading maven distribution
28
+ # MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
29
+ # MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output
30
+ # ----------------------------------------------------------------------------
31
+
32
+ set -euf
33
+ [ "${MVNW_VERBOSE-}" != debug ] || set -x
34
+
35
+ # OS specific support.
36
+ native_path() { printf %s\\n "$1"; }
37
+ case "$(uname)" in
38
+ CYGWIN* | MINGW*)
39
+ [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")"
40
+ native_path() { cygpath --path --windows "$1"; }
41
+ ;;
42
+ esac
43
+
44
+ # set JAVACMD and JAVACCMD
45
+ set_java_home() {
46
+ # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched
47
+ if [ -n "${JAVA_HOME-}" ]; then
48
+ if [ -x "$JAVA_HOME/jre/sh/java" ]; then
49
+ # IBM's JDK on AIX uses strange locations for the executables
50
+ JAVACMD="$JAVA_HOME/jre/sh/java"
51
+ JAVACCMD="$JAVA_HOME/jre/sh/javac"
52
+ else
53
+ JAVACMD="$JAVA_HOME/bin/java"
54
+ JAVACCMD="$JAVA_HOME/bin/javac"
55
+
56
+ if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then
57
+ echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2
58
+ echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2
59
+ return 1
60
+ fi
61
+ fi
62
+ else
63
+ JAVACMD="$(
64
+ 'set' +e
65
+ 'unset' -f command 2>/dev/null
66
+ 'command' -v java
67
+ )" || :
68
+ JAVACCMD="$(
69
+ 'set' +e
70
+ 'unset' -f command 2>/dev/null
71
+ 'command' -v javac
72
+ )" || :
73
+
74
+ if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then
75
+ echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2
76
+ return 1
77
+ fi
78
+ fi
79
+ }
80
+
81
+ # hash string like Java String::hashCode
82
+ hash_string() {
83
+ str="${1:-}" h=0
84
+ while [ -n "$str" ]; do
85
+ char="${str%"${str#?}"}"
86
+ h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296))
87
+ str="${str#?}"
88
+ done
89
+ printf %x\\n $h
90
+ }
91
+
92
+ verbose() { :; }
93
+ [ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; }
94
+
95
+ die() {
96
+ printf %s\\n "$1" >&2
97
+ exit 1
98
+ }
99
+
100
+ trim() {
101
+ # MWRAPPER-139:
102
+ # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds.
103
+ # Needed for removing poorly interpreted newline sequences when running in more
104
+ # exotic environments such as mingw bash on Windows.
105
+ printf "%s" "${1}" | tr -d '[:space:]'
106
+ }
107
+
108
+ # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties
109
+ while IFS="=" read -r key value; do
110
+ case "${key-}" in
111
+ distributionUrl) distributionUrl=$(trim "${value-}") ;;
112
+ distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;;
113
+ esac
114
+ done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties"
115
+ [ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties"
116
+
117
+ case "${distributionUrl##*/}" in
118
+ maven-mvnd-*bin.*)
119
+ MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/
120
+ case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in
121
+ *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;;
122
+ :Darwin*x86_64) distributionPlatform=darwin-amd64 ;;
123
+ :Darwin*arm64) distributionPlatform=darwin-aarch64 ;;
124
+ :Linux*x86_64*) distributionPlatform=linux-amd64 ;;
125
+ *)
126
+ echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2
127
+ distributionPlatform=linux-amd64
128
+ ;;
129
+ esac
130
+ distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
131
+ ;;
132
+ maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
133
+ *) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
134
+ esac
135
+
136
+ # apply MVNW_REPOURL and calculate MAVEN_HOME
137
+ # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
138
+ [ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}"
139
+ distributionUrlName="${distributionUrl##*/}"
140
+ distributionUrlNameMain="${distributionUrlName%.*}"
141
+ distributionUrlNameMain="${distributionUrlNameMain%-bin}"
142
+ MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}"
143
+ MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")"
144
+
145
+ exec_maven() {
146
+ unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || :
147
+ exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD"
148
+ }
149
+
150
+ if [ -d "$MAVEN_HOME" ]; then
151
+ verbose "found existing MAVEN_HOME at $MAVEN_HOME"
152
+ exec_maven "$@"
153
+ fi
154
+
155
+ case "${distributionUrl-}" in
156
+ *?-bin.zip | *?maven-mvnd-?*-?*.zip) ;;
157
+ *) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;;
158
+ esac
159
+
160
+ # prepare tmp dir
161
+ if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then
162
+ clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; }
163
+ trap clean HUP INT TERM EXIT
164
+ else
165
+ die "cannot create temp dir"
166
+ fi
167
+
168
+ mkdir -p -- "${MAVEN_HOME%/*}"
169
+
170
+ # Download and Install Apache Maven
171
+ verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
172
+ verbose "Downloading from: $distributionUrl"
173
+ verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
174
+
175
+ # select .zip or .tar.gz
176
+ if ! command -v unzip >/dev/null; then
177
+ distributionUrl="${distributionUrl%.zip}.tar.gz"
178
+ distributionUrlName="${distributionUrl##*/}"
179
+ fi
180
+
181
+ # verbose opt
182
+ __MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR=''
183
+ [ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v
184
+
185
+ # normalize http auth
186
+ case "${MVNW_PASSWORD:+has-password}" in
187
+ '') MVNW_USERNAME='' MVNW_PASSWORD='' ;;
188
+ has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;;
189
+ esac
190
+
191
+ if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then
192
+ verbose "Found wget ... using wget"
193
+ wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl"
194
+ elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then
195
+ verbose "Found curl ... using curl"
196
+ curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl"
197
+ elif set_java_home; then
198
+ verbose "Falling back to use Java to download"
199
+ javaSource="$TMP_DOWNLOAD_DIR/Downloader.java"
200
+ targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName"
201
+ cat >"$javaSource" <<-END
202
+ public class Downloader extends java.net.Authenticator
203
+ {
204
+ protected java.net.PasswordAuthentication getPasswordAuthentication()
205
+ {
206
+ return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() );
207
+ }
208
+ public static void main( String[] args ) throws Exception
209
+ {
210
+ setDefault( new Downloader() );
211
+ java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() );
212
+ }
213
+ }
214
+ END
215
+ # For Cygwin/MinGW, switch paths to Windows format before running javac and java
216
+ verbose " - Compiling Downloader.java ..."
217
+ "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java"
218
+ verbose " - Running Downloader.java ..."
219
+ "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")"
220
+ fi
221
+
222
+ # If specified, validate the SHA-256 sum of the Maven distribution zip file
223
+ if [ -n "${distributionSha256Sum-}" ]; then
224
+ distributionSha256Result=false
225
+ if [ "$MVN_CMD" = mvnd.sh ]; then
226
+ echo "Checksum validation is not supported for maven-mvnd." >&2
227
+ echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
228
+ exit 1
229
+ elif command -v sha256sum >/dev/null; then
230
+ if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then
231
+ distributionSha256Result=true
232
+ fi
233
+ elif command -v shasum >/dev/null; then
234
+ if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then
235
+ distributionSha256Result=true
236
+ fi
237
+ else
238
+ echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2
239
+ echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
240
+ exit 1
241
+ fi
242
+ if [ $distributionSha256Result = false ]; then
243
+ echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2
244
+ echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2
245
+ exit 1
246
+ fi
247
+ fi
248
+
249
+ # unzip and move
250
+ if command -v unzip >/dev/null; then
251
+ unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip"
252
+ else
253
+ tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar"
254
+ fi
255
+ printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url"
256
+ mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
257
+
258
+ clean || :
259
+ exec_maven "$@"
mvnw.cmd ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <# : batch portion
2
+ @REM ----------------------------------------------------------------------------
3
+ @REM Licensed to the Apache Software Foundation (ASF) under one
4
+ @REM or more contributor license agreements. See the NOTICE file
5
+ @REM distributed with this work for additional information
6
+ @REM regarding copyright ownership. The ASF licenses this file
7
+ @REM to you under the Apache License, Version 2.0 (the
8
+ @REM "License"); you may not use this file except in compliance
9
+ @REM with the License. You may obtain a copy of the License at
10
+ @REM
11
+ @REM http://www.apache.org/licenses/LICENSE-2.0
12
+ @REM
13
+ @REM Unless required by applicable law or agreed to in writing,
14
+ @REM software distributed under the License is distributed on an
15
+ @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ @REM KIND, either express or implied. See the License for the
17
+ @REM specific language governing permissions and limitations
18
+ @REM under the License.
19
+ @REM ----------------------------------------------------------------------------
20
+
21
+ @REM ----------------------------------------------------------------------------
22
+ @REM Apache Maven Wrapper startup batch script, version 3.3.2
23
+ @REM
24
+ @REM Optional ENV vars
25
+ @REM MVNW_REPOURL - repo url base for downloading maven distribution
26
+ @REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
27
+ @REM MVNW_VERBOSE - true: enable verbose log; others: silence the output
28
+ @REM ----------------------------------------------------------------------------
29
+
30
+ @IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0)
31
+ @SET __MVNW_CMD__=
32
+ @SET __MVNW_ERROR__=
33
+ @SET __MVNW_PSMODULEP_SAVE=%PSModulePath%
34
+ @SET PSModulePath=
35
+ @FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @(
36
+ IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B)
37
+ )
38
+ @SET PSModulePath=%__MVNW_PSMODULEP_SAVE%
39
+ @SET __MVNW_PSMODULEP_SAVE=
40
+ @SET __MVNW_ARG0_NAME__=
41
+ @SET MVNW_USERNAME=
42
+ @SET MVNW_PASSWORD=
43
+ @IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*)
44
+ @echo Cannot start maven from wrapper >&2 && exit /b 1
45
+ @GOTO :EOF
46
+ : end batch / begin powershell #>
47
+
48
+ $ErrorActionPreference = "Stop"
49
+ if ($env:MVNW_VERBOSE -eq "true") {
50
+ $VerbosePreference = "Continue"
51
+ }
52
+
53
+ # calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties
54
+ $distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl
55
+ if (!$distributionUrl) {
56
+ Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
57
+ }
58
+
59
+ switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) {
60
+ "maven-mvnd-*" {
61
+ $USE_MVND = $true
62
+ $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip"
63
+ $MVN_CMD = "mvnd.cmd"
64
+ break
65
+ }
66
+ default {
67
+ $USE_MVND = $false
68
+ $MVN_CMD = $script -replace '^mvnw','mvn'
69
+ break
70
+ }
71
+ }
72
+
73
+ # apply MVNW_REPOURL and calculate MAVEN_HOME
74
+ # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
75
+ if ($env:MVNW_REPOURL) {
76
+ $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" }
77
+ $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')"
78
+ }
79
+ $distributionUrlName = $distributionUrl -replace '^.*/',''
80
+ $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$',''
81
+ $MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain"
82
+ if ($env:MAVEN_USER_HOME) {
83
+ $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain"
84
+ }
85
+ $MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
86
+ $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME"
87
+
88
+ if (Test-Path -Path "$MAVEN_HOME" -PathType Container) {
89
+ Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME"
90
+ Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
91
+ exit $?
92
+ }
93
+
94
+ if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) {
95
+ Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl"
96
+ }
97
+
98
+ # prepare tmp dir
99
+ $TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile
100
+ $TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir"
101
+ $TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null
102
+ trap {
103
+ if ($TMP_DOWNLOAD_DIR.Exists) {
104
+ try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
105
+ catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
106
+ }
107
+ }
108
+
109
+ New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null
110
+
111
+ # Download and Install Apache Maven
112
+ Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
113
+ Write-Verbose "Downloading from: $distributionUrl"
114
+ Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
115
+
116
+ $webclient = New-Object System.Net.WebClient
117
+ if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) {
118
+ $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD)
119
+ }
120
+ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
121
+ $webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null
122
+
123
+ # If specified, validate the SHA-256 sum of the Maven distribution zip file
124
+ $distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum
125
+ if ($distributionSha256Sum) {
126
+ if ($USE_MVND) {
127
+ Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties."
128
+ }
129
+ Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
130
+ if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) {
131
+ Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property."
132
+ }
133
+ }
134
+
135
+ # unzip and move
136
+ Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null
137
+ Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null
138
+ try {
139
+ Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null
140
+ } catch {
141
+ if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) {
142
+ Write-Error "fail to move MAVEN_HOME"
143
+ }
144
+ } finally {
145
+ try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
146
+ catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
147
+ }
148
+
149
+ Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
pom.xml ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
3
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+ <modelVersion>4.0.0</modelVersion>
6
+ <parent>
7
+ <groupId>org.springframework.boot</groupId>
8
+ <artifactId>spring-boot-starter-parent</artifactId>
9
+ <version>3.5.4</version>
10
+ <relativePath /> <!-- lookup parent from repository -->
11
+ </parent>
12
+ <groupId>com.example</groupId>
13
+ <artifactId>app</artifactId>
14
+ <version>0.0.1-SNAPSHOT</version>
15
+ <name>app</name>
16
+ <description>Demo project for Spring Boot</description>
17
+ <url />
18
+ <licenses>
19
+ <license />
20
+ </licenses>
21
+ <developers>
22
+ <developer />
23
+ </developers>
24
+ <scm>
25
+ <connection />
26
+ <developerConnection />
27
+ <tag />
28
+ <url />
29
+ </scm>
30
+ <properties>
31
+ <java.version>24</java.version>
32
+ <grpc.version>1.72.0</grpc.version>
33
+ <protobuf-java.version>4.30.2</protobuf-java.version>
34
+ <spring-grpc.version>0.9.0</spring-grpc.version>
35
+ <jjwt.version>0.12.6</jjwt.version>
36
+ </properties>
37
+ <dependencies>
38
+ <dependency>
39
+ <groupId>org.springframework.boot</groupId>
40
+ <artifactId>spring-boot-starter-data-jpa</artifactId>
41
+ </dependency>
42
+ <dependency>
43
+ <groupId>org.springframework.boot</groupId>
44
+ <artifactId>spring-boot-starter-oauth2-authorization-server</artifactId>
45
+ </dependency>
46
+ <dependency>
47
+ <groupId>org.springframework.boot</groupId>
48
+ <artifactId>spring-boot-starter-security</artifactId>
49
+ </dependency>
50
+ <dependency>
51
+ <groupId>org.springframework.boot</groupId>
52
+ <artifactId>spring-boot-starter-web</artifactId>
53
+ </dependency>
54
+ <dependency>
55
+ <groupId>org.springframework.security</groupId>
56
+ <artifactId>spring-security-crypto</artifactId>
57
+ </dependency>
58
+ <dependency>
59
+ <groupId>org.bouncycastle</groupId>
60
+ <artifactId>bcprov-jdk18on</artifactId>
61
+ <version>1.81</version>
62
+ </dependency>
63
+ <dependency>
64
+ <groupId>io.grpc</groupId>
65
+ <artifactId>grpc-services</artifactId>
66
+ </dependency>
67
+ <dependency>
68
+ <groupId>org.springframework.grpc</groupId>
69
+ <artifactId>spring-grpc-server-web-spring-boot-starter</artifactId>
70
+ </dependency>
71
+ <dependency>
72
+ <groupId>io.jsonwebtoken</groupId>
73
+ <artifactId>jjwt-api</artifactId>
74
+ <version>${jjwt.version}</version>
75
+ </dependency>
76
+ <dependency>
77
+ <groupId>io.jsonwebtoken</groupId>
78
+ <artifactId>jjwt-impl</artifactId>
79
+ <version>${jjwt.version}</version>
80
+ </dependency>
81
+ <dependency>
82
+ <groupId>io.jsonwebtoken</groupId>
83
+ <artifactId>jjwt-jackson</artifactId>
84
+ <version>${jjwt.version}</version>
85
+ </dependency>
86
+ <dependency>
87
+ <groupId>org.springdoc</groupId>
88
+ <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
89
+ <version>2.8.9</version>
90
+ </dependency>
91
+
92
+
93
+ <dependency>
94
+ <groupId>org.postgresql</groupId>
95
+ <artifactId>postgresql</artifactId>
96
+ <scope>runtime</scope>
97
+ </dependency>
98
+ <dependency>
99
+ <groupId>org.springframework.boot</groupId>
100
+ <artifactId>spring-boot-configuration-processor</artifactId>
101
+ <optional>true</optional>
102
+ </dependency>
103
+ <dependency>
104
+ <groupId>org.projectlombok</groupId>
105
+ <artifactId>lombok</artifactId>
106
+ <optional>true</optional>
107
+ </dependency>
108
+ <dependency>
109
+ <groupId>org.springframework.boot</groupId>
110
+ <artifactId>spring-boot-starter-test</artifactId>
111
+ <scope>test</scope>
112
+ </dependency>
113
+ <dependency>
114
+ <groupId>org.springframework.grpc</groupId>
115
+ <artifactId>spring-grpc-test</artifactId>
116
+ <scope>test</scope>
117
+ </dependency>
118
+ <dependency>
119
+ <groupId>org.springframework.security</groupId>
120
+ <artifactId>spring-security-test</artifactId>
121
+ <scope>test</scope>
122
+ </dependency>
123
+ </dependencies>
124
+ <dependencyManagement>
125
+ <dependencies>
126
+ <dependency>
127
+ <groupId>org.springframework.grpc</groupId>
128
+ <artifactId>spring-grpc-dependencies</artifactId>
129
+ <version>${spring-grpc.version}</version>
130
+ <type>pom</type>
131
+ <scope>import</scope>
132
+ </dependency>
133
+ </dependencies>
134
+ </dependencyManagement>
135
+
136
+ <build>
137
+ <plugins>
138
+ <plugin>
139
+ <groupId>org.apache.maven.plugins</groupId>
140
+ <artifactId>maven-compiler-plugin</artifactId>
141
+ <configuration>
142
+ <annotationProcessorPaths>
143
+ <path>
144
+ <groupId>org.projectlombok</groupId>
145
+ <artifactId>lombok</artifactId>
146
+ </path>
147
+ <path>
148
+ <groupId>org.springframework.boot</groupId>
149
+ <artifactId>spring-boot-configuration-processor</artifactId>
150
+ </path>
151
+ </annotationProcessorPaths>
152
+ </configuration>
153
+ </plugin>
154
+ <!-- <plugin>
155
+ <groupId>io.github.ascopes</groupId>
156
+ <artifactId>protobuf-maven-plugin</artifactId>
157
+ <version>3.4.2</version>
158
+ <configuration>
159
+ <protocVersion>${protobuf-java.version}</protocVersion>
160
+ <binaryMavenPlugins>
161
+ <binaryMavenPlugin>
162
+ <groupId>io.grpc</groupId>
163
+ <artifactId>protoc-gen-grpc-java</artifactId>
164
+ <version>${grpc.version}</version>
165
+ <options>@generated=omit</options>
166
+ </binaryMavenPlugin>
167
+ </binaryMavenPlugins>
168
+ </configuration>
169
+ <executions>
170
+ <execution>
171
+ <id>generate</id>
172
+ <goals>
173
+ <goal>generate</goal>
174
+ </goals>
175
+ </execution>
176
+ </executions>
177
+ </plugin> -->
178
+ <plugin>
179
+ <groupId>org.springframework.boot</groupId>
180
+ <artifactId>spring-boot-maven-plugin</artifactId>
181
+ <configuration>
182
+ <excludes>
183
+ <exclude>
184
+ <groupId>org.projectlombok</groupId>
185
+ <artifactId>lombok</artifactId>
186
+ </exclude>
187
+ </excludes>
188
+ </configuration>
189
+ </plugin>
190
+ </plugins>
191
+ </build>
192
+
193
+ </project>
src/main/java/com/example/app/AppApplication.java ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app;
2
+
3
+ import org.springframework.boot.SpringApplication;
4
+ import org.springframework.boot.autoconfigure.SpringBootApplication;
5
+
6
+ @SpringBootApplication
7
+ public class AppApplication {
8
+
9
+ public static void main(String[] args) {
10
+ SpringApplication.run(AppApplication.class, args);
11
+ }
12
+
13
+ }
src/main/java/com/example/app/configurations/DataSourceConfiguration.java ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.configurations;
2
+
3
+ import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
4
+ import org.springframework.boot.context.properties.ConfigurationProperties;
5
+ import org.springframework.context.annotation.Bean;
6
+ import org.springframework.context.annotation.Configuration;
7
+ import org.springframework.context.annotation.Primary;
8
+
9
+ @Configuration(proxyBeanMethods = false)
10
+ public class DataSourceConfiguration {
11
+ @Bean
12
+ @Primary
13
+ @ConfigurationProperties("app.datasource")
14
+ public DataSourceProperties dataSourceProperties() {
15
+ return new DataSourceProperties();
16
+ }
17
+ }
src/main/java/com/example/app/configurations/PasswordEncoderConfiguration.java ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.configurations;
2
+
3
+ import org.springframework.context.annotation.Bean;
4
+ import org.springframework.context.annotation.Configuration;
5
+ import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
6
+ import org.springframework.security.crypto.password.PasswordEncoder;
7
+
8
+ @Configuration
9
+ public class PasswordEncoderConfiguration {
10
+ @Bean
11
+ public static PasswordEncoder passwordEncoder() {
12
+ return Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8();
13
+ }
14
+ }
src/main/java/com/example/app/configurations/SecurityConfiguration.java ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.configurations;
2
+
3
+ import org.springframework.context.annotation.Bean;
4
+ import org.springframework.context.annotation.Configuration;
5
+ import org.springframework.security.authentication.AuthenticationManager;
6
+ import org.springframework.security.authentication.ProviderManager;
7
+ import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
8
+ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9
+ import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
10
+ import org.springframework.security.config.http.SessionCreationPolicy;
11
+ import org.springframework.security.core.userdetails.UserDetailsService;
12
+ import org.springframework.security.crypto.password.PasswordEncoder;
13
+ import org.springframework.security.web.SecurityFilterChain;
14
+ import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
15
+
16
+ import com.example.app.filters.JwtAuthFilter;
17
+
18
+ import lombok.RequiredArgsConstructor;
19
+
20
+ @Configuration
21
+ @EnableWebSecurity
22
+ @RequiredArgsConstructor
23
+ public class SecurityConfiguration {
24
+ private final JwtAuthFilter jwtAuthFilter;
25
+ private final UserDetailsService userDetailsService;
26
+ private final PasswordEncoder passwordEncoder;
27
+
28
+ @Bean
29
+ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
30
+ http
31
+ // Disable CSRF (not needed for stateless JWT)
32
+ .csrf(csrf -> csrf.disable())
33
+
34
+ // Configure endpoint authorization
35
+ .authorizeHttpRequests(auth -> auth
36
+ .requestMatchers("/users/auth", "/users", "/docs/**",
37
+ "/v3/api-docs")
38
+ .permitAll()
39
+
40
+ .requestMatchers("/auth/user/**").hasAuthority("ROLE_USER")
41
+ .requestMatchers("/auth/admin/**").hasAuthority("ROLE_ADMIN")
42
+
43
+ .anyRequest().authenticated())
44
+
45
+ // Stateless session (required for JWT)
46
+ .sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
47
+
48
+ // Add JWT filter before Spring Security's default filter
49
+ .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
50
+
51
+ return http.build();
52
+ }
53
+
54
+ /*
55
+ * Authentication manager bean
56
+ * Required for programmatic authentication (e.g., in /generateToken)
57
+ */
58
+ @Bean
59
+ public AuthenticationManager authenticationManager() throws Exception {
60
+ DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider(userDetailsService);
61
+ authenticationProvider.setPasswordEncoder(passwordEncoder);
62
+ return new ProviderManager(authenticationProvider);
63
+ }
64
+ }
src/main/java/com/example/app/controllers/UserAccountController.java ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.controllers;
2
+
3
+ import org.springframework.web.bind.annotation.DeleteMapping;
4
+ import org.springframework.web.bind.annotation.PathVariable;
5
+ import org.springframework.web.bind.annotation.PostMapping;
6
+ import org.springframework.web.bind.annotation.RequestBody;
7
+ import org.springframework.web.bind.annotation.RequestMapping;
8
+ import org.springframework.web.bind.annotation.RestController;
9
+
10
+ import com.example.app.dtos.UserAccountCreationDto;
11
+ import com.example.app.services.UserAccountService;
12
+
13
+ import lombok.RequiredArgsConstructor;
14
+
15
+ @RestController
16
+ @RequestMapping("/users")
17
+ @RequiredArgsConstructor
18
+ public class UserAccountController {
19
+ private final UserAccountService userAccountService;
20
+
21
+ @PostMapping
22
+ public Long save(@RequestBody UserAccountCreationDto dto) {
23
+ return userAccountService.save(dto).getId();
24
+ }
25
+
26
+ @DeleteMapping("/{id}")
27
+ public void deleteById(@PathVariable Long id) {
28
+ userAccountService.deleteById(id);
29
+ }
30
+ }
src/main/java/com/example/app/controllers/UserAuthController.java ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.controllers;
2
+
3
+ import org.springframework.beans.factory.annotation.Autowired;
4
+ import org.springframework.security.authentication.AuthenticationManager;
5
+ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
6
+ import org.springframework.security.core.Authentication;
7
+ import org.springframework.security.core.userdetails.UsernameNotFoundException;
8
+ import org.springframework.web.bind.annotation.PostMapping;
9
+ import org.springframework.web.bind.annotation.RequestBody;
10
+ import org.springframework.web.bind.annotation.RequestMapping;
11
+ import org.springframework.web.bind.annotation.RestController;
12
+
13
+ import com.example.app.dtos.JwtRequestDto;
14
+ import com.example.app.mappers.JwtRequestMapper;
15
+ import com.example.app.misc.JwtRequest;
16
+ import com.example.app.services.JwtService;
17
+
18
+ @RestController
19
+ @RequestMapping("/users/auth")
20
+ public class UserAuthController {
21
+ @Autowired
22
+ private JwtService jwtService;
23
+ @Autowired
24
+ private AuthenticationManager authenticationManager;
25
+ @Autowired
26
+ JwtRequestMapper mapper;
27
+
28
+ @PostMapping()
29
+ public String authenticateAndGetToken(@RequestBody JwtRequestDto dto) {
30
+ JwtRequest jwtRequest = mapper.fromDto(dto);
31
+ Authentication authentication = authenticationManager.authenticate(
32
+ new UsernamePasswordAuthenticationToken(jwtRequest.getUsername(), jwtRequest.getPassword()));
33
+ if (authentication.isAuthenticated()) {
34
+ return jwtService.generateToken(jwtRequest.getUsername());
35
+ }
36
+ throw new UsernameNotFoundException("Invalid user request");
37
+ }
38
+ }
src/main/java/com/example/app/dtos/JwtRequestDto.java ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.dtos;
2
+
3
+ import lombok.Getter;
4
+ import lombok.ToString;
5
+
6
+ @Getter
7
+ @ToString
8
+ public class JwtRequestDto {
9
+ private String email;
10
+ private String password;
11
+ }
src/main/java/com/example/app/dtos/UserAccountCreationDto.java ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.dtos;
2
+
3
+ import lombok.AllArgsConstructor;
4
+ import lombok.Builder;
5
+ import lombok.Getter;
6
+
7
+ @Builder
8
+ @Getter
9
+ @AllArgsConstructor
10
+ public class UserAccountCreationDto {
11
+ private final String email;
12
+ private final String password;
13
+ }
src/main/java/com/example/app/dtos/UserAccountDto.java ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.dtos;
2
+
3
+ import lombok.AllArgsConstructor;
4
+ import lombok.Builder;
5
+ import lombok.Getter;
6
+ import lombok.Setter;
7
+
8
+ @Builder
9
+ @Getter
10
+ @Setter
11
+ @AllArgsConstructor
12
+ public class UserAccountDto {
13
+ private String email;
14
+ }
src/main/java/com/example/app/entities/Role.java ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.entities;
2
+
3
+ import java.util.Set;
4
+
5
+ import jakarta.persistence.Column;
6
+ import jakarta.persistence.Entity;
7
+ import jakarta.persistence.GeneratedValue;
8
+ import jakarta.persistence.GenerationType;
9
+ import jakarta.persistence.Id;
10
+ import jakarta.persistence.ManyToMany;
11
+ import jakarta.persistence.Table;
12
+ import lombok.AllArgsConstructor;
13
+ import lombok.Builder;
14
+ import lombok.Getter;
15
+ import lombok.NoArgsConstructor;
16
+
17
+ @Entity
18
+ @Table(name = "roles")
19
+ @Builder
20
+ @Getter
21
+ @NoArgsConstructor
22
+ @AllArgsConstructor
23
+ public class Role {
24
+ @Id
25
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
26
+ private Long id;
27
+ @Column(name = "name", nullable = false, unique = true)
28
+ private String name;
29
+ @ManyToMany(mappedBy = "roles")
30
+ private Set<UserAccount> userAccounts;
31
+ }
src/main/java/com/example/app/entities/UserAccount.java ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.entities;
2
+
3
+ import java.util.Set;
4
+
5
+ import jakarta.persistence.Column;
6
+ import jakarta.persistence.Entity;
7
+ import jakarta.persistence.GeneratedValue;
8
+ import jakarta.persistence.GenerationType;
9
+ import jakarta.persistence.Id;
10
+ import jakarta.persistence.JoinColumn;
11
+ import jakarta.persistence.JoinTable;
12
+ import jakarta.persistence.ManyToMany;
13
+ import jakarta.persistence.Table;
14
+ import lombok.AllArgsConstructor;
15
+ import lombok.Builder;
16
+ import lombok.Getter;
17
+ import lombok.NoArgsConstructor;
18
+ import lombok.Setter;
19
+ import lombok.ToString;
20
+
21
+ @Table(name = "user_accounts")
22
+ @Entity
23
+ @Builder
24
+ @Getter
25
+ @Setter
26
+ @ToString
27
+ @NoArgsConstructor
28
+ @AllArgsConstructor
29
+ public class UserAccount {
30
+ @Id
31
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
32
+ private Long id;
33
+ @Column(name = "email", nullable = false, unique = true)
34
+ private String email;
35
+ @Column(name = "password_hash", nullable = false)
36
+ private String passwordHash;
37
+ @ManyToMany
38
+ @JoinTable(name = "user_account_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
39
+ private Set<Role> roles;
40
+ }
src/main/java/com/example/app/filters/JwtAuthFilter.java ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.filters;
2
+
3
+ import java.io.IOException;
4
+ import java.util.Optional;
5
+
6
+ import org.springframework.beans.factory.annotation.Autowired;
7
+ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
8
+ import org.springframework.security.core.context.SecurityContextHolder;
9
+ import org.springframework.security.core.userdetails.UserDetails;
10
+ import org.springframework.security.core.userdetails.UserDetailsService;
11
+ import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
12
+ import org.springframework.stereotype.Component;
13
+ import org.springframework.web.filter.OncePerRequestFilter;
14
+
15
+ import com.example.app.services.JwtService;
16
+
17
+ import jakarta.servlet.FilterChain;
18
+ import jakarta.servlet.ServletException;
19
+ import jakarta.servlet.http.HttpServletRequest;
20
+ import jakarta.servlet.http.HttpServletResponse;
21
+
22
+ @Component
23
+ public class JwtAuthFilter extends OncePerRequestFilter {
24
+ @Autowired
25
+ private UserDetailsService userDetailsService;
26
+ @Autowired
27
+ private JwtService jwtService;
28
+
29
+ @Override
30
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
31
+ throws IOException, ServletException {
32
+ final Optional<String> authHeader = Optional.ofNullable(request.getHeader("Authorization"));
33
+ Optional<String> token = authHeader
34
+ .filter(x -> x.startsWith("Bearer "))
35
+ .map(x -> x.substring(7));
36
+ Optional<String> username = token.flatMap(x -> jwtService.extractUsername(x));
37
+
38
+ if (username.isPresent()
39
+ && Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()).isEmpty()) {
40
+ UserDetails userDetails = userDetailsService.loadUserByUsername(username.get());
41
+ if (jwtService.validateToken(token.get(), userDetails)) {
42
+ UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(userDetails,
43
+ null, userDetails.getAuthorities());
44
+ authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
45
+ SecurityContextHolder.getContext().setAuthentication(authToken);
46
+ }
47
+ }
48
+
49
+ filterChain.doFilter(request, response);
50
+ }
51
+ }
src/main/java/com/example/app/mappers/JwtRequestMapper.java ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.mappers;
2
+
3
+ import org.springframework.stereotype.Component;
4
+
5
+ import com.example.app.dtos.JwtRequestDto;
6
+ import com.example.app.misc.JwtRequest;
7
+
8
+ @Component
9
+ public class JwtRequestMapper implements Mapper<JwtRequest, JwtRequestDto> {
10
+ @Override
11
+ public JwtRequest fromDto(JwtRequestDto dto) {
12
+ return JwtRequest.builder().username(dto.getEmail()).password(dto.getPassword()).build();
13
+ }
14
+
15
+ @Override
16
+ public JwtRequestDto toDto(JwtRequest entity) {
17
+ throw new UnsupportedOperationException("Unimplemented method 'toDto'");
18
+ }
19
+
20
+ }
src/main/java/com/example/app/mappers/Mapper.java ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ package com.example.app.mappers;
2
+
3
+ public interface Mapper<E, D> {
4
+ public E fromDto(D dto);
5
+
6
+ public D toDto(E entity);
7
+ }
src/main/java/com/example/app/mappers/UserAccountCreationMapper.java ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.mappers;
2
+
3
+ import org.springframework.stereotype.Component;
4
+
5
+ import com.example.app.dtos.UserAccountCreationDto;
6
+ import com.example.app.entities.UserAccount;
7
+
8
+ import lombok.RequiredArgsConstructor;
9
+
10
+ import org.springframework.security.crypto.password.PasswordEncoder;
11
+
12
+ @Component
13
+ @RequiredArgsConstructor
14
+ public class UserAccountCreationMapper implements Mapper<UserAccount, UserAccountCreationDto> {
15
+ private final PasswordEncoder passwordEncoder;
16
+
17
+ @Override
18
+ public UserAccount fromDto(UserAccountCreationDto dto) {
19
+ return UserAccount
20
+ .builder()
21
+ .id(null)
22
+ .email(dto.getEmail())
23
+ .passwordHash(passwordEncoder.encode(dto.getPassword()))
24
+ .build();
25
+ }
26
+
27
+ @Override
28
+ public UserAccountCreationDto toDto(UserAccount entity) {
29
+ throw new UnsupportedOperationException("Unimplemented method 'toDto'");
30
+ }
31
+
32
+ }
src/main/java/com/example/app/mappers/UserAccountMapper.java ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.mappers;
2
+
3
+ import org.springframework.stereotype.Component;
4
+
5
+ import com.example.app.dtos.UserAccountDto;
6
+ import com.example.app.entities.UserAccount;
7
+
8
+ @Component
9
+ public class UserAccountMapper implements Mapper<UserAccount, UserAccountDto> {
10
+
11
+ @Override
12
+ public UserAccount fromDto(UserAccountDto dto) {
13
+ throw new UnsupportedOperationException("Unimplemented method 'fromDto'");
14
+ }
15
+
16
+ @Override
17
+ public UserAccountDto toDto(UserAccount entity) {
18
+ return UserAccountDto.builder().email(entity.getEmail()).build();
19
+ }
20
+ }
src/main/java/com/example/app/misc/JwtRequest.java ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.misc;
2
+
3
+ import lombok.AllArgsConstructor;
4
+ import lombok.Builder;
5
+ import lombok.Getter;
6
+
7
+ @Builder
8
+ @Getter
9
+ @AllArgsConstructor
10
+ public class JwtRequest {
11
+ private String username;
12
+ private String password;
13
+ }
src/main/java/com/example/app/repositories/UserAccountRepository.java ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.repositories;
2
+
3
+ import java.util.Optional;
4
+
5
+ import org.springframework.data.repository.CrudRepository;
6
+
7
+ import com.example.app.entities.UserAccount;
8
+
9
+ public interface UserAccountRepository extends CrudRepository<UserAccount, Long> {
10
+ public Optional<UserAccount> findByEmail(String email);
11
+ }
src/main/java/com/example/app/services/JwtService.java ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.services;
2
+
3
+ import java.util.Date;
4
+ import java.util.HashMap;
5
+ import java.util.Map;
6
+ import java.util.Optional;
7
+ import java.util.function.Function;
8
+
9
+ import javax.crypto.SecretKey;
10
+
11
+ import org.springframework.beans.factory.annotation.Value;
12
+ import org.springframework.security.core.userdetails.UserDetails;
13
+ import org.springframework.stereotype.Service;
14
+
15
+ import io.jsonwebtoken.Claims;
16
+ import io.jsonwebtoken.Jwts;
17
+ import io.jsonwebtoken.io.Decoders;
18
+ import io.jsonwebtoken.security.Keys;
19
+ import lombok.Getter;
20
+
21
+ @Getter
22
+ @Service
23
+ public class JwtService {
24
+ @Value("${SECRET}")
25
+ private String SECRET;
26
+
27
+ public String generateToken(String username) {
28
+ Map<String, Object> claims = new HashMap<>();
29
+ return generateToken(claims, username);
30
+ }
31
+
32
+ private String generateToken(Map<String, Object> claims, String username) {
33
+ return Jwts
34
+ .builder()
35
+ .claims(claims)
36
+ .subject(username)
37
+ .issuedAt(new Date())
38
+ .expiration(new Date(System.currentTimeMillis() + 1000 * 60 * 30))
39
+ .signWith(getSignKey())
40
+ .compact();
41
+ }
42
+
43
+ private SecretKey getSignKey() {
44
+ byte[] keyBytes = Decoders.BASE64.decode(SECRET);
45
+ return Keys.hmacShaKeyFor(keyBytes);
46
+ }
47
+
48
+ public Optional<String> extractUsername(String token) {
49
+ return extractClaim(token, Claims::getSubject);
50
+ }
51
+
52
+ public Optional<Date> extractExpiration(String token) {
53
+ return extractClaim(token, Claims::getExpiration);
54
+ }
55
+
56
+ public <T> Optional<T> extractClaim(String token, Function<Claims, T> claimResolver) {
57
+ final Claims claims = extractAllClaims(token);
58
+ return Optional.ofNullable(claimResolver.apply(claims));
59
+ }
60
+
61
+ private Claims extractAllClaims(String token) {
62
+ return Jwts
63
+ .parser()
64
+ .verifyWith(getSignKey())
65
+ .build()
66
+ .parseSignedClaims(token)
67
+ .getPayload();
68
+ }
69
+
70
+ private boolean isTokenExpired(String token) {
71
+ return extractExpiration(token).map(x -> x.before(new Date())).orElse(false);
72
+ }
73
+
74
+ public boolean validateToken(String token, UserDetails user) {
75
+ return extractUsername(token).map(x -> x.equals(user.getUsername())).orElse(false) && !isTokenExpired(token);
76
+ }
77
+ }
src/main/java/com/example/app/services/UserAccountDetails.java ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.services;
2
+
3
+ import java.util.Collection;
4
+ import java.util.List;
5
+ import java.util.stream.Collectors;
6
+
7
+ import org.springframework.security.core.GrantedAuthority;
8
+ import org.springframework.security.core.authority.SimpleGrantedAuthority;
9
+ import org.springframework.security.core.userdetails.UserDetails;
10
+
11
+ import com.example.app.entities.Role;
12
+ import com.example.app.entities.UserAccount;
13
+
14
+ public class UserAccountDetails implements UserDetails {
15
+ private String email;
16
+ private String passwordHash;
17
+ private List<GrantedAuthority> authorities;
18
+
19
+ public UserAccountDetails(UserAccount userAccount) {
20
+ this.email = userAccount.getEmail();
21
+ this.passwordHash = userAccount.getPasswordHash();
22
+ this.authorities = userAccount.getRoles().stream().map(Role::getName).map(SimpleGrantedAuthority::new)
23
+ .collect(Collectors.toList());
24
+ }
25
+
26
+ @Override
27
+ public Collection<? extends GrantedAuthority> getAuthorities() {
28
+ return authorities;
29
+ }
30
+
31
+ @Override
32
+ public String getUsername() {
33
+ return email;
34
+ }
35
+
36
+ @Override
37
+ public String getPassword() {
38
+ return passwordHash;
39
+ }
40
+
41
+ @Override
42
+ public boolean isAccountNonExpired() {
43
+ return true;
44
+ }
45
+
46
+ @Override
47
+ public boolean isAccountNonLocked() {
48
+ return true;
49
+ }
50
+
51
+ @Override
52
+ public boolean isCredentialsNonExpired() {
53
+ return true;
54
+ }
55
+
56
+ @Override
57
+ public boolean isEnabled() {
58
+ return true;
59
+ }
60
+ }
src/main/java/com/example/app/services/UserAccountService.java ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app.services;
2
+
3
+ import org.springframework.beans.factory.annotation.Autowired;
4
+ import org.springframework.security.core.userdetails.UserDetails;
5
+ import org.springframework.security.core.userdetails.UserDetailsService;
6
+ import org.springframework.security.core.userdetails.UsernameNotFoundException;
7
+ import org.springframework.stereotype.Service;
8
+
9
+ import com.example.app.dtos.UserAccountCreationDto;
10
+ import com.example.app.entities.UserAccount;
11
+ import com.example.app.mappers.UserAccountCreationMapper;
12
+ import com.example.app.repositories.UserAccountRepository;
13
+
14
+ @Service
15
+ public class UserAccountService implements UserDetailsService {
16
+ @Autowired
17
+ private UserAccountRepository repository;
18
+ @Autowired
19
+ private UserAccountCreationMapper mapper;
20
+
21
+ public UserAccount save(UserAccountCreationDto dto) {
22
+ final UserAccount user = mapper.fromDto(dto);
23
+ return repository.save(user);
24
+ }
25
+
26
+ public void deleteById(Long id) {
27
+ repository.deleteById(id);
28
+ }
29
+
30
+ @Override
31
+ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
32
+ return repository.findByEmail(username).map(UserAccountDetails::new).orElseThrow(
33
+ () -> new UsernameNotFoundException(String.format("User of `email=%s` not found", username)));
34
+ }
35
+
36
+ }
src/main/resources/application.properties ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ spring.application.name=app
2
+ spring.jpa.hibernate.ddl-auto=update
3
+ springdoc.swagger-ui.path=/docs/swagger-ui.html
4
+ springdoc.api-docs.path=/docs/api-docs
5
+ app.datasource.url=${DATASOURCE_URL}
src/test/java/com/example/app/AppApplicationTests.java ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.app;
2
+
3
+ import org.junit.jupiter.api.Test;
4
+ import org.springframework.boot.test.context.SpringBootTest;
5
+
6
+ @SpringBootTest
7
+ class AppApplicationTests {
8
+
9
+ @Test
10
+ void contextLoads() {
11
+ }
12
+
13
+ }