join with security
This commit is contained in:
@@ -1,12 +1,20 @@
|
||||
package com.spring.security1.controller;
|
||||
|
||||
import com.spring.security1.model.User;
|
||||
import com.spring.security1.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
@RequiredArgsConstructor
|
||||
public class IndexController {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
||||
|
||||
@GetMapping({"", "/"})
|
||||
public String index() {
|
||||
@@ -33,22 +41,26 @@ public class IndexController {
|
||||
return "manager";
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("/login")
|
||||
public String login() {
|
||||
return "login";
|
||||
@GetMapping("/loginForm")
|
||||
public String loginForm() {
|
||||
return "loginForm";
|
||||
}
|
||||
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("/join")
|
||||
public String join() {
|
||||
return "join";
|
||||
@GetMapping("/joinForm")
|
||||
public String joinForm() {
|
||||
return "joinForm";
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("/joinProc")
|
||||
public String joinProc() {
|
||||
return "회원가입 완료!";
|
||||
@PostMapping("/join")
|
||||
public String join(User user) {
|
||||
System.out.println(user);
|
||||
user.setRole("ROLE_USER");
|
||||
// 패스워드가 암호화 되지 않으면 시큐리티로 로그인 할 수 없음.
|
||||
String rawPassword = user.getPassword();
|
||||
String encPassword = bCryptPasswordEncoder.encode(rawPassword);
|
||||
user.setPassword(encPassword);
|
||||
userRepository.save(user);
|
||||
return "redirect:/loginForm";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user