-
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.
Merge pull request #14 from ADPRO-C11/subbox-management
Subbox management
- Loading branch information
Showing
27 changed files
with
757 additions
and
394 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
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 |
---|---|---|
|
@@ -35,3 +35,12 @@ out/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ |
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,19 @@ | ||
version: '3.7' | ||
|
||
services: | ||
prometheus: | ||
image: prom/prometheus:v2.44.0 | ||
container_name: prometheus | ||
ports: | ||
- "9090:9090" | ||
volumes: | ||
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml | ||
|
||
grafana: | ||
image: grafana/grafana:9.5.2 | ||
container_name: grafana | ||
ports: | ||
- "3000:3000" | ||
restart: unless-stopped | ||
volumes: | ||
- ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources |
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 @@ | ||
apiVersion: 1 | ||
datasources: | ||
- name: Prometheus | ||
type: prometheus | ||
access: proxy | ||
url: http://prometheus:9090 | ||
isDefault: true |
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,12 @@ | ||
global: | ||
scrape_interval: 15s # By default, scrape targets every 15 seconds. | ||
evaluation_interval: 15s # By default, scrape rules every 15 seconds. | ||
|
||
scrape_configs: | ||
- job_name: 'MyAppMetrics' | ||
metrics_path: '/actuator/prometheus' | ||
scrape_interval: 3s | ||
static_configs: | ||
- targets: ['34.124.168.155:80'] | ||
labels: | ||
application: 'snackscription_subscriptionbox' |
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
48 changes: 48 additions & 0 deletions
48
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/config/JWTAuthFilter.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,48 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.config; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.utils.JWTUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
@Component | ||
public class JWTAuthFilter extends OncePerRequestFilter { | ||
|
||
private final JWTUtils jwtUtils; | ||
|
||
public JWTAuthFilter(JWTUtils jwtUtils) { | ||
this.jwtUtils = jwtUtils; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
final String authHeader = request.getHeader("Authorization"); | ||
final String jwtToken; | ||
|
||
if (authHeader == null || authHeader.isBlank()) { | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
jwtToken = authHeader.substring(7); | ||
|
||
if (jwtUtils.isTokenValid(jwtToken)) { | ||
String role = jwtUtils.extractRole(jwtToken); | ||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( | ||
null, null, Collections.singletonList(() -> role)); | ||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/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,37 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
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; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.utils.JWTUtils; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
|
||
private final JWTUtils jwtUtils; | ||
|
||
public SecurityConfig(JWTUtils jwtUtils) { | ||
this.jwtUtils = jwtUtils; | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { | ||
httpSecurity | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.authorizeHttpRequests(authorizeRequests -> | ||
authorizeRequests | ||
.requestMatchers("/actuator/prometheus").permitAll() // Allow unauthenticated access | ||
.requestMatchers("/subscription-box/**", "/public/**").permitAll() | ||
.anyRequest().authenticated()) | ||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
.addFilterBefore(new JWTAuthFilter(jwtUtils), UsernamePasswordAuthenticationFilter.class); | ||
|
||
return httpSecurity.build(); | ||
} | ||
} |
Oops, something went wrong.