Skip to content

Commit 5cd9471

Browse files
committed
Add swagger customizer to be behind reverse proxy
1 parent cc89da4 commit 5cd9471

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Without authentication, you can only access the public endpoints, for example:
4343
curl http://localhost:8080/courses
4444
```
4545

46-
You can also see all available endpoints in the [OpenAPI documentation](http://localhost:8080/swagger/swagger-ui/index.html).
46+
You can also see all available endpoints in the [OpenAPI documentation](http://localhost:8080/swagger-ui/index.html).
4747

4848
### Running the frontend
4949

src/main/java/net/hackyourfuture/coursehub/SecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3939
.permitAll()
4040
.requestMatchers(HttpMethod.POST, "/login", "/register")
4141
.permitAll()
42-
.requestMatchers(HttpMethod.GET, "/courses", "/swagger/**")
42+
.requestMatchers(HttpMethod.GET, "/courses", "/swagger-ui/**", "/v3/api-docs/**")
4343
.permitAll()
4444
.requestMatchers("/students/**")
4545
.hasRole("student")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.hackyourfuture.coursehub;
2+
3+
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class SwaggerBehindReverseProxyConfig {
9+
10+
@Bean
11+
ServerBaseUrlCustomizer serverBaseUrlCustomizer() {
12+
return (serverBaseUrl, request) -> {
13+
if (serverBaseUrl.contains("coursehub.hyf.dev")) {
14+
return serverBaseUrl + "/api";
15+
}
16+
return serverBaseUrl;
17+
};
18+
}
19+
}

src/main/java/net/hackyourfuture/coursehub/web/StudentController.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
import net.hackyourfuture.coursehub.service.CourseService;
66
import net.hackyourfuture.coursehub.web.model.CourseListResponse;
77
import org.springframework.http.HttpStatus;
8-
import org.springframework.http.HttpStatusCode;
98
import org.springframework.security.core.annotation.AuthenticationPrincipal;
109
import org.springframework.web.bind.annotation.GetMapping;
1110
import org.springframework.web.bind.annotation.PathVariable;
1211
import org.springframework.web.bind.annotation.RequestMapping;
1312
import org.springframework.web.bind.annotation.RestController;
14-
import org.springframework.web.client.HttpClientErrorException;
15-
import org.springframework.web.client.HttpServerErrorException;
16-
import org.springframework.web.client.HttpStatusCodeException;
13+
import org.springframework.web.server.ResponseStatusException;
1714

1815
@RestController
1916
@RequestMapping("/students")
@@ -29,7 +26,7 @@ public StudentController(CourseService courseService) {
2926
public CourseListResponse getCoursesForStudent(
3027
@PathVariable @Positive Integer studentId, @AuthenticationPrincipal AuthenticatedUser user) {
3128
if (!user.getUserId().equals(studentId)) {
32-
throw new HttpClientErrorException(HttpStatus.FORBIDDEN);
29+
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
3330
}
3431
var courses = courseService.getCoursesForStudent(studentId);
3532
return new CourseListResponse(courses);

src/main/resources/application.properties

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,3 @@ spring.datasource.password=course_user_password
88

99
# Server configuration
1010
server.port=8080
11-
12-
springdoc.swagger-ui.path=/swagger/swagger-ui.html
13-
springdoc.api-docs.path=/swagger/v3/api-docs

0 commit comments

Comments
 (0)