我试图在我的Spring Boot应用中转到登录页面,但出现此错误
Circular view path [/login.jsp]: would dispatch back to the current handler URL [/login.jsp] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
在stackoverflow上,人们给出需要增加百里香依赖的建议
('org.springframework.boot:spring-boot-starter-thymeleaf')
但是之后我得到了这个错误
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers
此错误表明Spring在
/resurses/templates
文件夹中找不到模板login.html。如果我在另一个文件夹中有自己的login.jsp并且不想使用任何模板,该怎么办?
这是我的登录映射
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model, String error, String logout) {
if (error != null)
model.addAttribute("error", "Your username and password is invalid.");
if (logout != null)
model.addAttribute("message", "You have been logged out successfully.");
return "login";
}
这是我的安全配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/resources/**", "/registration").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
最佳答案
很简单,只需在您的控制器中为登录页面创建自己的映射,例如:
@Controller
public class AppController{
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout, Model model, HttpServletRequest request) {
ModelAndView view = new ModelAndView();
if (error != null) {
view.addObject("error", "Invalid username and password!");
}
if (logout != null) {
view.addObject("msg", "You've been logged out successfully.");
}
view.setViewName("your-login-page");
return view;
}
}
并且配置应如下所示:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().and()
.formLogin().loginPage("/login").loginProcessingUrl("/login").failureUrl("/login?error")
}
并且application.properties文件应如下所示:
spring.thymeleaf.suffix=.your-file-type
spring.thymeleaf.prefix=/WEB-INF/jsp-pages/
其中“ / WEB-INF / jsp-pages /”是您的文件目录