Skip to content

Commit 56af0f5

Browse files
committed
Spring Security Fully implemented, and CI/CD pipeline for event-service is temporarily disabled for development phase
1 parent 8c331e9 commit 56af0f5

17 files changed

Lines changed: 478 additions & 74 deletions

File tree

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1-
name: Deploy Event Service to Amazon ECS
2-
3-
on:
4-
push:
5-
# branches: [ "main" ]
6-
# # PRO TIP: Only run this pipeline if files in the event-service folder change
7-
# paths:
8-
# - 'event-service/**'
9-
10-
env:
11-
AWS_REGION: us-east-1
12-
ECR_REPOSITORY: event-service
13-
ECS_SERVICE: event-service
14-
ECS_CLUSTER: stream-seat-cluster
15-
CONTAINER_NAME: event-service-container
16-
17-
jobs:
18-
deploy:
19-
runs-on: ubuntu-latest
20-
21-
# This tells GitHub to run all 'run' commands inside the event-service directory
22-
defaults:
23-
run:
24-
working-directory: ./event-service
25-
26-
steps:
27-
- name: Checkout Code
28-
uses: actions/checkout@v4
29-
30-
- name: Set up JDK 17
31-
uses: actions/setup-java@v3
32-
with:
33-
java-version: '17'
34-
distribution: 'temurin'
35-
cache: maven
36-
37-
- name: Build JAR with Maven
38-
run: mvn clean package -DskipTests
39-
40-
- name: Configure AWS credentials
41-
uses: aws-actions/configure-aws-credentials@v1
42-
with:
43-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
44-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
45-
aws-region: ${{ env.AWS_REGION }}
46-
47-
- name: Login to Amazon ECR
48-
id: login-ecr
49-
uses: aws-actions/amazon-ecr-login@v1
50-
51-
- name: Build, Tag, and Push Image to ECR
52-
env:
53-
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
54-
run: |
55-
# buildx is used for AMD64 compatibility on Fargate
56-
docker buildx create --use
57-
docker buildx build --platform linux/amd64 \
58-
-t $ECR_REGISTRY/$ECR_REPOSITORY:latest \
59-
--push .
60-
61-
- name: Force ECS Deployment
62-
run: |
63-
aws ecs update-service --cluster ${{ env.ECS_CLUSTER }} \
64-
--service ${{ env.ECS_SERVICE }} \
65-
--force-new-deployment
1+
#name: Deploy Event Service to Amazon ECS
2+
#
3+
#on:
4+
# push:
5+
## branches: [ "main" ]
6+
## # PRO TIP: Only run this pipeline if files in the event-service folder change
7+
## paths:
8+
## - 'event-service/**'
9+
#
10+
#env:
11+
# AWS_REGION: us-east-1
12+
# ECR_REPOSITORY: event-service
13+
# ECS_SERVICE: event-service
14+
# ECS_CLUSTER: stream-seat-cluster
15+
# CONTAINER_NAME: event-service-container
16+
#
17+
#jobs:
18+
# deploy:
19+
# runs-on: ubuntu-latest
20+
#
21+
# # This tells GitHub to run all 'run' commands inside the event-service directory
22+
# defaults:
23+
# run:
24+
# working-directory: ./event-service
25+
#
26+
# steps:
27+
# - name: Checkout Code
28+
# uses: actions/checkout@v4
29+
#
30+
# - name: Set up JDK 17
31+
# uses: actions/setup-java@v3
32+
# with:
33+
# java-version: '17'
34+
# distribution: 'temurin'
35+
# cache: maven
36+
#
37+
# - name: Build JAR with Maven
38+
# run: mvn clean package -DskipTests
39+
#
40+
# - name: Configure AWS credentials
41+
# uses: aws-actions/configure-aws-credentials@v1
42+
# with:
43+
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
44+
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
45+
# aws-region: ${{ env.AWS_REGION }}
46+
#
47+
# - name: Login to Amazon ECR
48+
# id: login-ecr
49+
# uses: aws-actions/amazon-ecr-login@v1
50+
#
51+
# - name: Build, Tag, and Push Image to ECR
52+
# env:
53+
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
54+
# run: |
55+
# # buildx is used for AMD64 compatibility on Fargate
56+
# docker buildx create --use
57+
# docker buildx build --platform linux/amd64 \
58+
# -t $ECR_REGISTRY/$ECR_REPOSITORY:latest \
59+
# --push .
60+
#
61+
# - name: Force ECS Deployment
62+
# run: |
63+
# aws ecs update-service --cluster ${{ env.ECS_CLUSTER }} \
64+
# --service ${{ env.ECS_SERVICE }} \
65+
# --force-new-deployment

‎.idea/encodings.xml‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.streamseat.apigateway.utils;
22

3+
import io.jsonwebtoken.Claims;
34
import io.jsonwebtoken.Jwts;
45
import io.jsonwebtoken.SignatureAlgorithm;
56
import io.jsonwebtoken.io.Decoders;
@@ -10,37 +11,60 @@
1011
import java.util.Date;
1112
import java.util.HashMap;
1213
import java.util.Map;
14+
import java.util.function.Function;
1315

1416
@Component
1517
public class JwtUtil {
16-
// In production, move this to AWS Secrets Manager or environment variables
18+
1719
private static final String SECRET = "5367566B59703373367639792F423F4528482B4D6251655468576D5A71347437";
1820

1921
public void validateToken(final String token) {
2022
Jwts.parserBuilder().setSigningKey(getSignKey()).build().parseClaimsJws(token);
2123
}
2224

25+
// This now returns the User ID (Subject)
2326
public String extractUserId(String token) {
24-
return Jwts.parserBuilder().setSigningKey(getSignKey()).build()
25-
.parseClaimsJws(token).getBody().getSubject();
27+
return extractClaim(token, Claims::getSubject);
28+
}
29+
30+
// New helper to extract the Username from custom claims
31+
public String extractUserName(String token) {
32+
return extractAllClaims(token).get("username", String.class);
33+
}
34+
35+
// Generic helper to extract specific claims
36+
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
37+
final Claims claims = extractAllClaims(token);
38+
return claimsResolver.apply(claims);
39+
}
40+
41+
private Claims extractAllClaims(String token) {
42+
return Jwts.parserBuilder()
43+
.setSigningKey(getSignKey())
44+
.build()
45+
.parseClaimsJws(token)
46+
.getBody();
2647
}
2748

2849
private Key getSignKey() {
2950
byte[] keyBytes = Decoders.BASE64.decode(SECRET);
3051
return Keys.hmacShaKeyFor(keyBytes);
3152
}
3253

33-
public String generateToken(String userName) {
54+
// Updated to accept both ID and Name
55+
public String generateToken(String userId, String userName) {
3456
Map<String, Object> claims = new HashMap<>();
35-
return createToken(claims, userName);
57+
claims.put("username", userName); // Add username as extra info
58+
return createToken(claims, userId); // Use ID as the subject
3659
}
3760

38-
private String createToken(Map<String, Object> claims, String userName) {
61+
private String createToken(Map<String, Object> claims, String userId) {
3962
return Jwts.builder()
4063
.setClaims(claims)
41-
.setSubject(userName)
64+
.setSubject(userId) // Subject is now the Unique ID
4265
.setIssuedAt(new Date(System.currentTimeMillis()))
4366
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 30))
44-
.signWith(getSignKey(), SignatureAlgorithm.HS256).compact();
67+
.signWith(getSignKey(), SignatureAlgorithm.HS256)
68+
.compact();
4569
}
46-
}
70+
}

‎auth-service/pom.xml‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.streamseat</groupId>
9+
<artifactId>stream-seat</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>auth-service</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
<name>auth-service</name>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-security</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.mysql</groupId>
34+
<artifactId>mysql-connector-j</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.cloud</groupId>
40+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<optional>true</optional>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>io.jsonwebtoken</groupId>
51+
<artifactId>jjwt-api</artifactId>
52+
<version>0.11.5</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>io.jsonwebtoken</groupId>
56+
<artifactId>jjwt-impl</artifactId>
57+
<version>0.11.5</version>
58+
<scope>runtime</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.jsonwebtoken</groupId>
62+
<artifactId>jjwt-jackson</artifactId>
63+
<version>0.11.5</version>
64+
<scope>runtime</scope>
65+
</dependency>
66+
</dependencies>
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.springframework.boot</groupId>
72+
<artifactId>spring-boot-maven-plugin</artifactId>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.streamseat;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
System.out.println("Hello, World!");
6+
}
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.streamseat.authservice;
2+
3+
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7+
8+
@SpringBootApplication
9+
@EnableDiscoveryClient
10+
public class AuthServiceApplication {
11+
public static void main(String[] args) {
12+
SpringApplication.run(AuthServiceApplication.class, args);
13+
}
14+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.streamseat.authservice.config;
2+
3+
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.authentication.AuthenticationManager;
7+
import org.springframework.security.authentication.AuthenticationProvider;
8+
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
9+
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
10+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
11+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
12+
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
13+
import org.springframework.security.core.userdetails.UserDetailsService;
14+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
15+
import org.springframework.security.crypto.password.PasswordEncoder;
16+
import org.springframework.security.web.SecurityFilterChain;
17+
18+
@Configuration
19+
@EnableWebSecurity
20+
public class AuthConfig {
21+
22+
// 1. Password Encoder (used by AuthService to hash passwords)
23+
@Bean
24+
public PasswordEncoder passwordEncoder() {
25+
return new BCryptPasswordEncoder();
26+
}
27+
28+
// 2. Security Filter Chain (Defines which routes are open)
29+
@Bean
30+
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
31+
return http.csrf(AbstractHttpConfigurer::disable)
32+
.authorizeHttpRequests(auth -> auth
33+
.requestMatchers("/auth/register", "/auth/token", "/auth/validate").permitAll()
34+
.anyRequest().authenticated()
35+
)
36+
.build();
37+
}
38+
39+
// 3. Authentication Manager (Used in AuthController to verify login)
40+
@Bean
41+
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
42+
return config.getAuthenticationManager();
43+
}
44+
45+
// 4. Link with your CustomUserDetailsService
46+
@Bean
47+
public AuthenticationProvider authenticationProvider(UserDetailsService userDetailsService) {
48+
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
49+
authenticationProvider.setUserDetailsService(userDetailsService);
50+
authenticationProvider.setPasswordEncoder(passwordEncoder());
51+
return authenticationProvider;
52+
}
53+
}

0 commit comments

Comments
 (0)