Skip to content

Commit 420737f

Browse files
committed
Add Kotlin WebFlux sample
Issue: gh-5558
1 parent 8cf4ada commit 420737f

File tree

10 files changed

+301
-0
lines changed

10 files changed

+301
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
3+
plugins {
4+
id("io.spring.convention.spring-sample-boot")
5+
kotlin("jvm")
6+
kotlin("plugin.spring") version "1.3.71"
7+
}
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
dependencies {
14+
implementation(project(":spring-security-core"))
15+
implementation(project(":spring-security-config"))
16+
implementation(project(":spring-security-web"))
17+
implementation("org.springframework.boot:spring-boot-starter-webflux")
18+
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
19+
implementation("org.thymeleaf.extras:thymeleaf-extras-springsecurity5")
20+
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
21+
implementation("org.jetbrains.kotlin:kotlin-reflect")
22+
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
23+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
24+
25+
testImplementation(project(":spring-security-test"))
26+
testImplementation("org.springframework.boot:spring-boot-starter-test") {
27+
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
28+
}
29+
testImplementation("io.projectreactor:reactor-test")
30+
}
31+
32+
tasks.withType<Test> {
33+
useJUnitPlatform()
34+
}
35+
36+
tasks.withType<KotlinCompile> {
37+
kotlinOptions {
38+
freeCompilerArgs = listOf("-Xjsr305=strict")
39+
jvmTarget = "1.8"
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2002-2020 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+
* https://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+
17+
package org.springframework.security.samples
18+
19+
import org.springframework.boot.autoconfigure.SpringBootApplication
20+
import org.springframework.boot.runApplication
21+
22+
@SpringBootApplication
23+
class KotlinWebfluxApplication
24+
25+
fun main(args: Array<String>) {
26+
runApplication<KotlinWebfluxApplication>(*args)
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2020 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+
* https://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+
17+
package org.springframework.security.samples.config
18+
19+
import org.springframework.context.annotation.Bean
20+
import org.springframework.security.config.Customizer
21+
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
22+
import org.springframework.security.config.web.server.ServerHttpSecurity
23+
import org.springframework.security.config.web.server.invoke
24+
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService
25+
import org.springframework.security.core.userdetails.ReactiveUserDetailsService
26+
import org.springframework.security.core.userdetails.User
27+
import org.springframework.security.web.server.SecurityWebFilterChain
28+
29+
@EnableWebFluxSecurity
30+
class SecurityConfig {
31+
32+
@Bean
33+
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
34+
return http {
35+
authorizeExchange {
36+
authorize("/log-in", permitAll)
37+
authorize("/", permitAll)
38+
authorize("/css/**", permitAll)
39+
authorize("/user/**", hasAuthority("ROLE_USER"))
40+
}
41+
formLogin {
42+
loginPage = "/log-in"
43+
}
44+
}
45+
}
46+
47+
@Bean
48+
fun userDetailsService(): ReactiveUserDetailsService {
49+
val userDetails = User.withDefaultPasswordEncoder()
50+
.username("user")
51+
.password("password")
52+
.roles("USER")
53+
.build()
54+
return MapReactiveUserDetailsService(userDetails)
55+
}
56+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2002-2020 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+
* https://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+
17+
package org.springframework.security.samples.web
18+
19+
import org.springframework.stereotype.Controller
20+
import org.springframework.web.bind.annotation.GetMapping
21+
22+
@Controller
23+
class MainController {
24+
25+
@GetMapping("/")
26+
fun index(): String {
27+
return "index"
28+
}
29+
30+
@GetMapping("/user/index")
31+
fun userIndex(): String {
32+
return "user/index"
33+
}
34+
35+
@GetMapping("/log-in")
36+
fun login(): String {
37+
return "login"
38+
}
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
server:
2+
port: 8080
3+
4+
spring:
5+
thymeleaf:
6+
cache: false
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
font-family: sans;
3+
font-size: 1em;
4+
}
5+
6+
div.logout {
7+
float: right;
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
3+
<head>
4+
<title>Hello Spring Security</title>
5+
<meta charset="utf-8" />
6+
<link rel="stylesheet" href="/static/css/main.css" th:href="@{/css/main.css}" />
7+
</head>
8+
<body>
9+
<div th:fragment="logout" class="logout" sec:authorize="isAuthenticated()">
10+
Logged in user: <span sec:authentication="name"></span> |
11+
Roles: <span sec:authentication="principal.authorities"></span>
12+
<div>
13+
<form action="#" th:action="@{/logout}" method="post">
14+
<input type="submit" value="Logout" />
15+
</form>
16+
</div>
17+
</div>
18+
<h1>Hello Spring Security</h1>
19+
<p>This is an unsecured page, but you can access the secured pages after authenticating.</p>
20+
<ul>
21+
<li>Go to the <a href="/user/index" th:href="@{/user/index}">secured pages</a></li>
22+
</ul>
23+
</body>
24+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
3+
<head>
4+
<title>Login page</title>
5+
<meta charset="utf-8" />
6+
<link rel="stylesheet" href="/static/css/main.css" th:href="@{/css/main.css}" />
7+
</head>
8+
<body>
9+
<h1>Login page</h1>
10+
<p>Example user: user / password</p>
11+
<form th:action="@{/log-in}" method="post">
12+
<label for="username">Username</label>:
13+
<input type="text" id="username" name="username" autofocus="autofocus" /> <br />
14+
<label for="password">Password</label>:
15+
<input type="password" id="password" name="password" /> <br />
16+
<input type="submit" value="Log in" />
17+
</form>
18+
<p><a href="/" th:href="@{/}">Back to home page</a></p>
19+
</body>
20+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
3+
<head>
4+
<title>Hello Spring Security</title>
5+
<meta charset="utf-8" />
6+
<link rel="stylesheet" href="/static/css/main.css" th:href="@{/css/main.css}" />
7+
</head>
8+
<body>
9+
<div th:substituteby="index::logout"></div>
10+
<h1>This is a secured page!</h1>
11+
<p><a href="/" th:href="@{/}">Back to home page</a></p>
12+
</body>
13+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2002-2020 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+
* https://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+
17+
package org.springframework.security.samples
18+
19+
import org.junit.jupiter.api.Test
20+
import org.springframework.beans.factory.annotation.Autowired
21+
import org.springframework.boot.test.context.SpringBootTest
22+
import org.springframework.context.ApplicationContext
23+
import org.springframework.test.web.reactive.server.WebTestClient
24+
import org.springframework.security.test.context.support.WithMockUser
25+
26+
@SpringBootTest
27+
class KotlinWebfluxApplicationTests {
28+
29+
lateinit var rest: WebTestClient
30+
31+
@Autowired
32+
fun setup(context: ApplicationContext) {
33+
rest = WebTestClient
34+
.bindToApplicationContext(context)
35+
.configureClient()
36+
.build()
37+
}
38+
39+
@Test
40+
fun `index page is not protected`() {
41+
rest
42+
.get()
43+
.uri("/")
44+
.exchange()
45+
.expectStatus().isOk
46+
}
47+
48+
@Test
49+
fun `protected page when unauthenticated then redirects to login `() {
50+
rest
51+
.get()
52+
.uri("/user/index")
53+
.exchange()
54+
.expectStatus().is3xxRedirection
55+
.expectHeader().valueEquals("Location", "/log-in")
56+
}
57+
58+
@Test
59+
@WithMockUser
60+
fun `protected page can be accessed when authenticated`() {
61+
rest
62+
.get()
63+
.uri("/user/index")
64+
.exchange()
65+
.expectStatus().isOk
66+
}
67+
}

0 commit comments

Comments
 (0)