-
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
17 changed files
with
309 additions
and
10 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
41 changes: 41 additions & 0 deletions
41
src/main/java/project/bookstore/config/SecurityConfig.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,41 @@ | ||
package project.bookstore.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.Customizer; | ||
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.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
@Configuration | ||
@EnableMethodSecurity | ||
@RequiredArgsConstructor | ||
public class SecurityConfig { | ||
private final UserDetailsService userDetailsService; | ||
|
||
@Bean | ||
public PasswordEncoder getPasswordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain getSecurityFilterChain(HttpSecurity http) throws Exception { | ||
return http | ||
.cors(AbstractHttpConfigurer::disable) | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.authorizeHttpRequests( | ||
auth -> auth | ||
.requestMatchers("/auth/**", "/swagger-ui/**", "/v3/api-docs/**") | ||
.permitAll() | ||
.anyRequest() | ||
.authenticated() | ||
) | ||
.userDetailsService(userDetailsService) | ||
.build(); | ||
} | ||
} |
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
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,27 @@ | ||
package project.bookstore.model; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import org.springframework.security.core.GrantedAuthority; | ||
|
||
@Entity | ||
@Table(name = "roles") | ||
public class Role implements GrantedAuthority { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(unique = true, nullable = false) | ||
@Enumerated(EnumType.STRING) | ||
private RoleName role; | ||
|
||
@Override | ||
public String getAuthority() { | ||
return role.name(); | ||
} | ||
} |
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,6 @@ | ||
package project.bookstore.model; | ||
|
||
public enum RoleName { | ||
USER, | ||
ADMIN | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/project/bookstore/repository/role/RoleRepo.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,7 @@ | ||
package project.bookstore.repository.role; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import project.bookstore.model.Role; | ||
|
||
public interface RoleRepo extends JpaRepository<Role, Integer> { | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/project/bookstore/security/CustomUserDetailsService.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,20 @@ | ||
package project.bookstore.security; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
import project.bookstore.repository.user.UserRepo; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CustomUserDetailsService implements UserDetailsService { | ||
private final UserRepo userRepo; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { | ||
return userRepo.findByEmail(email) | ||
.orElseThrow(() -> new UsernameNotFoundException("Can't find user by email: " + email)); | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
src/main/resources/db/changelog/changes/03_create_roles_table.yaml
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,21 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: create_roles_table | ||
author: vlad | ||
changes: | ||
- createTable: | ||
tableName: roles | ||
columns: | ||
- column: | ||
name: id | ||
type: bigint | ||
autoIncrement: true | ||
constraints: | ||
primaryKey: true | ||
nullable: false | ||
- column: | ||
name: role | ||
type: varchar(255) | ||
constraints: | ||
unique: true | ||
nullable: false |
24 changes: 24 additions & 0 deletions
24
src/main/resources/db/changelog/changes/04_create_users_roles_table.yaml
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,24 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: create_users_roles_table | ||
author: vlad | ||
changes: | ||
- createTable: | ||
tableName: users_roles | ||
columns: | ||
- column: | ||
name: user_id | ||
type: bigint | ||
constraints: | ||
primaryKey: true | ||
nullable: false | ||
foreignKeyName: fk_users_id | ||
references: users(id) | ||
- column: | ||
name: role_id | ||
type: bigint | ||
constraints: | ||
primaryKey: true | ||
nullable: false | ||
foreignKeyName: fk_roles_id | ||
references: roles(id) |
13 changes: 13 additions & 0 deletions
13
src/main/resources/db/changelog/changes/05_add_role_id_to_users.yaml
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 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: add_role_id_to_users | ||
author: vlad | ||
changes: | ||
- addColumn: | ||
tableName: users | ||
columns: | ||
- column: | ||
name: role_id | ||
type: bigint | ||
constraints: | ||
nullable: false |
98 changes: 98 additions & 0 deletions
98
src/main/resources/db/changelog/changes/06_insert_users_to_db.yaml
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,98 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: 1 | ||
author: vlad | ||
changes: | ||
- insert: | ||
tableName: roles | ||
columns: | ||
- column: | ||
name: id | ||
valueNumeric: 1 | ||
- column: | ||
name: role | ||
value: ROLE_USER | ||
- insert: | ||
tableName: roles | ||
columns: | ||
- column: | ||
name: id | ||
valueNumeric: 2 | ||
- column: | ||
name: role | ||
value: ROLE_ADMIN | ||
|
||
- changeSet: | ||
id: 2 | ||
author: vlad | ||
changes: | ||
- insert: | ||
tableName: users | ||
columns: | ||
- column: | ||
name: id | ||
valueNumeric: 1 | ||
- column: | ||
name: email | ||
value: [email protected] | ||
- column: | ||
name: password | ||
value: encodedPassword1 | ||
- column: | ||
name: first_name | ||
value: John | ||
- column: | ||
name: last_name | ||
value: Doe | ||
- column: | ||
name: shipping_address | ||
value: 123 Main St, City, Country | ||
- column: | ||
name: role_id | ||
valueNumeric: 1 | ||
- insert: | ||
tableName: users | ||
columns: | ||
- column: | ||
name: id | ||
valueNumeric: 2 | ||
- column: | ||
name: email | ||
value: [email protected] | ||
- column: | ||
name: password | ||
value: encodedPassword2 | ||
- column: | ||
name: first_name | ||
value: Admin | ||
- column: | ||
name: last_name | ||
value: User | ||
- column: | ||
name: shipping_address | ||
value: 456 Admin St, City, Country | ||
- column: | ||
name: role_id | ||
valueNumeric: 2 | ||
- changeSet: | ||
id: 3 | ||
author: vlad | ||
changes: | ||
- insert: | ||
tableName: users_roles | ||
columns: | ||
- column: | ||
name: user_id | ||
valueNumeric: 1 | ||
- column: | ||
name: role_id | ||
valueNumeric: 1 | ||
- insert: | ||
tableName: users_roles | ||
columns: | ||
- column: | ||
name: user_id | ||
valueNumeric: 2 | ||
- column: | ||
name: role_id | ||
valueNumeric: 2 |
Oops, something went wrong.