英文:
Spring security login can't find view and get Resource not found HTTP 404
问题
I am working on a little Spring Boot project. I have set up a Spring Security config to enable h2console and public endpoints.
I can access these public endpoints. However, when I try to access /admin/login, I get a 404 HTTP error and see this message in the console log:
2023-05-23T18:01:07.248+02:00 [DEBUG] [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet: GET "/admin/login", parameters={}
2023-05-23T18:01:07.248+02:00 [DEBUG] [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping: Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-05-23T18:01:07.249+02:00 [DEBUG] [nio-8080-exec-3] o.s.w.s.r.ResourceHttpRequestHandler: Resource not found
2023-05-23T18:01:07.249+02:00 [DEBUG] [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet: Completed 404 NOT_FOUND
...
This is my Spring Security HTTP config:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(authorize ->
authorize
.requestMatchers(
PathRequest.toH2Console()
).permitAll()
.requestMatchers(
"/api/auth/login",
"/api/auth/logout",
"/admin/login",
"/error"
).permitAll()
).formLogin(formLogin ->
formLogin
.usernameParameter("username")
.passwordParameter("password")
.loginPage("/admin/login")
.failureUrl("/admin/login?failed")
.loginProcessingUrl("/admin/login/process")
.permitAll()
).logout(logout ->
logout
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.logoutUrl("/api/auth/logout")
.logoutSuccessUrl("/logout-success")
).headers(headers ->
headers.frameOptions(frameOptions -> frameOptions.sameOrigin())
);
return http.build();
}
I tested putting my admin/login.html in /resources, /resources/static, and /resources/templates (as shown in the console log), but it continues not to work in any of these directories.
英文:
I am working on a little Spring boot project. I have setted up a spring security config to enable h2console, public endpoints..
I can access to this public endpoints. Anyway when I try access to /admin/login I a get an 404 HTTP error and this message in the console log:
2023-05-23T18:01:07.248+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m GET "/admin/login", parameters={}
[2m2023-05-23T18:01:07.248+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
[2m2023-05-23T18:01:07.249+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36mo.s.w.s.r.ResourceHttpRequestHandler [0;39m [2m:[0;39m Resource not found
[2m2023-05-23T18:01:07.249+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Completed 404 NOT_FOUND
[2m2023-05-23T18:01:07.250+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
[2m2023-05-23T18:01:07.250+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
[2m2023-05-23T18:01:07.250+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
[2m2023-05-23T18:01:07.250+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
[2m2023-05-23T18:01:07.250+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m "ERROR" dispatch for GET "/error", parameters={}
[2m2023-05-23T18:01:07.250+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
[2m2023-05-23T18:01:07.251+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36mo.s.w.s.m.m.a.HttpEntityMethodProcessor [0;39m [2m:[0;39m Using 'application/json', given [image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*] and supported [application/json, application/*+json]
[2m2023-05-23T18:01:07.251+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36mo.s.w.s.m.m.a.HttpEntityMethodProcessor [0;39m [2m:[0;39m Writing [{timestamp=Tue May 23 18:01:07 CEST 2023, status=404, error=Not Found, message=No message available, (truncated)...]
[2m2023-05-23T18:01:07.251+02:00[0;39m [32mDEBUG[0;39m [35m21672[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Exiting from "ERROR" dispatch, status 404
This is my spring security http config:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf((csrf) -> csrf.disable())
.authorizeHttpRequests((authorize) ->
authorize
.requestMatchers(
PathRequest.toH2Console()
).permitAll()
.requestMatchers(
"/api/auth/login",
"/api/auth/logout",
"/admin/login",
"/error"
).permitAll()
).formLogin((formLogin) ->
formLogin
.usernameParameter("username")
.passwordParameter("password")
.loginPage("/admin/login")
.failureUrl("/admin/login?failed")
.loginProcessingUrl("/admin/login/process")
.permitAll()
).logout((logout) ->
logout
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.logoutUrl("/api/auth/logout")
.logoutSuccessUrl("/logout-success")
).headers((headers) ->
headers
.frameOptions(frameOptions -> frameOptions.sameOrigin())
)
;
return http.build();
}
I tested putting my "admin/login.html" in "/resources, /resources/static and /resources/templates" (like console log show) but continue not working in any of this directories.
答案1
得分: 1
你必须创建一个有效的映射到所选路径,我假设你正在使用Thymeleaf作为模板引擎,为了使其工作,在你的REST控制器中创建一个映射,例如:
@GetMapping(value = "/admin/login")
public String login(){
return "login";
}
"YourHtmlLoginFileName" 是你的/resources/templates目录下文件的名称,不包括扩展名。
英文:
You have to create valid mapping to selected path, i assume you are using thymeleaf as template engine, in order to work, in your REST controller create a mapping for example:
@GetMapping(value = "/admin/login")
public String login(){
return "login";
}
"YourHtmlLoginFileName" is the name of the file in your /resources/templates whithout extension.
答案2
得分: 0
检查您的配置文件中的 contextPath
键(例如,application.properties 或 application.yml)。如果您有这个参数,您需要在您的请求之前添加 /{contextPath.value}
。
英文:
Check your contextPath
key in your configuration file (e.g. application.properties or application.yml). If you have this param, you have to add /{contextPath.value}
before your requets.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论