Web Socket 1

This commit is contained in:
Super User 2025-08-01 16:55:29 +05:30
parent 0108c37c0b
commit 4a7e68dd4f
9 changed files with 86 additions and 7 deletions

View File

@ -1,6 +1,6 @@
export class ApplicationHttpRouts { export class ApplicationHttpRouts {
private static readonly PROTOCOL: string = 'http'; private static readonly PROTOCOL: string = 'http';
private static readonly BACKEND_SERVER: string = '192.168.0.100'; private static readonly BACKEND_SERVER: string = '10.131.90.167';
private static readonly FRONT_PORT_NUMBER: string = '4200'; private static readonly FRONT_PORT_NUMBER: string = '4200';
private static readonly PORT_NUMBER: string = '8083'; private static readonly PORT_NUMBER: string = '8083';
private static readonly SOCKET: string = `${this.PROTOCOL}://${this.BACKEND_SERVER}:${this.PORT_NUMBER}`; private static readonly SOCKET: string = `${this.PROTOCOL}://${this.BACKEND_SERVER}:${this.PORT_NUMBER}`;

2
docker/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# Ignore database data directory
/db_data/

View File

@ -31,7 +31,7 @@ services:
spring: spring:
#image: mathewfrancisv/spring_back_postgres:v1.0.0 #image: mathewfrancisv/spring_back_postgres:v1.0.0
image: mathewfrancisv/btc_cezen_backend:v1.0.0 image: mathewfrancisv/btc_cezen_backend:v1.0.2
container_name: spring_app container_name: spring_app
ports: ports:
- "8083:8080" - "8083:8080"
@ -41,7 +41,7 @@ services:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/horse SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/horse
SPRING_DATASOURCE_USERNAME: postgres # Ensure this matches POSTGRES_USER SPRING_DATASOURCE_USERNAME: postgres # Ensure this matches POSTGRES_USER
SPRING_DATASOURCE_PASSWORD: root # Ensure this matches POSTGRES_PASSWORD SPRING_DATASOURCE_PASSWORD: root # Ensure this matches POSTGRES_PASSWORD
SPRING_DATASOURCE_CORSIP: http://192.168.0.100:4200 SPRING_DATASOURCE_CORSIP: http://10.131.90.167:4200
#network_mode: host #network_mode: host
networks: networks:
- app_network - app_network

View File

@ -55,6 +55,12 @@
<!-- <artifactId>mariadb-java-client</artifactId>--> <!-- <artifactId>mariadb-java-client</artifactId>-->
<!-- <scope>runtime</scope>--> <!-- <scope>runtime</scope>-->
<!-- </dependency>--> <!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>

View File

@ -91,7 +91,7 @@ public class CezenRoutsSecurityChain {
//.csrf(AbstractHttpConfigurer::disable) //.csrf(AbstractHttpConfigurer::disable)
.csrf((csrf) -> .csrf((csrf) ->
csrf.csrfTokenRequestHandler(requestHandler). csrf.csrfTokenRequestHandler(requestHandler).
ignoringRequestMatchers("/open/signup","/user/getXSRfToken","/user/ping") ignoringRequestMatchers("/open/signup","/user/getXSRfToken","/user/ping","/websocket","/websocket/**")
//.csrfTokenRepository(new CookieCsrfTokenRepository()) //.csrfTokenRepository(new CookieCsrfTokenRepository())
.csrfTokenRepository(cookieCsrfTokenRepo) .csrfTokenRepository(cookieCsrfTokenRepo)
) )
@ -111,11 +111,13 @@ public class CezenRoutsSecurityChain {
"/btc/get_race_card", "/btc/get_race_card",
"/cezen/set_aors", "/cezen/set_aors",
"/cezen/set_password", "/cezen/set_password",
"/cezen/add_extension" "/cezen/add_extension",
"/test/crash"
).hasAnyRole("admin") ).hasAnyRole("admin")
//any one who is authenticated can access /logout //any one who is authenticated can access /logout
.requestMatchers("/user/getXSRfToken","/user/ping", "/logout").authenticated() .requestMatchers("/user/getXSRfToken","/user/ping", "/logout").authenticated()
//all the rest are open to public //all the rest are open to public
.requestMatchers("/websocket", "/websocket/**").permitAll()
.requestMatchers( "/open/login").permitAll() .requestMatchers( "/open/login").permitAll()
) )
// redirect to /login if the user is not authenticated Customizer.withDefaults() enables a security feature using the defaults provided by Spring Security // redirect to /login if the user is not authenticated Customizer.withDefaults() enables a security feature using the defaults provided by Spring Security

View File

@ -0,0 +1,27 @@
package com.example.cezenBTC.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
// written by mathew francis
// this class is the configuration for a web socket
@Configuration
@EnableWebSocketMessageBroker
public class CezenWebSocketConfig implements WebSocketMessageBrokerConfigurer{
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic"); // Enables a simple memory-based message broker to carry messages back to the client on destinations prefixed with /topic
config.setApplicationDestinationPrefixes("/app"); // Prefix for messages from client to backend
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket").setAllowedOrigins("*"); // Native WebSocket
registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS(); // Registers "/websocket" endpoint and enables SockJS fallback
}
}

View File

@ -0,0 +1,23 @@
package com.example.cezenBTC.controller;
import com.example.cezenBTC.service.BtcWebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestApi {
@Autowired
BtcWebSocketService btcWebSocketService;
@GetMapping("/crash")
String breakConnection(){
btcWebSocketService.sendServerDownNotification();
return "Backend error";
}
}

View File

@ -79,8 +79,10 @@ public class JWTTokenValidatorFilter extends OncePerRequestFilter {
protected boolean shouldNotFilter(HttpServletRequest request) { protected boolean shouldNotFilter(HttpServletRequest request) {
return request.getServletPath().equals("/open/signup") return request.getServletPath().equals("/open/signup")
|| request.getServletPath().equals("/open/login"); || request.getServletPath().equals("/open/login")
|| request.getServletPath().equals("/websocket/**")
|| request.getServletPath().equals("/websocket");
// //bellow was done to archive this /exposed/** // //bellow was done to archive this /exposed/**
// request.getServletPath().split("/")[1].equals("exposed"); // request.getServletPath().split("/")[1].equals("exposed"); /websocket/**
} }
} }

View File

@ -0,0 +1,17 @@
package com.example.cezenBTC.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
@Service
public class BtcWebSocketService {
@Autowired
private SimpMessagingTemplate template;
public void sendServerDownNotification() {
template.convertAndSend("/topic/server-status", "DOWN");
}
}