-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add security and swagger in inventory (#75)
- Loading branch information
Showing
9 changed files
with
165 additions
and
6 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
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
25 changes: 25 additions & 0 deletions
25
inventory/src/main/java/com/fjb/inventory/config/CorsConfig.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,25 @@ | ||
package com.fjb.inventory.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class CorsConfig { | ||
|
||
@Value("${cors.allowed}") | ||
private String corsAllowed; | ||
|
||
@Bean | ||
public WebMvcConfigurer corsConfigure() { | ||
return new WebMvcConfigurer() { | ||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**").allowedMethods(corsAllowed) | ||
.allowedOrigins(corsAllowed).allowedHeaders(corsAllowed); | ||
} | ||
}; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
inventory/src/main/java/com/fjb/inventory/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,44 @@ | ||
package com.fjb.inventory.config; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.security.config.Customizer; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
@Configuration | ||
public class SecurityConfig { | ||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
|
||
return http | ||
.authorizeHttpRequests(auth -> auth | ||
.anyRequest().permitAll()) | ||
.oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults())) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public JwtAuthenticationConverter jwtAuthenticationConverterForKeycloak() { | ||
Converter<Jwt, Collection<GrantedAuthority>> jwtGrantedAuthoritiesConverter = jwt -> { | ||
Map<String, Collection<String>> realmAccess = jwt.getClaim("realm_access"); | ||
Collection<String> roles = realmAccess.get("roles"); | ||
return roles.stream() | ||
.map(role -> new SimpleGrantedAuthority("ROLE_" + role)) | ||
.collect(Collectors.toList()); | ||
}; | ||
|
||
var jwtAuthenticationConverter = new JwtAuthenticationConverter(); | ||
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter); | ||
|
||
return jwtAuthenticationConverter; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
inventory/src/main/java/com/fjb/inventory/config/SwaggerConfig.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,32 @@ | ||
package com.fjb.inventory.config; | ||
|
||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; | ||
import io.swagger.v3.oas.annotations.info.Info; | ||
import io.swagger.v3.oas.annotations.security.OAuthFlow; | ||
import io.swagger.v3.oas.annotations.security.OAuthFlows; | ||
import io.swagger.v3.oas.annotations.security.OAuthScope; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.security.SecurityScheme; | ||
import io.swagger.v3.oas.annotations.servers.Server; | ||
|
||
@OpenAPIDefinition( | ||
info = @Info( | ||
title = "Inventory Service API", | ||
description = "Inventory API documentation", | ||
version = "1.0" | ||
), | ||
security = @SecurityRequirement(name = "oauth2_bearer"), | ||
servers = { | ||
@Server(url = "${server.servlet.context-path}", | ||
description = "Default Server URL") | ||
}) | ||
@SecurityScheme(name = "oauth2_bearer", type = SecuritySchemeType.OAUTH2, | ||
flows = @OAuthFlows( | ||
authorizationCode = @OAuthFlow( | ||
authorizationUrl = "${springdoc.oauthflow.authorization-url}", | ||
tokenUrl = "${springdoc.oauthflow.token-url}", | ||
scopes = {@OAuthScope(name = "openid", description = "openid") | ||
}))) | ||
public class SwaggerConfig { | ||
} |
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
spring: | ||
application: | ||
name: inventory | ||
datasource: | ||
url: ${INVENTORY_DATASOURCE_URL} | ||
username: ${INVENTORY_DATA_USERNAME} | ||
password: ${INVENTORY_DATA_PASSWORD} | ||
driver-class-name: org.postgresql.Driver | ||
|
||
jpa: | ||
hibernate: | ||
ddl-auto: update | ||
show-sql: true | ||
properties: | ||
hibernate: | ||
format_sql: false | ||
database: postgresql | ||
database-platform: org.hibernate.dialect.PostgreSQLDialect | ||
open-in-view: false | ||
|
||
security: | ||
oauth2: | ||
resourceserver: | ||
jwt: | ||
issuer-uri: http://localhost:8080/realms/Matcha | ||
springdoc: | ||
oauthflow: | ||
authorization-url: http://localhost:8080/realms/Matcha/protocol/openid-connect/auth | ||
token-url: http://localhost:8080/realms/Matcha/protocol/openid-connect/token | ||
swagger-ui: | ||
oauth: | ||
client-id: swagger | ||
use-pkce-with-authorization-code-grant: true | ||
packagesToScan: com.fjb.inventory | ||
path: /swagger-ui | ||
|
||
server: | ||
port: 8082 | ||
servlet: | ||
context-path: /inventory | ||
cors: | ||
allowed: "*" | ||
|
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