Tested KeyCloak validation - AUTH
Test Keycloak JWT validation against access token issued via NextAuth; ensure Spring Boot resource server correctly validates token signature, expiration, issuer, and optional audience.
This commit is contained in:
1 parent
92971a3983
commit
7306ad1b26
18 files changed
+623
-66
No files matched your search
@@ -8,6 +8,11 @@ public class BackendApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BackendApplication.class, args);
|
||||
//call the test
|
||||
test t = new test();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.envsync.backend;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeHttpRequests(auth -> auth
|
||||
.requestMatchers("/api/secure").authenticated()
|
||||
.anyRequest().permitAll()
|
||||
)
|
||||
.oauth2ResourceServer(oauth2 -> oauth2
|
||||
.jwt(Customizer.withDefaults()) // uses issuer-uri/jwk-set-uri
|
||||
);
|
||||
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.envsync.backend;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class test {
|
||||
|
||||
@GetMapping("/api/secure")
|
||||
public String secure() {
|
||||
//send json response
|
||||
return "{\"message\": \"This is a secure endpoint!\"}";
|
||||
}
|
||||
}
|
||||
@@ -1 +1,32 @@
|
||||
spring.application.name=backend
|
||||
# ----- Postgres -----
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
|
||||
spring.datasource.username=admin
|
||||
spring.datasource.password=admin
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
# ----- Redis -----
|
||||
spring.data.redis.host=localhost
|
||||
spring.data.redis.port=6379
|
||||
|
||||
# ----- MinIO -----
|
||||
minio.url=http://localhost:9000
|
||||
minio.access-key=minioadmin
|
||||
minio.secret-key=minioadmin
|
||||
minio.bucket=envsync-backups
|
||||
|
||||
# ----- Keycloak -----
|
||||
# Set audience to your Keycloak client-id to enforce aud validation (optional).
|
||||
app.security.jwt.audience=envsync-app
|
||||
# Allow clock skew in seconds for exp/nbf validation.
|
||||
app.security.jwt.clock-skew-seconds=60
|
||||
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8180/realms/envsync
|
||||
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://localhost:8180/realms/envsync/protocol/openid-connect/certs
|
||||
logging.level.org.springframework.security=DEBUG
|
||||
logging.level.org.springframework.security.oauth2=DEBUG
|
||||
# ----- Server port -----
|
||||
server.port=8081
|
||||
Reference in new issue
Block a user