Skip to content

Commit

Permalink
update spring boot and java (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunohenriquepj authored Jul 1, 2022
1 parent 0527e63 commit a3da132
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 114 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ jobs:

- name: Validate Gradle Wrapper
uses: gradle/[email protected]

thundra:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'adopt'
- name: Thundra Gradle Test Instrumentation
uses: thundra-io/[email protected]
Expand Down Expand Up @@ -60,10 +60,10 @@ jobs:
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
restore-keys: ${{ runner.os }}-gradle-wrapper-

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'adopt'

- name: Build with Gradle
Expand Down Expand Up @@ -100,10 +100,10 @@ jobs:
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
restore-keys: ${{ runner.os }}-gradle-wrapper-

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'adopt'

- name: Test
Expand Down Expand Up @@ -145,10 +145,10 @@ jobs:
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
restore-keys: ${{ runner.os }}-gradle-wrapper-

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'adopt'

- name: Test
Expand Down Expand Up @@ -224,10 +224,10 @@ jobs:
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
restore-keys: ${{ runner.os }}-gradle-wrapper-

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'adopt'

- name: Download artifact
Expand Down
3 changes: 2 additions & 1 deletion .sdkmanrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
java=11.0.11.hs-adpt
java=17.0.2-open
kotlin=1.6.21
40 changes: 20 additions & 20 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.springframework.boot") version "2.6.4"
id("org.springframework.boot") version "2.7.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.6.10"
kotlin("plugin.spring") version "1.6.10"
kotlin("plugin.jpa") version "1.6.10"
kotlin("jvm") version "1.6.21"
kotlin("plugin.spring") version "1.6.21"
kotlin("plugin.jpa") version "1.6.21"
jacoco
id("com.adarshr.test-logger") version "3.1.0"
id("com.adarshr.test-logger") version "3.2.0"
}


group = "com.example"
version = "0.0.0"
java.sourceCompatibility = JavaVersion.VERSION_11
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
mavenCentral()
Expand All @@ -27,26 +27,26 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("io.springfox:springfox-boot-starter:3.0.0")
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("org.springdoc:springdoc-openapi-ui:1.6.9")
implementation("org.springdoc:springdoc-openapi-kotlin:1.6.9")
implementation("io.jsonwebtoken:jjwt:0.9.1")
runtimeOnly("mysql:mysql-connector-java")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.mockk:mockk:1.12.2")
testImplementation("com.github.javafaker:javafaker:1.0.2")
testImplementation("io.kotest:kotest-assertions-core:4.6.3")
testImplementation("org.testcontainers:testcontainers:1.16.2")
testImplementation(platform("org.testcontainers:testcontainers-bom:1.16.2"))
testImplementation("org.testcontainers:junit-jupiter:1.16.2")
testImplementation("io.mockk:mockk:1.12.4")
testImplementation("net.datafaker:datafaker:1.4.0")
testImplementation("io.kotest:kotest-assertions-core:5.3.1")
testImplementation(platform("org.testcontainers:testcontainers-bom:1.17.2"))
testImplementation("org.testcontainers:testcontainers")
testImplementation("org.testcontainers:junit-jupiter")
testImplementation("org.testcontainers:mysql")
testRuntimeOnly("mysql:mysql-connector-java")
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
jvmTarget = "17"
}
}

