Skip to content

Commit 796e423

Browse files
add sample-secured router
1 parent 08d8fe2 commit 796e423

File tree

9 files changed

+94
-12
lines changed

9 files changed

+94
-12
lines changed

http/user-app.http

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,33 @@ Authorization: Bearer {{oauthToken}}
4141
client.global.set("permissionId", null)
4242
%}
4343

44-
### todo: move
44+
### external
4545

4646
###
4747
GET {{host}}/external/{{$random.integer(0, 1000)}}
4848
Authorization: Bearer {{oauthToken}}
4949

50+
### sample-secured
51+
52+
###
53+
GET {{host}}/sample-secured/{{$random.integer(0, 1000)}}
54+
Authorization: Bearer {{oauthToken}}
55+
5056
###
51-
GET {{host}}/error?message=foo
57+
POST {{host}}/sample-secured
58+
Content-Type: application/json
5259
Authorization: Bearer {{oauthToken}}
60+
61+
{"data": "sample data: {{$uuid}}"}
62+
63+
###
64+
PUT {{host}}/sample-secured/{{$random.integer(0, 1000)}}
65+
Content-Type: application/json
66+
Authorization: Bearer {{oauthToken}}
67+
68+
{"data": "sample data: {{$uuid}} change"}
69+
70+
###
71+
DELETE {{host}}/sample-secured/{{$random.integer(0, 1000)}}
72+
Authorization: Bearer {{oauthToken}}
73+

src/main/kotlin/com/softeno/template/app/config/security/SecurityConfig.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ class SecurityConfig {
100100
"/v3/api-docs/**",
101101
)
102102
.permitAll()
103-
.pathMatchers("/permissions/**", "/ws/**", "/graphql/**", "/external/**").hasAuthority("ROLE_ADMIN")
104-
// .pathMatchers("/sample-secured/**").authenticated()
103+
.pathMatchers("/permissions/**", "/external/**").hasAuthority("ROLE_ADMIN")
104+
.pathMatchers("/sample-secured/**").authenticated()
105105
}
106106
.oauth2ResourceServer { rss ->
107107
rss.jwt { jwtDecoder(issuer, jwkSetUri) }
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.softeno.template.sample.http.external.api
1+
package com.softeno.template.sample.http.api
22

3-
import com.softeno.template.sample.http.external.config.ExternalClientConfig
3+
import com.softeno.template.sample.http.config.ExternalClientConfig
44
import kotlinx.coroutines.reactor.awaitSingle
55
import org.apache.commons.logging.LogFactory
66
import org.springframework.beans.factory.annotation.Qualifier
@@ -14,8 +14,6 @@ import org.springframework.web.reactive.function.client.WebClient
1414
import reactor.core.publisher.Mono
1515
import java.time.Duration
1616

17-
data class SampleResponseDto(val data: String)
18-
1917
@RestController
2018
@RequestMapping("/external")
2119
@Validated
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.softeno.template.sample.http.api
2+
3+
import org.apache.commons.logging.LogFactory
4+
import org.springframework.context.annotation.Bean
5+
import org.springframework.context.annotation.Configuration
6+
import org.springframework.stereotype.Service
7+
import org.springframework.web.reactive.function.server.RequestPredicates.*
8+
import org.springframework.web.reactive.function.server.RouterFunction
9+
import org.springframework.web.reactive.function.server.RouterFunctions.route
10+
import org.springframework.web.reactive.function.server.ServerResponse
11+
import org.springframework.web.reactive.function.server.ServerResponse.ok
12+
import org.springframework.web.reactive.function.server.body
13+
import reactor.core.publisher.Mono
14+
15+
@Configuration
16+
class SampleRouter {
17+
// OAuth2 secured sample resource
18+
@Bean
19+
fun routes(service: SampleService): RouterFunction<ServerResponse> {
20+
return route(GET("/sample-secured/{id}")) { req ->
21+
ok().body(service.getHandler(req.pathVariable("id")))
22+
}.andRoute(POST("/sample-secured")) { req ->
23+
req.bodyToMono(SampleResponseDto::class.java).map {
24+
service.postHandler(it)
25+
}.flatMap { ok().body(it) }
26+
}.andRoute(PUT("/sample-secured/{id}")) { req ->
27+
req.bodyToMono(SampleResponseDto::class.java).map {
28+
service.putHandler(req.pathVariable("id"), it)
29+
}.flatMap { ok().bodyValue(it) }
30+
}.andRoute(DELETE("/sample-secured/{id}")) { req ->
31+
ok().body(service.deleteHandler(req.pathVariable("id")))
32+
}
33+
}
34+
}
35+
36+
@Service
37+
class SampleService {
38+
private val log = LogFactory.getLog(javaClass)
39+
40+
fun postHandler(request: SampleResponseDto): Mono<SampleResponseDto> {
41+
log.info("[sample-service]: POST request: $request")
42+
return Mono.just(request)
43+
}
44+
45+
fun getHandler(id: String): Mono<SampleResponseDto> {
46+
log.info("[sample-service]: GET id: $id")
47+
return Mono.just(SampleResponseDto(data = id))
48+
}
49+
50+
fun putHandler(id: String, request: SampleResponseDto): Mono<SampleResponseDto> {
51+
log.info("[sample-service]: PUT id: $id, request: $request")
52+
return Mono.just(request)
53+
}
54+
55+
fun deleteHandler(id: String): Mono<Void> {
56+
log.info("[sample-service]: GET id: $id")
57+
return Mono.empty()
58+
}
59+
60+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.softeno.template.sample.http.api
2+
3+
data class SampleResponseDto(val data: String)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.softeno.template.sample.http.external.config
1+
package com.softeno.template.sample.http.config
22

33
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig
44
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.softeno.template.sample.http.external.config
1+
package com.softeno.template.sample.http.config
22

33
import org.springframework.boot.context.properties.ConfigurationProperties
44
import org.springframework.context.annotation.Bean

src/test/kotlin/com/softeno/template/app/IntegrationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.softeno.template.app.permission.PermissionFixture
1111
import com.softeno.template.app.permission.PermissionFixture.Companion.aPermission
1212
import com.softeno.template.app.permission.PermissionFixture.Companion.aPermissionDto
1313
import com.softeno.template.app.permission.db.PermissionRepository
14-
import com.softeno.template.sample.http.external.api.SampleResponseDto
14+
import com.softeno.template.sample.http.api.SampleResponseDto
1515
import io.mockk.every
1616
import kotlinx.coroutines.flow.count
1717
import kotlinx.coroutines.flow.first
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.softeno.template.sample.http.external.config
1+
package com.softeno.template.sample.http.config
22

33
import org.springframework.context.annotation.Bean
44
import org.springframework.context.annotation.Configuration

0 commit comments

Comments
 (0)