Skip to content

Commit 0eb590f

Browse files
committed
added timeout after 3 login attempts to server manager
1 parent 4a384b8 commit 0eb590f

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

server_manager/src/main/kotlin/com/imsproject/servermanager/SecurityConfig.kt

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.imsproject.servermanager
22

3+
import kotlinx.coroutines.DelicateCoroutinesApi
4+
import kotlinx.coroutines.GlobalScope
5+
import kotlinx.coroutines.Job
6+
import kotlinx.coroutines.launch
37
import org.springframework.context.annotation.Bean
48
import org.springframework.context.annotation.Configuration
59
import org.springframework.security.authentication.AuthenticationManager
@@ -12,6 +16,9 @@ import org.springframework.security.core.Authentication
1216
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
1317
import org.springframework.security.crypto.password.PasswordEncoder
1418
import org.springframework.security.web.SecurityFilterChain
19+
import org.springframework.security.web.authentication.WebAuthenticationDetails
20+
import java.util.concurrent.ConcurrentHashMap
21+
import java.util.concurrent.atomic.AtomicInteger
1522

1623
@EnableWebSecurity
1724
@Configuration
@@ -34,17 +41,45 @@ class SecurityConfig {
3441

3542
private val encoder: PasswordEncoder = BCryptPasswordEncoder()
3643
private val pass = "\$2a\$10\$Ss09W28r0vuNd67EHqcAw.piDzMvPFV4YHK0d0rh2C30O26NYAewG"
44+
private val badAttemptsMap : MutableMap<String, AtomicInteger> = ConcurrentHashMap()
45+
private val lockedOutAddresses : MutableMap<String, Job> = ConcurrentHashMap()
3746

3847
override fun authenticate(authentication: Authentication): Authentication {
39-
val userName = authentication.name
40-
if(userName.lowercase() != "admin") {
41-
throw BadCredentialsException("Bad Credentials")
48+
val details = authentication.details as WebAuthenticationDetails
49+
val remoteAddress = details.remoteAddress
50+
51+
if(lockedOutAddresses.contains(remoteAddress)) {
52+
throw BadCredentialsException("Login attempts exceeded, try again later")
4253
}
43-
val password = authentication.credentials.toString()
44-
if (encoder.matches(password, pass)) {
45-
return UsernamePasswordAuthenticationToken(userName, password, emptyList())
46-
} else {
47-
throw BadCredentialsException("Bad Credentials")
54+
55+
try{
56+
val userName = authentication.name
57+
if(userName.lowercase() != "admin") {
58+
badAttemptsMap.computeIfAbsent(remoteAddress) { AtomicInteger(0) }.incrementAndGet()
59+
throw BadCredentialsException("Bad Credentials")
60+
}
61+
val password = authentication.credentials.toString()
62+
if (encoder.matches(password, pass)) {
63+
badAttemptsMap.remove(remoteAddress)
64+
return UsernamePasswordAuthenticationToken(userName, password, emptyList())
65+
} else {
66+
badAttemptsMap.computeIfAbsent(remoteAddress) { AtomicInteger(0) }.incrementAndGet()
67+
throw BadCredentialsException("Bad Credentials")
68+
}
69+
} finally {
70+
val attemptsCount = badAttemptsMap[remoteAddress]
71+
if(attemptsCount != null){
72+
synchronized(attemptsCount) {
73+
if(remoteAddress !in lockedOutAddresses && attemptsCount.get() >= 3) {
74+
@OptIn(DelicateCoroutinesApi::class)
75+
lockedOutAddresses[remoteAddress] = GlobalScope.launch {
76+
kotlinx.coroutines.delay(30 * 1000)
77+
lockedOutAddresses.remove(remoteAddress)
78+
badAttemptsMap.remove(remoteAddress)
79+
}
80+
}
81+
}
82+
}
4883
}
4984
}
5085
})

server_manager/src/main/kotlin/com/imsproject/servermanager/ServerManagerApplication.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.imsproject.servermanager
22

33
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration
45
import org.springframework.boot.runApplication
56

6-
@SpringBootApplication
7+
@SpringBootApplication(exclude = [UserDetailsServiceAutoConfiguration::class])
78
class DockerControllerApplication
89

910
fun main(args: Array<String>) {

0 commit comments

Comments
 (0)