我目前正在实现一个应用程序的登录页面,我想做。我现在试图让登录页面(html文件)显示当我访问http://localhost:3307/loginpage。但每次我这样做,我一直得到“白标错误页”。它是引导我到localhost:3307/loginpage,但它给出这个错误,而不是显示我的html页面。从我的理解,这发生在你犯了一个错误,它实际上没有与文件交互。将感谢任何帮助,我可以得到,谢谢。
我的应用程序属性(我通常使用正确的用户名/密码)
server.port=3307
spring.datasource.url=jdbc:mysql://localhost:3306/bookcollection
spring.datasource.driverClassName= com.mysql.cj.jdbc.Driver
spring.datasource.username=
spring.datasource.password=
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
我的WebSecurity。我允许访问/loginpage。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
private final UserService userService;
private final BCryptPasswordEncoder passwordEncoder;
public WebSecurityConfig(UserService userService, BCryptPasswordEncoder passwordEncoder) {
this.userService = userService;
this.passwordEncoder = passwordEncoder;
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeHttpRequests()
.antMatchers("/api/v*/registration/**")
.permitAll()
.anyRequest()
.authenticated().and()
.formLogin().loginPage("/loginpage").permitAll();
return http.build();
}
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
}
@Bean
public DaoAuthenticationProvider daoAutenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(passwordEncoder);
provider.setUserDetailsService(userService);
return provider;
}
}
这是我的LoginController。根据我的理解,@RequestMapping默认是一个Get请求。我使用print语句来查看是否会在控制台中弹出任何内容(它没有)。
@Controller
public class LoginController {
@GetMapping
public String getLogin(Model model) {
return "loginpage";
}
}
这是我的html文件,它被放置在资源/模板中
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
</head>
<body>
</body>
</html>
2条答案
按热度按时间pcww981p1#
问题已解决。
我决定使用 Thymeleaf 而不是 JSP,并设法使页面显示出来。
ycl3bljg2#
从官方Sping Boot 文档中:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web.servlet.spring-mvc.static-content
默认情况下,资源被Map到/**上,但是您可以使用spring.mvc.static-path-pattern属性对此进行调整。例如,将所有资源重定位到/resources/**可以如下实现:
您也可以使用spring.web.resources.static-locations属性(以目录位置清单取代预设值)自订静态资源位置。根Servlet情境路径“/"也会自动新增为位置。
除了前面提到的“标准”静态资源位置之外,Webjars内容还有一个特殊情况。路径在/webjars/**中的任何资源都是从jar文件提供的,如果它们是以Webjars格式打包的。
如果您的应用程序被打包为jar,请不要使用src/main/webapp目录。尽管此目录是一个通用标准,但它仅适用于war打包,并且如果您生成jar,大多数构建工具都会自动忽略此目录。
因此,请考虑将JSP页移动到另一个允许的目录。另外,请将@RestController更改为@Controller。由于此注解中包含@ResponseBody,因此它将无法与@RestController一起使用