Skip to main content

r/SpringBoot

members
online

How to setup Sprign Authorization Server with Jdbc and proxy using gateway oauth2 client to access resource server module How to setup Sprign Authorization Server with Jdbc and proxy using gateway oauth2 client to access resource server module
Question

I am working on an application to better understand the spring security and microservcies architecture

i have setup:

gateway - module with gateway, oauth2 client, jdbc and psql driver and web dependencies

auth - module with oauth2 authorization server and web dependencies

problems-service with web, jdbc, psql driver, oauth2 resource server dependencies

auth module security config

class AuthSecurityConfiguration {

    
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf(AbstractHttpConfigurer::disable)
                .oauth2AuthorizationServer(as -> as.oidc(withDefaults()))
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/", "/register").permitAll()
                        .requestMatchers("/login").permitAll()
                        .anyRequest().authenticated())
                .formLogin(withDefaults());

        return http.build();
    }

    
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    
    JdbcUserDetailsManager jdbcUserDetailsManager(DataSource dataSource) {
        return new JdbcUserDetailsManager(dataSource);
    }

    
    RegisteredClientRepository registeredClientRepository(JdbcOperations jdbcOperations) {

        JdbcRegisteredClientRepository jdbcRegisteredClientRepository = new JdbcRegisteredClientRepository(jdbcOperations);

        RegisteredClient registeredClient = RegisteredClient
                .withId("gateway-client")
                .clientId("gateway")
                .clientSecret(passwordEncoder().encode("secret"))
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                .redirectUri("http://localhost:8000/login/oauth2/code/gateway")
                .scope("openid")
                .scope("problems.read")
                .build();

        if (jdbcRegisteredClientRepository.findByClientId("gateway") == null) {
            jdbcRegisteredClientRepository.save(registeredClient);
        }

        return jdbcRegisteredClientRepository;
    }

}

auth module app.yml

spring:
  application:
    name: auth
  datasource:
    url: jdbc:postgresql://localhost:5432/db
    username: user
    password: pass
  sql:
    init:
      mode: always
server:
  port: 8002
logging:
  level:
    org.springframework.security: TRACE

Gateway security config:

public class GateSecurityConfig {

    
    public SecurityFilterChain securityFilterChain(HttpSecurity http) {
        http
                .csrf(AbstractHttpConfigurer::disable)
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/").permitAll()
                        .requestMatchers("/login").permitAll()
                        .anyRequest().authenticated())
                .oauth2Login(Customizer.withDefaults())
                .oauth2Client(Customizer.withDefaults());

        return http.build();
    }
}

Gateway app.yml

spring:
  application:
    name: gateway
  security:
    oauth2:
      client:
        registration:
          gateway:
            provider: auth
            client-id: gateway
            client-secret: secret
            authorization-grant-type: authorization_code
            client-authentication-method: client_secret_basic
            redirect-uri: "http://localhost:8000/login/oauth2/code/{registrationId}"
            scope:
              - openid
        provider:
          auth:
            issuer-uri: "http://localhost:8002"

server:
  port: 8000
logging:
  level:
    org:
      springframework:
        security: TRACE

gateway module redirect logic:

n
public class GatewayApplication {

    static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    
    (Ordered.HIGHEST_PRECEDENCE)
    RouterFunction<ServerResponse> backendRoutes(){
        return route ()
                .before(BeforeFilterFunctions.uri("http://localhost:8001/"))
                .before(BeforeFilterFunctions.rewritePath("/problems/", "/"))
                .filter(TokenRelayFilterFunctions.tokenRelay())
                .GET("/problems/**", http())
                .build();
    }

    
     am working on an application to better understand the spring security and microservcies architecture




i have setup:




gateway - module with gateway, oauth2 client, jdbc and psql driver and web dependencies




auth - module with oauth2 authorization server and web dependencies




problems-service with web, jdbc, psql driver, oauth2 resource server dependencies




auth module security config






class AuthSecurityConfiguration {

    
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf(AbstractHttpConfigurer::disable)
                .oauth2AuthorizationServer(as -> as.oidc(withDefaults()))
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/", "/register").permitAll()
                        .requestMatchers("/login").permitAll()
                        .anyRequest().authenticated())
                .formLogin(withDefaults());

        return http.build();
    }

    
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    
    JdbcUserDetailsManager jdbcUserDetailsManager(DataSource dataSource) {
        return new JdbcUserDetailsManager(dataSource);
    }

    
    RegisteredClientRepository registeredClientRepository(JdbcOperations jdbcOperations) {

        JdbcRegisteredClientRepository jdbcRegisteredClientRepository = new JdbcRegisteredClientRepository(jdbcOperations);

        RegisteredClient registeredClient = RegisteredClient
                .withId("gateway-client")
                .clientId("gateway")
                .clientSecret(passwordEncoder().encode("secret"))
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                .redirectUri("http://localhost:8000/login/oauth2/code/gateway")
                .scope("openid")
                .scope("problems.read")
                .build();

        if (jdbcRegisteredClientRepository.findByClientId("gateway") == null) {
            jdbcRegisteredClientRepository.save(registeredClient);
        }

        return jdbcRegisteredClientRepository;
    }

}




auth module app.yml



spring:
  application:
    name: auth
  datasource:
    url: jdbc:postgresql://localhost:5432/db
    username: user
    password: pass
  sql:
    init:
      mode: always
server:
  port: 8002
logging:
  level:
    org.springframework.security: TRACE




Gateway security config:





public class GateSecurityConfig {

    
    public SecurityFilterChain securityFilterChain(HttpSecurity http) {
        http
                .csrf(AbstractHttpConfigurer::disable)
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/").permitAll()
                        .requestMatchers("/login").permitAll()
                        .anyRequest().authenticated())
                .oauth2Login(Customizer.withDefaults())
                .oauth2Client(Customizer.withDefaults());

        return http.build();
    }
}




Gateway app.yml



spring:
  application:
    name: gateway
  security:
    oauth2:
      client:
        registration:
          gateway:
            provider: auth
            client-id: gateway
            client-secret: secret
            authorization-grant-type: authorization_code
            client-authentication-method: client_secret_basic
            redirect-uri: "http://localhost:8000/login/oauth2/code/{registrationId}"
            scope:
              - openid
        provider:
          auth:
            issuer-uri: "http://localhost:8002"

server:
  port: 8000
logging:
  level:
    org:
      springframework:
        security: TRACE




gateway module redirect logic:




n
public class GatewayApplication {

    static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    
    (Ordered.HIGHEST_PRECEDENCE)
    RouterFunction<ServerResponse> backendRoutes(){
        return route ()
                .before(BeforeFilterFunctions.uri("http://localhost:8001/"))
                .before(BeforeFilterFunctions.rewritePath("/problems/", "/"))
                .filter(TokenRelayFilterFunctions.tokenRelay())
                .GET("/problems/**", http())
                .build();
    }

    
    u/Order()
    RouterFunction<ServerResponse> frontendRoutes(){
        return route ()
                .before(BeforeFilterFunctions.uri("http://localhost:5173"))
                .GET("/**", http())
                .build();
    }
}




resource server app.yml file



spring:
  application:
    name: problems-service
  datasource:
    url: jdbc:postgresql://localhost:5432/db
    username: user
    password: pass
  security:
    oauth2:
      resourceserver:
        jwt:
          jwk-set-uri: http://localhost:8002
  sql:
    init:
      mode: always
server:
  port: 8001




The problem im running into is that when i hit my gateway i get 
redirected to the auth server endpoint which is 8002 as expected but 
when i authenticate with a user name and password that already existing 
in the datasource it then redirects me back to gateway where i am show 
an error of invalid credentials




i woudve provided trace logs but hit the word limit




If anyone please help me understand this security shabang as im very 
exhausted at this point not being able to figure this stuff out!




If you can please explain how to correctly implement the logic im 
trying here and show the example as well. Also if you can mention how to
 properly consume the gateway redirects as flow on the frontend


()
    RouterFunction<ServerResponse> frontendRoutes(){
        return route ()
                .before(BeforeFilterFunctions.uri("http://localhost:5173"))
                .GET("/**", http())
                .build();
    }
}

resource server app.yml file

spring:
  application:
    name: problems-service
  datasource:
    url: jdbc:postgresql://localhost:5432/db
    username: user
    password: pass
  security:
    oauth2:
      resourceserver:
        jwt:
          jwk-set-uri: http://localhost:8002
  sql:
    init:
      mode: always
server:
  port: 8001

The problem im running into is that when i hit my gateway i get redirected to the auth server endpoint which is 8002 as expected but when i authenticate with a user name and password that already existing in the datasource it then redirects me back to gateway where i am show an error of invalid credentials

i woudve provided trace logs but hit the word limit

If anyone please help me understand this security shabang as im very exhausted at this point not being able to figure this stuff out!

If you can please explain how to correctly implement the logic im trying here and show the example as well. Also if you can mention how to properly consume the gateway redirects as flow on the frontend


Get the care you need, when you need it with Amazon One Medical.
media poster


Built a Spotify-like backend with Spring Boot – Looking for feedback 🚀 Built a Spotify-like backend with Spring Boot – Looking for feedback 🚀
Discussion

TL;DR:
Built a Spotify-like backend using Spring Boot + JPA + PostgreSQL with songs, artists, albums, and playlists. Looking for feedback on architecture, service design, and ideas to make it more production-ready. Any suggestions welcome!

Hey everyone 👋

I built a music streaming backend (Spotify-style) using Spring Boot as a learning + portfolio project and would love some feedback.

SoundStream Backend Project

Tech Stack

  • Java 17, Spring Boot

  • Spring Data JPA (Hibernate)

  • PostgreSQL

  • Gradle, Postman

What it does

  • Manage Songs, Artists, Albums, Playlists

  • Many-to-Many & One-to-Many relationships

  • Create playlists, add/remove songs

  • Fetch songs by artist/album/playlist

What I want feedback on

  • Project structure & design

  • Service/repository layer quality

  • Should I switch fully to DTOs?

  • Features worth adding next

  • How to make it more production-ready

This project helped me learn real-world JPA issues (lazy loading, relationships, transactions).

Any suggestions or improvements are welcome. Thanks! 🙌