Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Security Configuration and Role Management #10

Merged
merged 3 commits into from
May 14, 2024

Conversation

nklimovych
Copy link
Owner

  • Added SecurityConfig class with PasswordEncoder and SecurityFilterChain beans
  • Implemented UserDetails interface in the User class.
  • Added the implementation for the UserDetailsService interface.
  • Added Role entity.
  • Added RoleRepository.
  • Added roles field to the User entity.
  • Added Liquibase changesets to insert roles into DB and assign roles for users.

…in beans. Implemented UserDetails interface in the User class. Added the implementation for UserDetailsService interface. Added Role entity. Added RoleRepository. Added roles field to the User entity. Added Liquibase changesets to insert roles into DB and assign roles for users.
Copy link

@karma-o karma-o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some very minor comments, good job!


@Column(nullable = false, unique = true)
@Enumerated(EnumType.STRING)
private RoleName role;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private RoleName role;
private RoleName name;

@@ -26,6 +32,8 @@ public UserResponseDto save(UserRegistrationRequestDto requestDto)

User user = userMapper.toModel(requestDto);
user.setEmail(email);
user.setPassword(passwordEncoder.encode(user.getPassword()));
user.setRoles(roleRepository.findAllByRoles(Set.of(RoleName.USER)));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have hardcoded only USER role here why wrap it into a set and do a findAll...()? Lets simplify it

Suggested change
user.setRoles(roleRepository.findAllByRoles(Set.of(RoleName.USER)));
user.setRoles(Collections.singleton(roleRepository.findByName(RoleName.USER)));


public interface RoleRepository extends JpaRepository<Role, Long> {

@Query("SELECT r FROM Role r WHERE r.role IN :roles")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think @Querry is necessary here

Suggested change
@Query("SELECT r FROM Role r WHERE r.role IN :roles")

…or the new user in the 'save' method in UserService, and replace the 'findAllByRoles' method with the 'findByName' method.
@nklimovych nklimovych merged commit afcb489 into main May 14, 2024
2 checks passed
@nklimovych nklimovych deleted the spring-security-extended branch May 14, 2024 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants