Skip to content

Commit 03df577

Browse files
committed
ELK image
1 parent 56063f6 commit 03df577

File tree

14 files changed

+409
-55
lines changed

14 files changed

+409
-55
lines changed

.github/workflows/main.yaml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
name: Continuous Integration for Comment Service
1+
name: Continuous Integration for User Service
22

33
on:
44
push:
55
branches:
6-
- ducbao
6+
- QA
77

88
jobs:
99
testing:
@@ -26,24 +26,24 @@ jobs:
2626
- name: Unit Tests
2727
run: mvn -B test --file pom.xml
2828

29-
# sonar-cloud-scan:
30-
# needs: testing
31-
# uses: ./.github/workflows/SonarQube.yaml
32-
# secrets:
33-
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
29+
# sonar-cloud-scan:
30+
# needs: testing
31+
# uses: ./.github/workflows/SonarQube.yaml
32+
# secrets:
33+
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
3434

3535
build-image:
3636
needs: testing
3737
uses: ./.github/workflows/build-image.yaml
3838
secrets:
3939
DOCKER_HUB_ACCESS_TOKEN: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
4040

41-
# scan-image:
42-
# needs: build-image
43-
# uses: ./.github/workflows/scan-image.yaml
41+
# scan-image:
42+
# needs: build-image
43+
# uses: ./.github/workflows/scan-image.yaml
4444

45-
# notify:
46-
# needs: scan-image
47-
# uses: ./.github/workflows/notifyCI.yaml
48-
# secrets:
49-
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
45+
# notify:
46+
# needs: scan-image
47+
# uses: ./.github/workflows/notifyCI.yaml
48+
# secrets:
49+
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ build/
3131

3232
### VS Code ###
3333
.vscode/
34+
#logback-*.xml

pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,39 @@
8282
<artifactId>gson</artifactId>
8383
<version>2.10.1</version>
8484
</dependency>
85+
<dependency>
86+
<groupId>io.jsonwebtoken</groupId>
87+
<artifactId>jjwt-api</artifactId>
88+
<version>0.11.5</version> <!-- Hoặc phiên bản mới nhất -->
89+
</dependency>
90+
<dependency>
91+
<groupId>io.jsonwebtoken</groupId>
92+
<artifactId>jjwt-impl</artifactId>
93+
<version>0.11.5</version>
94+
<scope>runtime</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>io.jsonwebtoken</groupId>
98+
<artifactId>jjwt-jackson</artifactId> <!-- Hoặc jjwt-gson nếu bạn dùng Gson -->
99+
<version>0.11.5</version>
100+
<scope>runtime</scope>
101+
</dependency>
102+
103+
104+
105+
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
106+
<dependency>
107+
<groupId>redis.clients</groupId>
108+
<artifactId>jedis</artifactId>
109+
<version>5.1.0</version>
110+
</dependency>
111+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
112+
<dependency>
113+
<groupId>org.springframework.boot</groupId>
114+
<artifactId>spring-boot-starter-data-redis</artifactId>
115+
<version>3.2.4</version>
116+
</dependency>
117+
85118

86119
<dependency>
87120
<groupId>net.logstash.logback</groupId>

resources.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: user-service
5+
labels:
6+
app: user-service
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: user-service
12+
template:
13+
metadata:
14+
labels:
15+
app: user-service
16+
spec:
17+
containers:
18+
- name: user-service
19+
image: datuits/devops-user-service:latest
20+
ports:
21+
- containerPort: 8081
22+
env:
23+
- name: SPRING_DATA_MONGODB_URI
24+
value: mongodb://user-mongo-service:27017/user-service
25+
resources:
26+
requests:
27+
memory: "32Mi"
28+
cpu: "0.2"
29+
limits:
30+
memory: "64Mi"
31+
cpu: "0.4"
32+
---
33+
apiVersion: v1
34+
kind: Service
35+
metadata:
36+
name: user-service
37+
spec:
38+
selector:
39+
app: user-service
40+
ports:
41+
- protocol: TCP
42+
port: 8081
43+
targetPort: 8081
44+
type: NodePort
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.programming.userService.config;
2+
3+
import com.programming.userService.util.JwtUtil;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
6+
import org.springframework.security.core.context.SecurityContextHolder;
7+
import org.springframework.security.core.userdetails.UserDetails;
8+
import org.springframework.security.core.userdetails.UserDetailsService;
9+
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
10+
import org.springframework.stereotype.Component;
11+
import org.springframework.web.filter.OncePerRequestFilter;
12+
import jakarta.servlet.FilterChain;
13+
import jakarta.servlet.ServletException;
14+
import jakarta.servlet.http.HttpServletRequest;
15+
import jakarta.servlet.http.HttpServletResponse;
16+
17+
import java.io.IOException;
18+
19+
@Component
20+
public class JwtRequestFilter extends OncePerRequestFilter {
21+
22+
@Autowired
23+
private UserDetailsService userDetailsService;
24+
25+
@Autowired
26+
private JwtUtil jwtUtil;
27+
28+
@Override
29+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
30+
throws ServletException, IOException {
31+
32+
final String authorizationHeader = request.getHeader("Authorization");
33+
34+
String username = null;
35+
String jwt = null;
36+
37+
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
38+
jwt = authorizationHeader.substring(7);
39+
username = jwtUtil.extractUsername(jwt);
40+
}
41+
42+
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
43+
44+
UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
45+
46+
if (jwtUtil.validateToken(jwt, userDetails)) {
47+
48+
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
49+
userDetails, null, userDetails.getAuthorities());
50+
usernamePasswordAuthenticationToken
51+
.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
52+
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
53+
}
54+
}
55+
chain.doFilter(request, response);
56+
}
57+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.programming.userService.config;
2+
3+
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
4+
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.data.redis.connection.RedisConnectionFactory;
8+
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
9+
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
10+
import org.springframework.data.redis.core.RedisTemplate;
11+
import org.springframework.data.redis.serializer.StringRedisSerializer;
12+
import org.springframework.beans.factory.annotation.Value;
13+
@Configuration
14+
@EnableRedisRepositories
15+
public class RedisConfig {
16+
17+
@Value("${spring.redis.host}")
18+
private String redisHost;
19+
20+
@Value("${spring.redis.port}")
21+
private int redisPort;
22+
23+
@Value("${spring.redis.password}")
24+
private String redisPassword;
25+
26+
@Bean
27+
public RedisConnectionFactory redisConnectionFactory() {
28+
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
29+
redisConfig.setHostName(redisHost);
30+
redisConfig.setPort(redisPort);
31+
redisConfig.setPassword(redisPassword);
32+
33+
return new LettuceConnectionFactory(redisConfig);
34+
}
35+
36+
37+
@Bean
38+
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
39+
RedisTemplate<String, Object> template = new RedisTemplate<>();
40+
template.setConnectionFactory(connectionFactory);
41+
template.setKeySerializer(new StringRedisSerializer());
42+
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
43+
return template;
44+
}
45+
}

src/main/java/com/programming/userService/config/SecurityConfig.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.security.crypto.password.PasswordEncoder;
1313
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
1414
import org.springframework.security.web.SecurityFilterChain;
15+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1516

1617
import java.util.ArrayList;
1718
import java.util.Collection;
@@ -21,25 +22,26 @@ public class SecurityConfig {
2122

2223
@Bean
2324
public SecurityFilterChain defaultFilterChain(HttpSecurity httpSecurity) throws Exception {
25+
JwtRequestFilter jwtRequestFilter = new JwtRequestFilter();
2426
return httpSecurity
2527
.csrf(csrf -> csrf.disable())
26-
.authorizeHttpRequests(auth -> auth.requestMatchers("/register", "/error").permitAll()
27-
.requestMatchers("/listUser").permitAll()
28-
.requestMatchers("/video/upload").permitAll()
29-
.requestMatchers("/video/list").permitAll()
30-
.requestMatchers("/file/upload").permitAll()
31-
.requestMatchers("/file/list").permitAll()
32-
.requestMatchers("/file/downloadZipFile").permitAll()
33-
.requestMatchers("/comments/upload").permitAll()
34-
.requestMatchers("/comments/**").permitAll()
35-
.requestMatchers("/video/**").permitAll()
36-
.requestMatchers("/login2").permitAll()
37-
.requestMatchers("/listUserbyId/**").permitAll()
38-
.requestMatchers("/updateProfile/**").permitAll()
39-
.requestMatchers("/send-verification-email").permitAll()
40-
.requestMatchers("/hello-world").permitAll()
41-
.requestMatchers("/**").permitAll()
28+
.authorizeHttpRequests(auth -> auth.requestMatchers("/user/register", "/user/error").permitAll()
29+
// .requestMatchers("/listUser").permitAll()
30+
.requestMatchers("/user/login2").permitAll()
31+
.requestMatchers("/user/login3").permitAll()
32+
.requestMatchers("/user/listUserbyId/**").permitAll()
33+
.requestMatchers("/user/hello-world").permitAll()
34+
.requestMatchers("/user/send-verification-email").permitAll()
35+
.requestMatchers("/user/logout").permitAll()
36+
.requestMatchers("/user/listUserbyUsername").permitAll()
37+
.requestMatchers("/user/listUserbyId/**").permitAll()
38+
.requestMatchers("/user/updateProfile/**").permitAll()
39+
.requestMatchers("/user/changePassword/**").permitAll()
40+
.requestMatchers("/user/listUser").permitAll()
41+
42+
.requestMatchers("/user/").authenticated()
4243
.anyRequest().authenticated())
44+
.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class)
4345
.httpBasic(Customizer.withDefaults())
4446
.formLogin(Customizer.withDefaults())
4547
.build();

0 commit comments

Comments
 (0)