Compare commits
3 Commits
920fbe489d
...
2bb46499bc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2bb46499bc | ||
|
|
cb07b71a88 | ||
|
|
a8046a1227 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -31,3 +31,5 @@ build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
**/docker/postgresql/pgdata
|
||||
@@ -8,6 +8,13 @@ services:
|
||||
- "50001:5432"
|
||||
networks:
|
||||
- "sportshub-local-network"
|
||||
environment:
|
||||
POSTGRES_DB: sportshub_db
|
||||
POSTGRES_USER: sportshub_admin
|
||||
POSTGRES_PASSWORD: password
|
||||
PGDATA: /var/lib/postgresql/data/pgdata
|
||||
volumes:
|
||||
- "./docker/postgresql/pgdata:/var/lib/postgresql/data/pgdata"
|
||||
|
||||
networks:
|
||||
sportshub-local-network:
|
||||
6
pom.xml
6
pom.xml
@@ -17,6 +17,7 @@
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
|
||||
<java-jwt.version>4.4.0</java-jwt.version>
|
||||
<postgresql.version>42.7.0</postgresql.version>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
@@ -66,6 +67,11 @@
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>${java-jwt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.sportshub.application.configuration;
|
||||
|
||||
import static org.sportshub.domain.user.model.UserRole.ADMIN;
|
||||
import static org.springframework.http.HttpMethod.GET;
|
||||
import static org.springframework.http.HttpMethod.OPTIONS;
|
||||
import static org.springframework.http.HttpMethod.POST;
|
||||
@@ -9,6 +8,7 @@ import org.sportshub.application.security.JwtAuthenticationFilter;
|
||||
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.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||
@@ -23,6 +23,7 @@ import static jakarta.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@EnableMethodSecurity(securedEnabled = true)
|
||||
public class SecurityConfiguration {
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(
|
||||
@@ -45,10 +46,6 @@ public class SecurityConfiguration {
|
||||
"/api/health/check",
|
||||
"/error"
|
||||
).permitAll()
|
||||
.requestMatchers(
|
||||
GET,
|
||||
"/api/users"
|
||||
).hasAuthority(ADMIN.name())
|
||||
.requestMatchers(
|
||||
POST,
|
||||
"/api/users/login"
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.sportshub.application.security.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@PreAuthorize("hasAuthority('ROLE_' + T(org.sportshub.domain.user.model.UserRole).ADMIN.name())")
|
||||
public @interface AllowedToAdmins {
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.sportshub.application.security.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@PreAuthorize("permitAll()")
|
||||
public @interface AllowedToAnonymous {
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.sportshub.application.security.JwtService;
|
||||
import org.sportshub.application.security.annotation.AllowedToAdmins;
|
||||
import org.sportshub.domain.exception.LoginFailureException;
|
||||
import org.sportshub.domain.user.model.User;
|
||||
import org.sportshub.domain.user.port.UserPort;
|
||||
@@ -31,6 +32,7 @@ public class UserUseCases {
|
||||
return userPort.findById(userId);
|
||||
}
|
||||
|
||||
@AllowedToAdmins
|
||||
public List<User> findAll() {
|
||||
return userPort.findAll();
|
||||
}
|
||||
|
||||
@@ -10,4 +10,6 @@ public interface UserPort {
|
||||
Optional<User> findById(UUID userId);
|
||||
|
||||
List<User> findAll();
|
||||
|
||||
void save(User user);
|
||||
}
|
||||
|
||||
@@ -25,5 +25,17 @@
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.sportshub.infrastructure.configuration;
|
||||
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories("org.sportshub.infrastructure")
|
||||
@EntityScan("org.sportshub.infrastructure")
|
||||
public class JpaConfiguration {
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import org.sportshub.domain.user.model.User;
|
||||
import org.sportshub.domain.user.port.UserPort;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
//@Component
|
||||
public class UserInMemoryAdapter implements UserPort {
|
||||
private static final List<User> users = List.of(
|
||||
new User(
|
||||
@@ -41,4 +41,9 @@ public class UserInMemoryAdapter implements UserPort {
|
||||
public List<User> findAll() {
|
||||
return users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(final User user) {
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.sportshub.infrastructure.user.adapter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.sportshub.domain.user.model.User;
|
||||
import org.sportshub.domain.user.port.UserPort;
|
||||
import org.sportshub.infrastructure.user.mapper.UserMapper;
|
||||
import org.sportshub.infrastructure.user.model.UserEntity;
|
||||
import org.sportshub.infrastructure.user.repository.UserJpaRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserJpaAdapter implements UserPort {
|
||||
private final UserJpaRepository userJpaRepository;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
public UserJpaAdapter(
|
||||
UserJpaRepository userJpaRepository,
|
||||
UserMapper userMapper
|
||||
) {
|
||||
this.userJpaRepository = userJpaRepository;
|
||||
this.userMapper = userMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> findById(final UUID userId) {
|
||||
return userJpaRepository.findById(userId)
|
||||
.map(userMapper::mapFrom);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> findAll() {
|
||||
return userJpaRepository.findAll()
|
||||
.stream()
|
||||
.map(userMapper::mapFrom)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(User user) {
|
||||
UserEntity userEntity = userMapper.mapTo(user);
|
||||
userJpaRepository.save(userEntity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.sportshub.infrastructure.user.mapper;
|
||||
|
||||
import org.sportshub.domain.user.model.User;
|
||||
import org.sportshub.infrastructure.user.model.UserEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserMapper {
|
||||
public User mapFrom(UserEntity userEntity) {
|
||||
return new User(userEntity.getId(), userEntity.getPassword(), userEntity.getRoles());
|
||||
}
|
||||
|
||||
public UserEntity mapTo(User user) {
|
||||
return new UserEntity(
|
||||
user.id(),
|
||||
user.password(),
|
||||
user.roles()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.sportshub.infrastructure.user.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.sportshub.domain.user.model.UserRole;
|
||||
|
||||
import jakarta.persistence.CollectionTable;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Table(name = "`user`")
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class UserEntity {
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String password;
|
||||
|
||||
@ElementCollection(targetClass = UserRole.class)
|
||||
@CollectionTable(
|
||||
name = "user_role",
|
||||
joinColumns = @JoinColumn(name = "user_id")
|
||||
)
|
||||
@Column(name = "role")
|
||||
private List<UserRole> roles;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.sportshub.infrastructure.user.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.sportshub.infrastructure.user.model.UserEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public interface UserJpaRepository extends JpaRepository<UserEntity, UUID> {
|
||||
@Query("SELECT u FROM UserEntity u JOIN FETCH u.roles WHERE u.id = :userId")
|
||||
Optional<UserEntity> findById(@Param("userId") UUID userId);
|
||||
|
||||
@Query("SELECT u FROM UserEntity u JOIN FETCH u.roles")
|
||||
List<UserEntity> findAll();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
\c sportshub_db
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
CREATE USER sportshub_user
|
||||
WITH PASSWORD 'password'
|
||||
NOCREATEDB;
|
||||
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE
|
||||
ON ALL TABLES
|
||||
IN SCHEMA public
|
||||
TO sportshub_user;
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE IF NOT EXISTS "user" (
|
||||
id UUID NOT NULL,
|
||||
password VARCHAR NOT NULL,
|
||||
CONSTRAINT user_pk PRIMARY KEY (id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_role (
|
||||
user_id UUID NOT NULL,
|
||||
role SMALLINT,
|
||||
CONSTRAINT user_role_pk PRIMARY KEY (user_id, role),
|
||||
CONSTRAINT user_role_fk_user_id FOREIGN KEY (user_id) REFERENCES "user" (id)
|
||||
);
|
||||
CREATE INDEX user_role_fk_user_id_idx ON user_role (user_id);
|
||||
@@ -12,3 +12,10 @@ server:
|
||||
whitelabel:
|
||||
enabled: false # Disable html error responses.
|
||||
include-stacktrace: never
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
driverClassName: org.postgresql.Driver
|
||||
url: jdbc:postgresql://localhost:50001/sportshub_db
|
||||
username: sportshub_user
|
||||
password: password
|
||||
|
||||
Reference in New Issue
Block a user