I try to log in using Spring Security. But, when I entered credentials and press the submit button the login page reload instead of opening main page. I searched the problem but found solution only for Spring Security in xml. But I have Spring Security is as Java class. I would be grateful for your help.
Spring Security config class:
package com.ensei.Chat.config;
import com.ensei.Chat.service.CustomUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailService customUserDetailService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/chat", "/chat/registration", "/chat/registrationDone", "/chat/exit")
.permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/chat/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserDetailService)
.passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder(8);
}
}
My Controller class:
package com.ensei.Chat.controller;
import com.ensei.Chat.ChatApplication;
import com.ensei.Chat.model.ChatManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/chat")
public class MyController {
@Autowired
ChatManager manager;
@GetMapping
public String start(){
return "chat/start";
}
@GetMapping("/registration")
public String register(){
return "chat/registration";
}
@PostMapping("/registrationDone")
public String registrationDone(String email, String password, String name){
manager.createUser(email, password, name);
return "chat/registrationDone";
}
@GetMapping("/login")
public String login(){
return "chat/login";
}
@GetMapping("/main")
public String main(String password){
return "chat/main";
}
@PostMapping("/main")
public String mainP(String password){
return "chat/main";
}
@GetMapping("/exit")
public void exit(){
ChatApplication.finishApp();
}
}
My login page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Log in</title>
</head>
<body>
<form action="main" method="post">
<label for="login">Логін</label>
<input id="login" type="text"><br><br>
<label for="password">Пароль</label>
<input id="password" type="text"><br><br>
<button type="submit">Далі</button>
</form>
</body>
</html>