-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0527e63
commit a3da132
Showing
16 changed files
with
138 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 41 additions & 34 deletions
75
src/main/kotlin/com/example/realworld/configuration/SecurityConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**" | ||
) | ||
} | ||
} | ||
} |
39 changes: 22 additions & 17 deletions
39
src/main/kotlin/com/example/realworld/configuration/SwaggerConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.