-
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.
feat: Use DB backed UserDetailsService
- Loading branch information
Showing
13 changed files
with
124 additions
and
19 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
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
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/dictionary/learning/platform/user/AppUserDetailsService.java
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,26 @@ | ||
package com.dictionary.learning.platform.user; | ||
|
||
import java.util.List; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
|
||
public class AppUserDetailsService implements UserDetailsService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public AppUserDetailsService(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
User user = userRepository.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException(username)); | ||
|
||
List<SimpleGrantedAuthority> authorities = List.of(new SimpleGrantedAuthority(user.getRole())); | ||
|
||
return new org.springframework.security.core.userdetails.User( | ||
user.getUsername(), user.getPassword(), authorities); | ||
} | ||
} |
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
6 changes: 5 additions & 1 deletion
6
app/src/main/java/com/dictionary/learning/platform/user/UserRepository.java
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,9 @@ | ||
package com.dictionary.learning.platform.user; | ||
|
||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
interface UserRepository extends JpaRepository<User, Long> {} | ||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
Optional<User> findByUsername(String userName); | ||
} |
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,3 @@ | ||
spring: | ||
flyway: | ||
locations: 'classpath:/dev/db/migration/postgresql' |
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 |
---|---|---|
|
@@ -17,6 +17,9 @@ management: | |
web: | ||
exposure: | ||
include: info,health,sbom | ||
info: | ||
git: | ||
mode: simple | ||
|
||
server: | ||
error: | ||
|
6 changes: 4 additions & 2 deletions
6
app/src/main/resources/db/migration/postgresql/V001__INIT_DATABASE.sql
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
23 changes: 23 additions & 0 deletions
23
app/src/main/resources/dev/db/migration/postgresql/V001__INIT_DATABASE.sql
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,23 @@ | ||
CREATE SEQUENCE IF NOT EXISTS user_seq START WITH 10 INCREMENT BY 1; | ||
|
||
CREATE TABLE users ( | ||
id BIGINT NOT NULL, | ||
username VARCHAR(255), | ||
password VARCHAR(255), | ||
role VARCHAR(255), | ||
CONSTRAINT pk_user PRIMARY KEY (id) | ||
); | ||
|
||
CREATE SEQUENCE IF NOT EXISTS word_seq START WITH 20 INCREMENT BY 1; | ||
|
||
CREATE TABLE words ( | ||
id BIGINT NOT NULL, | ||
en VARCHAR(255), | ||
sk VARCHAR(255), | ||
lesson INTEGER NOT NULL, | ||
grade INTEGER NOT NULL, | ||
user_id BIGINT, | ||
CONSTRAINT pk_word PRIMARY KEY (id) | ||
); | ||
|
||
ALTER TABLE words ADD CONSTRAINT FK_WORD_ON_USER FOREIGN KEY (user_id) REFERENCES users (id); |
13 changes: 13 additions & 0 deletions
13
app/src/main/resources/dev/db/migration/postgresql/V002__POPULATE_DATABASE.sql
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,13 @@ | ||
-- User | ||
INSERT INTO users (id, username, password, role) VALUES (1, 'jane', '{noop}jane', 'admin'); | ||
INSERT INTO users (id, username, password, role) VALUES (2, 'bob', '{noop}bob', 'user'); | ||
-- Word | ||
INSERT INTO words (id, en, sk, lesson, grade, user_id) VALUES (1, 'father', 'otec', 1, 1, 1); | ||
INSERT INTO words (id, en, sk, lesson, grade, user_id) VALUES (2, 'mother', 'matka', 1, 1, 1); | ||
INSERT INTO words (id, en, sk, lesson, grade, user_id) VALUES (3, 'brother', 'brat', 1, 1, 1); | ||
INSERT INTO words (id, en, sk, lesson, grade, user_id) VALUES (4, 'sister', 'sestra', 1, 1, 1); | ||
INSERT INTO words (id, en, sk, lesson, grade, user_id) VALUES (5, 'water', 'voda', 1, 2, 1); | ||
INSERT INTO words (id, en, sk, lesson, grade, user_id) VALUES (6, 'fire', 'oheň', 1, 2, 1); | ||
INSERT INTO words (id, en, sk, lesson, grade, user_id) VALUES (7, 'lion', 'lev', 2, 2, 1); | ||
INSERT INTO words (id, en, sk, lesson, grade, user_id) VALUES (8, 'mom', 'mama', 1, 1, 2); | ||
INSERT INTO words (id, en, sk, lesson, grade, user_id) VALUES (9, 'dad', 'tata', 1, 1, 2); |
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