Skip to content

Commit f012ae4

Browse files
authored
[KTL-767] Spring Security JWT with Kotlin (#1116)
* feat: kotlin spring boot jwt * fix: pom.xml * fix: indent * fix: remove * import
1 parent df2da47 commit f012ae4

File tree

19 files changed

+720
-0
lines changed

19 files changed

+720
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@
544544
<module>spring-mvc-kotlin</module>
545545
<module>spring-reactive-kotlin</module>
546546
<module>spring-security-kotlin-dsl</module>
547+
<module>spring-security-kotlin</module>
547548
</modules>
548549
</profile>
549550

spring-security-kotlin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Relevant Articles:

spring-security-kotlin/pom.xml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>spring-security-kotlin</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-boot-3</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
<relativePath>../parent-boot-3</relativePath>
14+
</parent>
15+
16+
<properties>
17+
<jwt.version>0.11.5</jwt.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-security</artifactId>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.jetbrains.kotlin</groupId>
33+
<artifactId>kotlin-reflect</artifactId>
34+
<version>${kotlin.version}</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.jetbrains.kotlin</groupId>
39+
<artifactId>kotlin-stdlib</artifactId>
40+
<version>${kotlin.version}</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.fasterxml.jackson.module</groupId>
45+
<artifactId>jackson-module-kotlin</artifactId>
46+
</dependency>
47+
48+
<!-- JJWT API -->
49+
<dependency>
50+
<groupId>io.jsonwebtoken</groupId>
51+
<artifactId>jjwt-api</artifactId>
52+
<version>${jwt.version}</version>
53+
</dependency>
54+
55+
<!-- JJWT Implementation -->
56+
<dependency>
57+
<groupId>io.jsonwebtoken</groupId>
58+
<artifactId>jjwt-impl</artifactId>
59+
<version>${jwt.version}</version>
60+
<scope>runtime</scope>
61+
</dependency>
62+
63+
<!-- JJWT Jackson Serializer -->
64+
<dependency>
65+
<groupId>io.jsonwebtoken</groupId>
66+
<artifactId>jjwt-jackson</artifactId>
67+
<version>${jwt.version}</version>
68+
<scope>runtime</scope>
69+
</dependency>
70+
71+
<!-- Test -->
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-starter-test</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>org.springframework.security</groupId>
80+
<artifactId>spring-security-test</artifactId>
81+
</dependency>
82+
</dependencies>
83+
84+
<build>
85+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
86+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
87+
<plugins>
88+
<plugin>
89+
<groupId>org.springframework.boot</groupId>
90+
<artifactId>spring-boot-maven-plugin</artifactId>
91+
</plugin>
92+
<plugin>
93+
<groupId>org.jetbrains.kotlin</groupId>
94+
<artifactId>kotlin-maven-plugin</artifactId>
95+
<version>${kotlin.version}</version>
96+
<configuration>
97+
<args>
98+
<arg>-Xjsr305=strict</arg>
99+
</args>
100+
<compilerPlugins>
101+
<plugin>spring</plugin>
102+
</compilerPlugins>
103+
</configuration>
104+
<dependencies>
105+
<dependency>
106+
<groupId>org.jetbrains.kotlin</groupId>
107+
<artifactId>kotlin-maven-allopen</artifactId>
108+
<version>${kotlin.version}</version>
109+
</dependency>
110+
</dependencies>
111+
</plugin>
112+
</plugins>
113+
</build>
114+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.security.jwt
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
6+
7+
@EnableWebSecurity
8+
@SpringBootApplication
9+
class JwtApplication
10+
11+
fun main(args: Array<String>) {
12+
runApplication<JwtApplication>(*args)
13+
}
14+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.security.jwt.controller
2+
3+
import com.baeldung.security.jwt.domain.AuthenticationRequest
4+
import com.baeldung.security.jwt.domain.AuthenticationResponse
5+
import com.baeldung.security.jwt.domain.RefreshTokenRequest
6+
import com.baeldung.security.jwt.domain.TokenResponse
7+
import com.baeldung.security.jwt.service.AuthenticationService
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+
@RestController
14+
@RequestMapping("/api/auth")
15+
class AuthController(
16+
private val authenticationService: AuthenticationService
17+
) {
18+
@PostMapping
19+
fun authenticate(
20+
@RequestBody authRequest: AuthenticationRequest
21+
): AuthenticationResponse =
22+
authenticationService.authentication(authRequest)
23+
24+
@PostMapping("/refresh")
25+
fun refreshAccessToken(
26+
@RequestBody request: RefreshTokenRequest
27+
): TokenResponse = TokenResponse(token = authenticationService.refreshAccessToken(request.token))
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.security.jwt.controller
2+
3+
import org.springframework.http.ResponseEntity
4+
import org.springframework.web.bind.annotation.GetMapping
5+
import org.springframework.web.bind.annotation.RequestMapping
6+
import org.springframework.web.bind.annotation.RestController
7+
8+
@RestController
9+
@RequestMapping("/api")
10+
class HelloController {
11+
@GetMapping("/hello")
12+
fun hello(): ResponseEntity<String> {
13+
return ResponseEntity.ok("Hello, Authorized User!")
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.security.jwt.controller
2+
3+
import io.jsonwebtoken.ExpiredJwtException
4+
import io.jsonwebtoken.security.SignatureException
5+
import org.springframework.http.HttpStatus
6+
import org.springframework.http.ResponseEntity
7+
import org.springframework.security.core.AuthenticationException
8+
import org.springframework.web.bind.annotation.ControllerAdvice
9+
import org.springframework.web.bind.annotation.ExceptionHandler
10+
11+
@ControllerAdvice
12+
class JwtControllerAdvice {
13+
@ExceptionHandler(value = [ExpiredJwtException::class, AuthenticationException::class, SignatureException::class])
14+
fun handleAuthenticationExceptions(ex: RuntimeException): ResponseEntity<String> {
15+
return ResponseEntity
16+
.status(HttpStatus.UNAUTHORIZED)
17+
.body("Authentication failed: ${ex.message}")
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.security.jwt.domain
2+
3+
data class AuthenticationRequest(
4+
val username: String,
5+
val password: String,
6+
)
7+
8+
data class AuthenticationResponse(
9+
val accessToken: String,
10+
val refreshToken: String,
11+
)
12+
13+
data class RefreshTokenRequest(
14+
val token: String
15+
)
16+
17+
data class TokenResponse(
18+
val token: String
19+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.security.jwt.domain
2+
3+
import java.util.UUID
4+
5+
data class User(
6+
val id: UUID,
7+
val name: String,
8+
val password: String,
9+
val role: Role
10+
)
11+
12+
enum class Role {
13+
USER, ADMIN
14+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.security.jwt.filter
2+
3+
import com.baeldung.security.jwt.service.TokenService
4+
import jakarta.servlet.FilterChain
5+
import jakarta.servlet.http.HttpServletRequest
6+
import jakarta.servlet.http.HttpServletResponse
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+
@Component
16+
class JwtAuthorizationFilter(
17+
private val userDetailsService: UserDetailsService,
18+
private val tokenService: TokenService
19+
) : OncePerRequestFilter() {
20+
override fun doFilterInternal(
21+
request: HttpServletRequest,
22+
response: HttpServletResponse,
23+
filterChain: FilterChain
24+
) {
25+
val authorizationHeader: String? = request.getHeader("Authorization")
26+
27+
if (null != authorizationHeader && authorizationHeader.startsWith("Bearer ")) {
28+
try {
29+
val token: String = authorizationHeader.substringAfter("Bearer ")
30+
val username: String = tokenService.extractUsername(token)
31+
32+
if (SecurityContextHolder.getContext().authentication == null) {
33+
val userDetails: UserDetails = userDetailsService.loadUserByUsername(username)
34+
35+
if (username == userDetails.username) {
36+
val authToken = UsernamePasswordAuthenticationToken(userDetails, null, userDetails.authorities)
37+
authToken.details = WebAuthenticationDetailsSource().buildDetails(request)
38+
SecurityContextHolder.getContext().authentication = authToken
39+
}
40+
}
41+
} catch (ex: Exception) {
42+
response.writer.write("""{"error": "Filter Authorization error: ${ex.message ?: "unknown error"}"}""")
43+
}
44+
}
45+
46+
filterChain.doFilter(request, response)
47+
}
48+
}

0 commit comments

Comments
 (0)