Spring Security登录找不到视图并获取”资源未找到 HTTP 404″错误。

huangapple go评论74阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月22日 07:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定