Skip to content

Commit

Permalink
Swagger spring sec 3 (eugenp#17742)
Browse files Browse the repository at this point in the history
* Swagger with Spring Security 3 - moving to another module

* Swagger with Spring Security 3

* Swagger with Spring Security 3

* Code formatting
  • Loading branch information
abh1navv authored Oct 16, 2024
1 parent 0df73c7 commit 92bb4e7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spring-boot-modules/spring-boot-3-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down Expand Up @@ -85,5 +94,6 @@
<apache-camel.version>4.7.0</apache-camel.version>
<langchain4j.version>0.33.0</langchain4j.version>
<spring-cloud-starter.version>4.1.3</spring-cloud-starter.version>
<springdoc-starter.version>2.6.0</springdoc-starter.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.swagger;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SwaggerApplication {

public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

http.authorizeHttpRequests(authorizeRequests -> authorizeRequests.requestMatchers("/swagger-ui/**")
.permitAll()
.requestMatchers("/v3/api-docs/**")
.permitAll());

return http.build();
}

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.requestMatchers("/swagger-ui/**", "/v3/api-docs/**");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.swagger.config;

import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfig {

@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("public")
.pathsToMatch("/**")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.swagger.controller;

import io.swagger.v3.oas.annotations.Operation;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@Operation(summary = "Returns a Hello World message")
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}

0 comments on commit 92bb4e7

Please sign in to comment.