Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions refrigerator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ dependencies {
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

implementation 'org.modelmapper:modelmapper:3.1.0'

implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
implementation 'io.jsonwebtoken:jjwt-impl:0.12.3'
implementation 'io.jsonwebtoken:jjwt-jackson:0.12.3'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import jakarta.persistence.*;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDate;

@Entity
@Table(name = "tbl_user")
Expand All @@ -11,4 +14,23 @@ public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_pk")
private long userPk;

@Column(name = "user_id", nullable = false, unique = true)
private String userId;

@Column(name = "user_pw", nullable = false)
private String userPw;

@Column(name = "user_email", nullable = false, unique = true)
private String userEmail;

@Column(name = "user_nickname", nullable = false, unique = true)
private String userNickname;

@Column(name = "join_date", nullable = false, updatable = false)
@CreationTimestamp
private LocalDate joinDate;

// @Column(name = "leave_date")
// private LocalDate leaveDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package moja.refrigerator.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
// 비밀번호 암호화를 위한 인코더
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// csrf 보안 비활성화
http
.csrf((auth) -> auth.disable());

// 기본 로그인 폼 비활성화
http
.formLogin((auth) -> auth.disable());

// HTTP Basic 인증 비활성화
http
.httpBasic((auth) -> auth.disable());

// URL 별 접근 권한 설정
http
.authorizeHttpRequests((auth) -> auth
.requestMatchers("/login", "/", "/join").permitAll() // 이 경로들은 모두 접근 가능
.requestMatchers("/admin").hasRole("ADMIN") // admin 경로는 ADMIN 역할을 가진 사용자만
.anyRequest().authenticated()); // 나머지는 인증된 사용자만

// 세션 관리 설정
http
.sessionManagement((session) -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)); // JWT 사용을 위한 세션리스 설정

return http.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package moja.refrigerator.controller.user;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminController {
@GetMapping("/admin")
public String getAdminPage() {
return "admin Controller";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package moja.refrigerator.controller.user;

import moja.refrigerator.service.user.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
private final UserService userService;

public UserController(UserService userService) {
this.userService = userService;
}

// @GetMapping("/")
// public String getMainPage() {
// return "user Controller";
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import moja.refrigerator.aggregate.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

public interface UserRepository extends JpaRepository<User, Long>{
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package moja.refrigerator.service.user;

public interface UserService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package moja.refrigerator.service.user;

import moja.refrigerator.repository.user.UserRepository;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final BCryptPasswordEncoder passwordEncoder;

public UserServiceImpl(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
}