Spring Security Spring安全登录无法找到视图和获取资源未找到HTTP 404

uz75evzq  于 2023-05-29  发布在  Spring
关注(0)|答案(1)|浏览(245)

bounty还有3天到期。此问题的答案有资格获得+50声望奖励。Genaut希望引起更多注意这个问题:

我希望任何有Spring安全或Spring安全迁移5-6经验的人都能得到一些帮助,并且能够检测配置中的问题
我正在做一个小小的Sping Boot 项目。我已经设置了一个spring安全配置来启用h2 consolepublic endpoints..
我可以访问此公共端点。无论如何,当我尝试访问/admin/login时,我会得到一个404 HTTP错误,并在控制台日志中显示以下消息:

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

这是我的spring security http配置

@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();
}

我测试了把我的“admin/login.html”放在“/resources,/resources/static and /resources/templates”中(就像控制台日志显示一样),但在这些目录中仍然无法工作。

a2mppw5e

a2mppw5e1#

你必须创建有效的Map到选定的路径,我假设你使用thymeleaf作为模板引擎,为了工作,在你的REST控制器中创建一个Map,例如:

@GetMapping(value = "/admin/login")
    public String login(){
        return "login";
    }

“YourHtmlLoginFileName”是您的/resources/templates中的文件名,不带扩展名。

相关问题