Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger configuration #22

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ SOFTWARE.
<spring-addons.version>7.1.8</spring-addons.version>
<jcabi-github.version>1.8.0</jcabi-github.version>
<jersey-common.version>3.1.2</jersey-common.version>
<springdoc.version>2.2.0</springdoc.version>
<jcabi-http.version>1.20.1</jcabi-http.version>
<testcontainers-keycloak.version>3.2.0</testcontainers-keycloak.version>
<testcontainers.version>1.19.3</testcontainers.version>
Expand Down Expand Up @@ -124,6 +125,11 @@ SOFTWARE.
<artifactId>eokson</artifactId>
<version>${eokson.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@

package git.tracehub.pmo.security;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.OAuthFlow;
import io.swagger.v3.oas.models.security.OAuthFlows;
import io.swagger.v3.oas.models.security.Scopes;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
Expand All @@ -31,13 +38,25 @@
import org.springframework.security.web.SecurityFilterChain;

/**
* Security configurations.
* Web configurations.
*
* @since 0.0.0
*/
@Configuration
@EnableMethodSecurity
public class SecurityConfig {
public class WebConfig {

Check warning on line 47 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L47

Added line #L47 was not covered by tests

/**
* API Version.
*/
@Value("${application.version}")
private String version;

/**
* Auth server url.
*/
@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
private String url;

/**
* Filter.
Expand All @@ -47,7 +66,6 @@
* @checkstyle NonStaticMethodCheck (30 lines)
*/
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
@SneakyThrows
public SecurityFilterChain client(final HttpSecurity http) {
return http.cors(Customizer.withDefaults())
Expand All @@ -60,7 +78,11 @@
)
).authorizeHttpRequests(
auth -> auth
.requestMatchers("/login").permitAll()
.requestMatchers(

Check warning on line 81 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L81

Added line #L81 was not covered by tests
"/login",
"/v3/**",
"/swagger-ui/**"
).permitAll()

Check warning on line 85 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L85

Added line #L85 was not covered by tests
.anyRequest().authenticated()
).exceptionHandling(
configurer -> configurer
Expand All @@ -75,4 +97,45 @@
).build();
}

/**
* Open API config for Swagger.
*
* @return OpenAPI
*/
@Bean
public OpenAPI openApi() {
final String name = "auth";
return new OpenAPI()
.addSecurityItem(

Check warning on line 109 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L107-L109

Added lines #L107 - L109 were not covered by tests
new SecurityRequirement()
.addList(name)
).components(

Check warning on line 112 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L111-L112

Added lines #L111 - L112 were not covered by tests
new Components()
.addSecuritySchemes(

Check warning on line 114 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L114

Added line #L114 was not covered by tests
name, new SecurityScheme()
.name(name)
.type(SecurityScheme.Type.OAUTH2)
.flows(
new OAuthFlows().authorizationCode(

Check warning on line 119 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L116-L119

Added lines #L116 - L119 were not covered by tests
new OAuthFlow()
.authorizationUrl(

Check warning on line 121 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L121

Added line #L121 was not covered by tests
"%s/protocol/openid-connect/auth"
.formatted(this.url)
).refreshUrl(

Check warning on line 124 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L123-L124

Added lines #L123 - L124 were not covered by tests
"%s/protocol/openid-connect/token"
.formatted(this.url)
).tokenUrl(

Check warning on line 127 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L126-L127

Added lines #L126 - L127 were not covered by tests
"%s/protocol/openid-connect/token"
.formatted(this.url)
).scopes(new Scopes())

Check warning on line 130 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L129-L130

Added lines #L129 - L130 were not covered by tests
)
)
)
).info(

Check warning on line 134 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L134

Added line #L134 was not covered by tests
new Info()
.title("PMO API")
.version(this.version)

Check warning on line 137 in src/main/java/git/tracehub/pmo/security/WebConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/git/tracehub/pmo/security/WebConfig.java#L136-L137

Added lines #L136 - L137 were not covered by tests
);
}

}
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
application:
title: ${APP_TITLE}
version: ${APP_VERSION}
server:
port: 8080
shutdown: graceful
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

,--------. ,--. ,--.
'--. .--',--.--.,--,--.,---. ,---. | ,---. ,--.,--.| |-.
| | | .--' ,-. | .--'| .-. :| .-. || || || .-. '
| | | | \ '-' \ `--.\ --.| | | |' '' '| `-' |
`--' `--' `--`--'`---' `----'`--' `--' `----' `---'

${application.title}
${application.version}
Powered by Spring Boot ${spring-boot.version}
3 changes: 3 additions & 0 deletions src/test/resources/application-pgit.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
application:
title: IT
version: 0.0.1
spring:
datasource:
username: test
Expand Down