-
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
Showing
21 changed files
with
487 additions
and
29 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
5 changes: 2 additions & 3 deletions
5
src/main/kotlin/com/albatros/springsecurity/SpringSecurityApplication.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
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
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/albatros/springsecurity/config/security/JwtConfig.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.albatros.springsecurity.config.security | ||
|
||
import jakarta.validation.constraints.Min | ||
import jakarta.validation.constraints.NotEmpty | ||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.validation.annotation.Validated | ||
|
||
@Validated | ||
@EnableConfigurationProperties(JwtConfig::class) | ||
@ConfigurationProperties("jwt") | ||
data class JwtConfig( | ||
@field:NotEmpty | ||
val key: String, | ||
@field:Min(0) | ||
val accessTokenExpiration: Long, | ||
@field:Min(0) | ||
val refreshTokenExpiration: Long, | ||
) |
86 changes: 77 additions & 9 deletions
86
src/main/kotlin/com/albatros/springsecurity/config/security/SecurityConfig.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,11 +1,79 @@ | ||
package com.albatros.springsecurity.config.security | ||
|
||
//import org.springframework.context.annotation.Configuration | ||
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity | ||
// | ||
//@Configuration | ||
//@EnableWebSecurity | ||
//class SecurityConfig { | ||
// | ||
//} | ||
// | ||
import com.albatros.springsecurity.domain.service.UserService | ||
import com.albatros.springsecurity.filter.JwtAuthenticationFilter | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.security.authentication.AuthenticationManager | ||
import org.springframework.security.authentication.AuthenticationProvider | ||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider | ||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration | ||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity | ||
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 | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter | ||
import org.springframework.web.cors.CorsConfiguration | ||
|
||
@Configuration | ||
@EnableMethodSecurity | ||
@EnableWebSecurity | ||
class SecurityConfig( | ||
private val jwtAuthenticationFilter: JwtAuthenticationFilter, | ||
private val userService: UserService | ||
) { | ||
@Bean | ||
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain = | ||
http.csrf { it.disable() } | ||
.cors { | ||
it.configurationSource { | ||
val corsConfig = CorsConfiguration() | ||
corsConfig.allowedMethods = listOf("GET", "POST", "PUT", "DELETE", "OPTIONS") | ||
corsConfig.setAllowedOriginPatterns( | ||
listOf("*") | ||
) | ||
corsConfig.allowedHeaders = listOf("*") | ||
corsConfig.allowCredentials = true | ||
corsConfig | ||
} | ||
} | ||
.authorizeHttpRequests { | ||
it.requestMatchers("/auth/**").permitAll() | ||
.requestMatchers("/user/admin").hasRole("USER") | ||
.requestMatchers("/user/**").hasRole("ADMIN") | ||
.requestMatchers( | ||
"/swagger-ui/**", | ||
"/swagger-ui.html", | ||
"/swagger-resources/*", | ||
"/api-docs/**", | ||
"/api-docs" | ||
).permitAll() | ||
.anyRequest().authenticated() | ||
} | ||
.sessionManagement { | ||
it.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
} | ||
.authenticationProvider( | ||
getAuthenticationProvider() | ||
) | ||
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter::class.java) | ||
.build() | ||
|
||
@Bean | ||
fun getAuthenticationProvider(): AuthenticationProvider = | ||
DaoAuthenticationProvider().apply { | ||
setUserDetailsService(userService.userDetailsService()) | ||
setPasswordEncoder(getPasswordEncoder()) | ||
} | ||
|
||
@Bean | ||
fun getPasswordEncoder(): PasswordEncoder = BCryptPasswordEncoder() | ||
|
||
@Bean | ||
fun getAuthenticationManager(config: AuthenticationConfiguration): AuthenticationManager = | ||
config.authenticationManager | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/com/albatros/springsecurity/controller/AuthController.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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.albatros.springsecurity.controller | ||
|
||
import com.albatros.springsecurity.domain.model.request.SignInRequest | ||
import com.albatros.springsecurity.domain.model.request.SignUpRequest | ||
import com.albatros.springsecurity.domain.service.AuthenticationService | ||
import jakarta.validation.Valid | ||
import org.springframework.validation.annotation.Validated | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@Validated | ||
@RequestMapping("/auth") | ||
class AuthController( | ||
private val authenticationService: AuthenticationService, | ||
) { | ||
@PostMapping("/sign-up") | ||
fun signUp(@Valid @RequestBody signUpRequest: SignUpRequest) = authenticationService.signUp(signUpRequest) | ||
|
||
@PostMapping("/sign-in") | ||
fun signIn(@Valid @RequestBody signInRequest: SignInRequest) = authenticationService.signIn(signInRequest) | ||
|
||
} |
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
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/albatros/springsecurity/domain/model/exception/AlreadyExistsException.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.albatros.springsecurity.domain.model.exception | ||
|
||
import org.springframework.http.HttpStatus | ||
|
||
class AlreadyExistsException(override val message: String) : AbstractApiException() { | ||
override val status: HttpStatus | ||
get() = HttpStatus.CONFLICT | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/albatros/springsecurity/domain/model/request/SignInRequest.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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.albatros.springsecurity.domain.model.request | ||
|
||
import jakarta.validation.constraints.Email | ||
import jakarta.validation.constraints.NotBlank | ||
import jakarta.validation.constraints.Size | ||
|
||
data class SignInRequest( | ||
|
||
@field:Size(min = 5, max = 30) | ||
@field:NotBlank | ||
val username: String, | ||
|
||
@field:Size(min = 5, max = 30) | ||
@field:NotBlank | ||
val password: String | ||
|
||
) |
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/com/albatros/springsecurity/domain/model/request/SignUpRequest.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.albatros.springsecurity.domain.model.request | ||
|
||
import jakarta.validation.constraints.Email | ||
import jakarta.validation.constraints.NotBlank | ||
import jakarta.validation.constraints.Size | ||
|
||
data class SignUpRequest( | ||
|
||
@field:Size(min = 5, max = 30) | ||
@field:NotBlank | ||
val username: String, | ||
|
||
@field:Email | ||
val email: String, | ||
|
||
@field:Size(min = 5, max = 30) | ||
@field:NotBlank | ||
val password: String | ||
|
||
) |
5 changes: 5 additions & 0 deletions
5
...ain/kotlin/com/albatros/springsecurity/domain/model/response/JwtAuthenticationResponse.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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.albatros.springsecurity.domain.model.response | ||
|
||
data class JwtAuthenticationResponse( | ||
val token: String | ||
) |
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
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/albatros/springsecurity/domain/service/AuthenticationService.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.albatros.springsecurity.domain.service | ||
|
||
import com.albatros.springsecurity.domain.model.request.SignInRequest | ||
import com.albatros.springsecurity.domain.model.request.SignUpRequest | ||
import com.albatros.springsecurity.domain.model.response.JwtAuthenticationResponse | ||
import org.springframework.validation.annotation.Validated | ||
|
||
@Validated | ||
interface AuthenticationService { | ||
|
||
fun signUp(request: SignUpRequest): JwtAuthenticationResponse | ||
|
||
fun signIn(request: SignInRequest): JwtAuthenticationResponse | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/albatros/springsecurity/domain/service/JwtService.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.albatros.springsecurity.domain.service | ||
|
||
import java.util.Date | ||
import org.springframework.security.core.userdetails.UserDetails | ||
import org.springframework.validation.annotation.Validated | ||
|
||
@Validated | ||
interface JwtService { | ||
fun isTokenValid(token: String, userDetails: UserDetails): Boolean | ||
fun extractUsername(token: String): String? | ||
fun generateToken(userDetails: UserDetails): String | ||
} |
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.