join with security
This commit is contained in:
@@ -1,14 +1,22 @@
|
|||||||
package com.spring.security1.config;
|
package com.spring.security1.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
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.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity // 스프링 시큐리티 필터(SecurityConfig)가 스프링 필터 체인에 등록
|
@EnableWebSecurity // 스프링 시큐리티 필터(SecurityConfig)가 스프링 필터 체인에 등록
|
||||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public BCryptPasswordEncoder encodePwd() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
http.csrf().disable();
|
http.csrf().disable();
|
||||||
@@ -19,7 +27,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
.anyRequest().permitAll()
|
.anyRequest().permitAll()
|
||||||
.and()
|
.and()
|
||||||
.formLogin()
|
.formLogin()
|
||||||
.loginPage("/login");
|
.loginPage("/loginForm");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,20 @@
|
|||||||
package com.spring.security1.controller;
|
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.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class IndexController {
|
public class IndexController {
|
||||||
|
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
||||||
|
|
||||||
@GetMapping({"", "/"})
|
@GetMapping({"", "/"})
|
||||||
public String index() {
|
public String index() {
|
||||||
@@ -33,22 +41,26 @@ public class IndexController {
|
|||||||
return "manager";
|
return "manager";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ResponseBody
|
@GetMapping("/loginForm")
|
||||||
@GetMapping("/login")
|
public String loginForm() {
|
||||||
public String login() {
|
return "loginForm";
|
||||||
return "login";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/joinForm")
|
||||||
@ResponseBody
|
public String joinForm() {
|
||||||
@GetMapping("/join")
|
return "joinForm";
|
||||||
public String join() {
|
|
||||||
return "join";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ResponseBody
|
@PostMapping("/join")
|
||||||
@GetMapping("/joinProc")
|
public String join(User user) {
|
||||||
public String joinProc() {
|
System.out.println(user);
|
||||||
return "회원가입 완료!";
|
user.setRole("ROLE_USER");
|
||||||
|
// 패스워드가 암호화 되지 않으면 시큐리티로 로그인 할 수 없음.
|
||||||
|
String rawPassword = user.getPassword();
|
||||||
|
String encPassword = bCryptPasswordEncoder.encode(rawPassword);
|
||||||
|
user.setPassword(encPassword);
|
||||||
|
userRepository.save(user);
|
||||||
|
return "redirect:/loginForm";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.spring.security1.model;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.hibernate.annotations.CreationTimestamp;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class User {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private int id;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
private String email;
|
||||||
|
private String role; // ROLE_USER, ROLE_ADMIN
|
||||||
|
|
||||||
|
@CreationTimestamp
|
||||||
|
private Timestamp createDate;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.spring.security1.repository;
|
||||||
|
|
||||||
|
import com.spring.security1.model.User;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
// CRUD 함수를 JpaRepository가 들고 있음.
|
||||||
|
// JpaRepository를 상속 했기 때문에 @Repository 어노테이션이 없어도 IoC된다.
|
||||||
|
@Repository
|
||||||
|
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||||
|
}
|
||||||
17
spring-security/src/main/resources/templates/joinForm.html
Normal file
17
spring-security/src/main/resources/templates/joinForm.html
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>회원가입 페이지</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>회원가입 페이지</h1>
|
||||||
|
<hr/>
|
||||||
|
<form action="/join" method="post">
|
||||||
|
<input type="text" name="username" placeholder="Username"/> <br/>
|
||||||
|
<input type="password" name="password" placeholder="Password"/> <br/>
|
||||||
|
<input type="email" name="email" placeholder="Email"/> <br/>
|
||||||
|
<button>회원가입</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
17
spring-security/src/main/resources/templates/loginForm.html
Normal file
17
spring-security/src/main/resources/templates/loginForm.html
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>로그인 페이지</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>로그인 페이지</h1>
|
||||||
|
<hr/>
|
||||||
|
<form>
|
||||||
|
<input type="text" name="username" placeholder="Username"/> <br/>
|
||||||
|
<input type="password" name="password" placeholder="Password"/> <br/>
|
||||||
|
<button>로그인</button>
|
||||||
|
</form>
|
||||||
|
<a href="/joinForm">회원가입을 아직 하지 않으셨나요?</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user