Expand All @@ -71,9 +71,9 @@ tasks.jacocoTestReport {

tasks.jacocoTestReport {
reports {
csv.isEnabled = false
xml.isEnabled = true
html.isEnabled = true
csv.required.set(false)
xml.required.set(true)
html.required.set(true)
}
}

Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3.7"

services:
mysql:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: spring_boot_kotlin_realworld
ports:
- "3307:3306"
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.realworld.configuration

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.fasterxml.jackson.databind.PropertyNamingStrategies
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

Expand All @@ -10,7 +10,7 @@ class JsonConfiguration {
@Bean
fun objectMapper(): ObjectMapper {
return ObjectMapper().apply {
propertyNamingStrategy = PropertyNamingStrategy.LOWER_CASE
propertyNamingStrategy = PropertyNamingStrategies.LOWER_CASE
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,69 +1,76 @@
package com.example.realworld.configuration

import com.example.realworld.filter.AuthorizationFilter
import com.example.realworld.filter.AuthenticationFilter
import com.example.realworld.handler.AuthenticationEntryPointHandler
import com.example.realworld.repository.UserRepository
import com.example.realworld.service.SecurityContextService
import com.example.realworld.service.UserDetailsService
import com.example.realworld.util.TokenUtil
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpMethod
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.builders.WebSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer
import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.SecurityFilterChain


@EnableWebSecurity
@Configuration
class SecurityConfiguration(
private val userDetailsService: UserDetailsService,
private val userRepository: UserRepository,
private val tokenUtil: TokenUtil,
private val securityContextService: SecurityContextService
) : WebSecurityConfigurerAdapter() {
) {
@Bean
fun passwordEncoder(): PasswordEncoder {
return BCryptPasswordEncoder()
}

@Bean
override fun authenticationManager(): AuthenticationManager {
return super.authenticationManager()
}

override fun configure(authenticationManager: AuthenticationManagerBuilder) {
authenticationManager.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder())
fun authenticationManager(authenticationConfiguration: AuthenticationConfiguration): AuthenticationManager {
return authenticationConfiguration.authenticationManager
}

override fun configure(http: HttpSecurity) {
http.authorizeRequests()
.antMatchers(HttpMethod.GET, "/").permitAll()
.antMatchers(HttpMethod.GET, "/actuator/**").permitAll()
.antMatchers(HttpMethod.POST, "/api/users").permitAll()
.antMatchers(HttpMethod.POST, "/api/users/login").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(AuthenticationEntryPointHandler())
.and()
.addFilter(AuthorizationFilter(authenticationManager(), tokenUtil, userRepository, securityContextService))
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.cors()
@Bean
fun filterChain(http: HttpSecurity, authenticationManager: AuthenticationManager): SecurityFilterChain {
return http
.authorizeHttpRequests {
it.antMatchers(HttpMethod.GET, "/").permitAll()
.antMatchers(HttpMethod.GET, "/actuator/**").permitAll()
.antMatchers(HttpMethod.POST, "/api/users").permitAll()
.antMatchers(HttpMethod.POST, "/api/users/login").permitAll()
.anyRequest().authenticated()
}
.csrf { it.disable() }
.exceptionHandling { it.authenticationEntryPoint(AuthenticationEntryPointHandler()) }
.addFilter(
AuthenticationFilter(
authenticationManager,
tokenUtil,
userRepository,
securityContextService
)
)
.sessionManagement { it.sessionCreationPolicy(SessionCreationPolicy.STATELESS) }
.cors {}
.build()
}

override fun configure(web: WebSecurity) {
web.ignoring().antMatchers(
"/v2/api-docs",
"/swagger-resources/**",
"/swagger-ui/**",
"/h2-console/**"
)
@Bean
fun webSecurityCustomizer(): WebSecurityCustomizer {
return WebSecurityCustomizer { web: WebSecurity ->
web.ignoring().antMatchers(
"/v3/api-docs/**",
"/swagger-ui/**",
"/swagger-ui.html",
"/h2-console/**"
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
package com.example.realworld.configuration

import io.swagger.v3.oas.models.ExternalDocumentation
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.info.License
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.ApiInfoBuilder
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.service.ApiInfo
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket

@Configuration
class SwaggerConfiguration {
@Bean
fun api(): Docket {
return Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
fun api(): OpenAPI {
return OpenAPI()
.info(getApiInfo())
.externalDocs(getExternalDocumentation())
}

private fun getApiInfo(): ApiInfo {
return ApiInfoBuilder()
private fun getExternalDocumentation(): ExternalDocumentation {
return ExternalDocumentation()
.description("RealWorld implementation using Spring Boot with Kotlin")
.url("https://github.com/brunohenriquepj/spring-boot-kotlin-realworld")
}

private fun getApiInfo(): Info {
return Info()
.title("RealWorld API Doc")
.description("Real World API")
.license(
License()
.name("MIT License")
.url("https://github.com/brunohenriquepj/spring-boot-kotlin-realworld/blob/main/LICENSE")
)
.version("0.0.0")
.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import javax.servlet.FilterChain
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class AuthorizationFilter(

class AuthenticationFilter(
authenticationManager: AuthenticationManager,
private val tokenUtil: TokenUtil,
private val userRepository: UserRepository,
Expand Down Expand Up @@ -60,4 +61,4 @@ class AuthorizationFilter(
}
return header.substring(tokenPrefix.length)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.springframework.security.web.AuthenticationEntryPoint
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


class AuthenticationEntryPointHandler : AuthenticationEntryPoint {
override fun commence(
request: HttpServletRequest,
Expand Down
Loading

0 comments on commit a3da132

Please sign in to comment.