我正在嘗試使用自定義登錄表單創建一個具有彈簧安全性的簡單彈簧啟動應用程式。
問題: 該應用程式直接打開默認登錄頁面而不是自定義登錄視圖,并且它也無法決議任何其他 thymeleaf 視圖。
直接訪問所有其他視圖 ( /home, /error) 會呈現錯誤:This localhost page can't be found.打開http://localhost:8080/login只需要進入默認登錄頁面,而不是自定義登錄頁面。
注意: html 模板放在 /src/main/resources/templates 檔案夾下 - 這是 springboot 查找的默認位置。
這里的關鍵是我正在使用
@EnableWebSecurity注解,帶有SecurityFilterChainbean(在 中介紹Spring Security 5.4),如下所示:
@EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain configure(HttpSecurity http) throws Exception { ... }而不是更常見(但自 以來已被棄用
Spring Security 5.7.0-M2)的擴展WebSecurityConfigurerAdapter類的方式,如下所示。@Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { ... }后者似乎作業正常,沒有任何問題。
各種論壇中建議的解決方案很少 - 我已經嘗試過。
- 保持專案結構,使所有其他包都放在基礎包(帶有帶
@SprintBootApplication注釋的主類的那個)下。 - 使用
@ComponentScan={<your_base_package>}- 如果包按第 1 點中提到的順序排列。或者,@ComponentScan={<package-1>, <package-2>, etc}如果包彼此獨立。
建議使用上述兩種解決方案以避免 404 錯誤并查看未解決的問題。 - 使用
@RestController而不是@Controller.
這是針對 WhiteLabel 錯誤以及僅作為字串而不是視圖回傳視圖名稱時建議的。 - 保持控制器方法中的映射 url 值(如,
/login)和視圖名稱不同。如果映射 url 為/login,則將視圖名稱更改為loginpage.html(或其他名稱)。
這是針對回圈路徑問題提出的 - 在決議視圖名稱時。 - 一些人建議
@RequestMapping("/login")在類級別而不是方法級別使用。雖然,我沒有看到這兩種方法有任何區別。
請注意,以上所有內容均基于WebSecurityConfigurerAdapter& 而不是基于SecurityFilterChain.
我可以從官方檔案/博客中找到滿足此要求的唯一參考資料(使用 自定義登錄
SecurityFilterChain)是這兩個:一世。
從日志中提取- 我發現相對/值得注意。
檢查“..invalid session id...”和“..Failed to authorize filter invocation...”部分。2022-08-18 12:09:43.297 INFO 16596 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 10 ms 2022-08-18 12:09:43.334 DEBUG 16596 --- [http-nio-8080-exec-1] o.s.security.web.FilterChainProxy : Securing GET / 2022-08-18 12:09:43.351 DEBUG 16596 --- [http-nio-8080-exec-1] s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to empty SecurityContext 2022-08-18 12:09:43.363 DEBUG 16596 --- [http-nio-8080-exec-1] ****o.s.s.w.a.AnonymousAuthenticationFilter : Set SecurityContextHolder to anonymous SecurityContext 2022-08-18 12:09:43.364 DEBUG 16596 --- [http-nio-8080-exec-1] o.s.s.w.session.SessionManagementFilter : Request requested invalid session id EF53E44688D581C69527A5442A987DB6 2022-08-18 12:09:43.394 DEBUG 16596 --- [http-nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor : Failed to authorize filter invocation [GET /] with attributes [authenticated] 2022-08-18 12:09:43.445 DEBUG 16596 --- [http-nio-8080-exec-1] o.s.s.w.s.HttpSessionRequestCache : Saved request http://localhost:8080/ to session**** 2022-08-18 12:09:43.448 DEBUG 16596 --- [http-nio-8080-exec-1] s.w.a.DelegatingAuthenticationEntryPoint : Trying to match using And [Not [RequestHeaderRequestMatcher [expectedHeaderName=X-Requested-With, expectedHeaderValue=XMLHttpRequest]], MediaTypeRequestMatcher [contentNegotiationStrategy=org.springframework.web.accept.HeaderContentNegotiationStrategy@51020050, matchingMediaTypes=[application/xhtml xml, image/*, text/html, text/plain], useEquals=false, ignoredMediaTypes=[*/*]]] 2022-08-18 12:09:43.450 DEBUG 16596 --- [http-nio-8080-exec-1] s.w.a.DelegatingAuthenticationEntryPoint : Match found! Executing org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint@416ee886 2022-08-18 12:09:43.454 DEBUG 16596 --- [http-nio-8080-exec-1] o.s.s.web.DefaultRedirectStrategy : Redirecting to http://localhost:8080/login 2022-08-18 12:09:43.459 DEBUG 16596 --- [http-nio-8080-exec-1] w.c.HttpSessionSecurityContextRepository : Did not store empty SecurityContext 2022-08-18 12:09:43.466 DEBUG 16596 --- [http-nio-8080-exec-1] w.c.HttpSessionSecurityContextRepository : Did not store empty SecurityContext 2022-08-18 12:09:43.466 DEBUG 16596 --- [http-nio-8080-exec-1] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request 2022-08-18 12:09:43.487 DEBUG 16596 --- [http-nio-8080-exec-2] o.s.security.web.FilterChainProxy : Securing GET /login完整的日志在這里: https
://codeshare.io/dwlLDK (似乎它只存在 24 小時。如果您無法訪問它,請告訴我)。編輯(2022-08-22):檔案 摘錄
application.properties(只是百里香和安全配置)-這是問題的原因(如接受的 [ self-identified ] 答案中所述)。# thymeLeaf spring.thymeleaf.enabled=true spring.thymeleaf.cache=false spring.thymeleaf.check-template=true spring.thymeleaf.check-template-location=true spring.thymeleaf.prefix=classpath:/templates spring.thymeleaf.suffix=.html #security server.error.whitelabel.enabled=falseuj5u.com熱心網友回復:
我在這里有一個示例 Spring Boot 應用程式: https ://github.com/Aliuken/JobVacanciesApp_Java11
希望它有所幫助。
uj5u.com熱心網友回復:
我自己解決了這個問題。
(將其添加為可接受的答案,以便偶然發現此頁面的人很容易。)
實際問題是檔案
/中的 thymeleaf前綴配置中缺少尾隨application.properties:spring.thymeleaf.prefix=classpath:/templates.這應該是
spring.thymeleaf.prefix=classpath:/templates/。一旦我添加它,它就開始識別模板(包括自定義登錄模板/視圖)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/505310.html
標籤:弹簧靴 弹簧MVC 弹簧安全 百里香叶 http-status-code-404
上一篇:如何在沒有XML檔案且僅使用Java的情況下配置Spring專案?
下一篇:傳輸層

