java - 登录时出现 java.lang.StackOverflowError
问题描述
我正在为我的项目使用 spring-boot-starter-security & validation-api。以前我的项目运行正常,用户可以注册到注册页面并且数据被正确存储在数据库中。为了登录,我制作了自定义登录页面,并且能够登录,但我不知道发生了什么错误。现在它显示stackoverflow错误
UserController.java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
UserRepository userRepository;
@ModelAttribute
public void addCommonData(Model model,Principal principal) {
String userName = principal.getName();
User user = userRepository.getUSerByUserName(userName);
//debug in console
System.out.println("user " + user);
model.addAttribute("user",user);
}
@RequestMapping("/index")
public String dashboard(Model model ,Principal principal) {
model.addAttribute("title","User Dashboard");
return "normal/user_dashboard";
}
@GetMapping("/add-contact")
public String openAddContactForm(Model model) {
model.addAttribute("title", "Add Contact");
model.addAttribute("contact",new Contact());
return "normal/add_contact_form";
}
}
Myconfig.java
@Configuration
@EnableWebSecurity
public class MyConfig extends WebSecurityConfigurerAdapter{
@Bean
public UserDetailsService getUserDetailsService() {
return new UserDetailsServiceImp();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(this.getUserDetailsService());
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
return daoAuthenticationProvider;
}
//Configure method
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.antMatchers("/**").permitAll()
.and().formLogin()
.loginPage("/signin")
.loginProcessingUrl("/dologin")
.defaultSuccessUrl("/user/index")
.and().csrf().disable();
}
}
我不明白错误发生在哪里。请帮助,我还可以提供 UserDetails 和 UserDetailsService 接口的实现代码。我正在使用以下依赖项 1.spring-boot-starter-data-jpa 2.spring-boot-starter-thymeleaf 3.spring-boot-starter-web 4.spring-boot-starter-security 5.spring-boot-devtools 6.validation-api 7.hibernate-validator 8.mysql-connector-java
对于授权部分,我在 Myconfig.java 中提供了代码。请帮助我不明白错误是什么
error
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Nov 15 21:48:55 NPT 2020
There was an unexpected error (type=Internal Server Error, status=500).
No message available
java.lang.StackOverflowError
表格的网址和动作都是正确的..
HomeController.java for remaining pages and request
@Controller
public class HomeController {
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Autowired
UserRepository userRepo;
@RequestMapping("/")
public String home(Model model) {
model.addAttribute("title","Home-Smart Contact Manager");
return "home";
}
@RequestMapping("/about")
public String about(Model model) {
model.addAttribute("title","About-Smart Contact Manager");
return "about";
}
@RequestMapping("/signup")
public String signup(Model model) {
model.addAttribute("title","Register-Smart Contact Manager");
model.addAttribute("user",new User());
return "signup";
}
//hander for registering user
@RequestMapping(value="/do_register",method=RequestMethod.POST)
public String registerUser(@Valid @ModelAttribute("user")User user,BindingResult result ,
@RequestParam(value="agreement",defaultValue="false") boolean agreement,
Model model, HttpSession session)
{
try {
//check gryoki nai vnyrw validation nai ho
if(!agreement)
{
System.out.println("you haven't agreed the terms and conditions");
throw new Exception("You haven't agreed the terms and conditions");
}
//if invalid data pathako xa vnya error aauxa
if(result.hasErrors()) {
model.addAttribute("user",user); //we have send user obj to the view
return "signup";
}
//yadi error xa vana back to the form with user input data sajilo
user.setRole("ROLE_USER");
user.setEnabled(true);
user.setImageUrl("default.png");
user.setPassword(passwordEncoder.encode(user.getPassword()));
System.out.println("agreement "+ agreement);
System.out.println("User "+ user);
User result1 = this.userRepo.save(user);
model.addAttribute("user",new User());
session.setAttribute("message", new Message("Successfully Registered!!","alert-success"));
return "signup";
}
catch(Exception e)
{
e.printStackTrace();
model.addAttribute("user",user);
session.setAttribute("message", new Message("Something Went Wrong !! " + e.getMessage(),"alert-danger"));
return "signup";
}
}
//handler for custom login
@GetMapping("/signin")
public String customLogin(Model model) {
model.addAttribute("title","Login Page - Smart Contact Manager");
return "login";
}
}
解决方案
推荐阅读
- mongodb - 如何使用 GraphQL 制作包含对象数组的组突变?
- python - 如何修复 snap7 中的模块错误
- amazon-web-services - 适用于私人团队的 AWS Sagemaker Ground Truth WorkerID
- java - 获取注释文本
- javascript - Tween - 无限重复而不从开始位置开始
- node.js - 如何在nodejs中进行API调用
- c# - WinDbg sosex 无法调试
- vhdl - 即使在 VHDL 进程中执行信号分配,信号也会变得未定义
- typescript - 保留函数和函数参数类型
- php - Woocommerce:如何在注销时显示自定义成功通知消息?