Skip to content

Commit 787dcef

Browse files
committed
feat(core): add simple jwt authentication
1 parent 089ebcb commit 787dcef

File tree

16 files changed

+161
-41
lines changed

16 files changed

+161
-41
lines changed

proxy.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
{
2-
"/auth": {
3-
"target": "http://localhost:8080",
4-
"secure": "false"
5-
},
62
"/api": {
73
"target": "http://localhost:8080",
84
"secure": "false"

schema.sql

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
drop constraint FKt0mnl3rej2p0h9gxnbalf2kdd;
1313

1414
alter table roles_permissions
15-
drop constraint FKeqt383nibym26cjj8we4uar8h;
15+
drop constraint FK570wuy6sacdnrw8wdqjfh7j0q;
1616

1717
alter table roles_permissions
1818
drop constraint FKqi9odri6c1o81vjox54eedwyh;
@@ -197,7 +197,7 @@
197197
first_name varchar(120) not null,
198198
last_name varchar(120) not null,
199199
locked boolean not null,
200-
password varchar(64) not null,
200+
password varchar(120) not null,
201201
username varchar(250) not null,
202202
primary key (id)
203203
);
@@ -217,7 +217,7 @@
217217
first_name varchar(120),
218218
last_name varchar(120),
219219
locked boolean,
220-
password varchar(64),
220+
password varchar(120),
221221
username varchar(250),
222222
primary key (id, rev)
223223
);
@@ -242,15 +242,9 @@
242242
alter table roles
243243
add constraint UK_ofx66keruapi6vyqpv6f2or37 unique (name);
244244

245-
alter table roles_permissions
246-
add constraint UK_oll9subcln0cdjt31bp72a3uv unique (permissions_id);
247-
248245
alter table users
249246
add constraint UK_r43af9ap4edm43mmtq01oddj6 unique (username);
250247

251-
alter table users_roles
252-
add constraint UK_60loxav507l5mreo05v0im1lq unique (roles_id);
253-
254248
alter table jwt_tokens
255249
add constraint FKhy6n4wirmw0ryw2wdmy9cx2mn
256250
foreign key (user_id)
@@ -272,9 +266,9 @@
272266
references revinfo;
273267

274268
alter table roles_permissions
275-
add constraint FKeqt383nibym26cjj8we4uar8h
269+
add constraint FK570wuy6sacdnrw8wdqjfh7j0q
276270
foreign key (permissions_id)
277-
references roles;
271+
references permissions;
278272

279273
alter table roles_permissions
280274
add constraint FKqi9odri6c1o81vjox54eedwyh

src/main/kotlin/com/shardis/domain/Role.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import javax.persistence.*
1111
@SequenceGenerator(allocationSize = 1, name = "sequenceIdGenerator", sequenceName = "sequence_roles")
1212
class Role(
1313
@Column(nullable = false, length = 64, unique = true) val name: String,
14-
@OneToMany(fetch = FetchType.LAZY) val permissions: MutableSet<Role> = mutableSetOf()
14+
@ManyToMany(fetch = FetchType.LAZY) val permissions: MutableSet<Permission> = mutableSetOf()
1515
) : AuditedEntity(), Serializable, GrantedAuthority {
1616
override fun getAuthority() = name
1717
}

