-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 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
32 changes: 32 additions & 0 deletions
32
...ot/mvc/security/jwt/authentication/main/infrastructure/primary/SpaWebFilter.java.mustache
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 {{packageName}}.shared.authentication.infrastructure.primary; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
public class SpaWebFilter extends OncePerRequestFilter { | ||
/** | ||
* Forwards any unmapped paths (except those containing a period) to the client {@code index.html}. | ||
*/ | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
// Request URI includes the contextPath if any, removed it. | ||
String path = request.getRequestURI().substring(request.getContextPath().length()); | ||
if ( | ||
!path.startsWith("/api") && | ||
!path.startsWith("/management") && | ||
!path.startsWith("/v3/api-docs") && | ||
!path.contains(".") | ||
) { | ||
request.getRequestDispatcher("/index.html").forward(request, response); | ||
return; | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
.../mvc/security/jwt/authentication/test/infrastructure/primary/SpaWebFilterIT.java.mustache
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,90 @@ | ||
package {{packageName}}.shared.authentication.infrastructure.primary; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import {{packageName}}.IntegrationTest; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@AutoConfigureMockMvc | ||
@WithMockUser | ||
@IntegrationTest | ||
class SpaWebFilterIT { | ||
@Autowired | ||
private MockMvc mockMvc; | ||
@Test | ||
void testFilterForwardsToIndex() throws Exception { | ||
mockMvc.perform(get("/")) | ||
.andExpect(status().isOk()) | ||
.andExpect(forwardedUrl("/index.html")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(authorities = "ADMIN") | ||
void testFilterDoesNotForwardToIndexForV3ApiDocs() throws Exception { | ||
mockMvc.perform(get("/v3/api-docs")) | ||
.andExpect(status().isOk()) | ||
.andExpect(forwardedUrl(null)); | ||
} | ||
|
||
@Test | ||
@WithMockUser(authorities = "ADMIN") | ||
void testFilterDoesNotForwardToIndexForManagement() throws Exception { | ||
mockMvc.perform(get("/management")) | ||
.andExpect(status().isForbidden()) | ||
.andExpect(forwardedUrl(null)); | ||
} | ||
|
||
@Test | ||
void testFilterDoesNotForwardToIndexForDotFile() throws Exception { | ||
mockMvc.perform(get("/file.js")) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
void getBackendEndpoint() throws Exception { | ||
mockMvc.perform(get("/test")) | ||
.andExpect(status().isOk()) | ||
.andExpect(forwardedUrl("/index.html")); | ||
} | ||
|
||
@Test | ||
void forwardUnmappedFirstLevelMapping() throws Exception { | ||
mockMvc.perform(get("/first-level")) | ||
.andExpect(status().isOk()) | ||
.andExpect(forwardedUrl("/index.html")); | ||
} | ||
|
||
@Test | ||
void forwardUnmappedSecondLevelMapping() throws Exception { | ||
mockMvc.perform(get("/first-level/second-level")) | ||
.andExpect(status().isOk()) | ||
.andExpect(forwardedUrl("/index.html")); | ||
} | ||
|
||
@Test | ||
void forwardUnmappedThirdLevelMapping() throws Exception { | ||
mockMvc.perform(get("/first-level/second-level/third-level")) | ||
.andExpect(status().isOk()) | ||
.andExpect(forwardedUrl("/index.html")); | ||
} | ||
|
||
@Test | ||
void forwardUnmappedDeepMapping() throws Exception { | ||
mockMvc.perform(get("/1/2/3/4/5/6/7/8/9/10")) | ||
.andExpect(forwardedUrl("/index.html")); | ||
} | ||
|
||
@Test | ||
void getUnmappedFirstLevelFile() throws Exception { | ||
mockMvc.perform(get("/foo.js")) | ||
.andExpect(status().isNotFound()); | ||
} | ||
} |