spring 404页面未找到(404)

izkcnapc  于 2023-05-16  发布在  Spring
关注(0)|答案(3)|浏览(140)

我的spring应用程序出现了问题,因为我试图包含一些安全方面的内容。
在构建了一个包含angularJS的小型工作应用程序后,我遵循了这个spring security tutorial,但我无法启动它。当我尝试访问应用程序的任何部分时,安全模块希望重定向到http://localhost:8080/login ...但找不到
出现意外错误(类型=未找到,状态=404)。没有可用的消息
也许我只是错过了一件小事,但我无法弄清楚它是什么^^
这是我的代码…

文件夹结构:

src/main/java
 +-Application.java
 +-SecurityConfiguration.java

src/main/resources
 +-static
  +-index.html
 +-templates
  +-login.html

pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.BUILD-SNAPSHOT</version>
</parent>

<!-- Additional lines to be added here... -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1100-jdbc4</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
    </dependency>

</dependencies>

应用程序.java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
    }
}

SecurityConfiguration.java:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().anyRequest().authenticated();
        http
                .formLogin().failureUrl("/login?error")
                .defaultSuccessUrl("/")
                .loginPage("/login")
                .permitAll()
                .and()
                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }
}

login.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>
</head>
<body>
    <div th:if="${param.error}">Invalid username and password.</div>
    <div th:if="${param.logout}">You have been logged out.</div>
    <form th:action="@{/login}" method="post">
        <div>
            <label> User Name : <input type="text" name="username" />
            </label>
        </div>
        <div>
            <label> Password: <input type="password" name="password" />
            </label>
        </div>
        <div>
            <input type="submit" value="Sign In" />
        </div>
    </form>
</body>
</html>
oyt4ldly

oyt4ldly1#

我按照上面评论中的提示,发现我在Application.class中忘记了extends WebMvcConfigurerAdapter
这解决了问题!

b5lpy0ml

b5lpy0ml2#

你的login.html在哪里?你可以直接在浏览器中访问/login吗?我怀疑你没有正确设置ViewResolver。如果你不能直接转到/login,那就是你的问题了。查看application.properties值spring.view.prefixspring.view.suffix
如果你可以直接到达那里,你可能会处理一个隐藏的错误。Sping Boot 包含一个AutoConfiguration,它会吞下异常并抛出404。您可能希望排除它(将@EnableAutoConfiguration更改为@EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class)。这将使您能够看到在请求处理过程中抛出的任何错误。

23c0lvtd

23c0lvtd3#

在我的情况下,我必须在解决404的控制器中定义以下内容

@GetMapping("/login")
public String loginPage(Model model) {
    return "login";
}

相关问题