src/main/kotlin/com/shardis/domain/User.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import javax.persistence.*
99
@SequenceGenerator(allocationSize = 1, name = "sequenceIdGenerator", sequenceName = "sequence_users")
1010
class User(
1111
@Column(nullable = false, length = 250, unique = true) var username: String,
12-
@Column(nullable = false, length = 64) var password: String,
12+
@Column(nullable = false, length = 120) var password: String,
1313
@Column(nullable = false, length = 120) var firstName: String,
1414
@Column(nullable = false, length = 120) var lastName: String,
1515
@Column(nullable = false, length = 250) var email: String,
1616
@Column(nullable = false) var enabled: Boolean,
1717
@Column(nullable = false) var expired: Boolean,
1818
@Column(nullable = false) var locked: Boolean,
19-
@OneToMany(fetch = FetchType.LAZY) var roles: MutableSet<Role> = mutableSetOf()
19+
@ManyToMany(fetch = FetchType.LAZY) var roles: MutableSet<Role> = mutableSetOf()
2020
) : AuditedEntity()
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.shardis.init
2+
3+
import com.shardis.domain.Permission
4+
import com.shardis.domain.Role
5+
import com.shardis.domain.User
6+
import com.shardis.repositories.PermissionRepository
7+
import com.shardis.repositories.RoleRepository
8+
import com.shardis.repositories.UserRepository
9+
import org.slf4j.LoggerFactory
10+
import org.springframework.security.crypto.password.PasswordEncoder
11+
import org.springframework.stereotype.Component
12+
import javax.annotation.PostConstruct
13+
import javax.transaction.Transactional
14+
15+
16+
@Transactional
17+
@Component
18+
class DbInitializer(
19+
val permissionRepository: PermissionRepository,
20+
val roleRepository: RoleRepository,
21+
val userRepository: UserRepository,
22+
val passwordEncoder: PasswordEncoder
23+
) {
24+
25+
@PostConstruct
26+
open fun dbInit() {
27+
28+
if (userRepository.findAll().none()) {
29+
30+
log.info("Db initialization")
31+
32+
val p1 = Permission("PERM_ONE")
33+
val p2 = Permission("PERM_TWO")
34+
val p3 = Permission("PERM_THREE")
35+
36+
log.info("saving permissions")
37+
38+
with(permissionRepository) {
39+
save(p1)
40+
save(p2)
41+
save(p3)
42+
}
43+
44+
45+
val roleAdmin = Role("ROLE_ADMIN", mutableSetOf(p1, p2, p3))
46+
val roleUser = Role("ROLE_USER", mutableSetOf(p1))
47+
48+
log.info("saving roles")
49+
50+
with(roleRepository) {
51+
save(roleAdmin)
52+
save(roleUser)
53+
}
54+
55+
val admin = User(
56+
username = "admin",
57+
password = passwordEncoder.encode("xxxxxx"),
58+
firstName = "Admin",
59+
lastName = "Admin",
60+
email = "admin@admin.com",
61+
enabled = true,
62+
expired = false,
63+
locked = false,
64+
roles = mutableSetOf(roleAdmin)
65+
)
66+
67+
val user = User(
68+
username = "user",
69+
password = passwordEncoder.encode("xxxxxx"),
70+
firstName = "User",
71+
lastName = "User",
72+
email = "user@user.com",
73+
enabled = true,
74+
expired = false,
75+
locked = false,
76+
roles = mutableSetOf(roleUser)
77+
)
78+
79+
log.info("saving users")
80+
81+
with(userRepository) {
82+
save(admin)
83+
save(user)
84+
}
85+
86+
}
87+
}
88+
89+
companion object {
90+
val log = LoggerFactory.getLogger(DbInitializer::class.java)
91+
}
92+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.shardis.repositories
2+
3+
import com.shardis.domain.Permission
4+
import org.springframework.data.repository.CrudRepository
5+
import org.springframework.stereotype.Repository
6+
7+
8+
@Repository
9+
interface PermissionRepository : CrudRepository<Permission, Long>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.shardis.repositories
2+
3+
import com.shardis.domain.Role
4+
import org.springframework.data.repository.CrudRepository
5+
import org.springframework.stereotype.Repository
6+
7+
8+
@Repository
9+
interface RoleRepository : CrudRepository<Role, Long>

src/main/kotlin/com/shardis/security/SecurityConfig.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.shardis.security
22

33
import com.shardis.ShardisProperties
44
import com.shardis.security.jwt.*
5-
import org.springframework.beans.factory.BeanInitializationException
65
import org.springframework.beans.factory.annotation.Autowired
76
import org.springframework.context.annotation.Bean
87
import org.springframework.context.annotation.Configuration
@@ -43,13 +42,9 @@ open class SecurityConfig(
4342

4443
@Autowired
4544
open fun configureGlobal(auth: AuthenticationManagerBuilder) {
46-
try {
47-
auth
48-
.userDetailsService(shardisUserDetailsService)
49-
.passwordEncoder(passwordEncoder())
50-
} catch (e: Exception) {
51-
throw BeanInitializationException("Security configuration failed", e)
52-
}
45+
auth
46+
.userDetailsService(shardisUserDetailsService)
47+
.passwordEncoder(passwordEncoder())
5348
}
5449

5550
override fun configure(httpSecurity: HttpSecurity) {

src/main/kotlin/com/shardis/security/ShardisUserDetailsService.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.shardis.security.support.ShardisUserDetails
77
import org.springframework.security.core.GrantedAuthority
88
import org.springframework.security.core.userdetails.UserDetails
99
import org.springframework.security.core.userdetails.UserDetailsService
10+
import org.springframework.security.core.userdetails.UsernameNotFoundException
1011
import org.springframework.stereotype.Service
1112
import org.springframework.transaction.annotation.Transactional
1213

@@ -17,16 +18,14 @@ open class ShardisUserDetailsService(val userRepository: UserRepository) : UserD
1718

1819
override fun loadUserByUsername(username: String): UserDetails? {
1920

20-
val user: User? = userRepository.findByUsername(username)
21+
val user: User = userRepository.findByUsername(username) ?: throw UsernameNotFoundException("User $username not found")
22+
23+
val authorities = mutableSetOf<GrantedAuthority>()
24+
authorities.addAll(user.roles)
25+
authorities.addAll(user.roles.flatMap(Role::permissions))
26+
return ShardisUserDetails(user.id!!, user.username, user.password, authorities)
2127

22-
user?.let {
23-
val authorities = mutableSetOf<GrantedAuthority>()
24-
authorities.addAll(user.roles)
25-
authorities.addAll(user.roles.flatMap(Role::permissions))
26-
return ShardisUserDetails(user.id!!, user.username, user.password, authorities)
27-
}
2828

29-
return null
3029
}
3130

3231
}

src/main/kotlin/com/shardis/security/support/SecurityUtils.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import org.springframework.security.core.context.SecurityContextHolder
55
object SecurityUtils {
66

77
fun getLoggedUser(): ShardisUserDetails? {
8-
val principal: Any = SecurityContextHolder.getContext().authentication.principal
9-
if (principal is ShardisUserDetails) {
10-
return principal
11-
} else {
12-
return null
8+
val principal: Any? = SecurityContextHolder.getContext()?.authentication?.principal
9+
10+
return when (principal) {
11+
is ShardisUserDetails -> principal
12+
else -> null
1313
}
1414
}
1515

0 commit comments

Comments
 (0)