๐Ÿ“‹ Project

[Spring boot] (์—๋ธŒ๋ฆฌํƒ€์ž„ ํด๋ก ์ฝ”๋”ฉ) + MySQL, ํšŒ์› ๊ฐ€์ž… ๊ตฌํ˜„(2)

sun_young 2023. 7. 15. 14:51

[์ฐธ๊ณ ] ์•„๋ž˜ ์ฒจ๋ถ€ํ•œ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑ๋œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค!

https://inkyu-yoon.github.io/docs/Language/SpringBoot/SpringSecurityJoin

 

· Spring Security๋ฅผ ์ด์šฉํ•œ ํšŒ์›๊ฐ€์ž… ๊ตฌํ˜„

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ์ง€์‹ ์ฐฝ๊ณ  ๐Ÿ“š

inkyu-yoon.github.io

 

์›๋ž˜ (1)์—์„œ ์ฐธ๊ณ ํ•œ ์ฑ…์ด ์žˆ๋Š”๋ฐ security๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์ดํ•ด๊ฐ€ ์•ˆ๋˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์–ด์„œ ๊ตฌ๊ธ€๋งํ•˜๋‹ค๊ฐ€ ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋˜์—ˆ๋‹คใ… ใ… 

 

1. ์˜์กด์„ฑ ์ถ”๊ฐ€

implementation 'org.springframework.security:spring-security-test'
implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'

 

2. security ์ฝ”๋“œ ์ถ”๊ฐ€

@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
        return httpSecurity
                .httpBasic().disable()
                .csrf().disable()
                .cors().and()
                .authorizeRequests()
                .antMatchers("user/register", "user/login").permitAll()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .build();
    }

security์— securityFilterChain์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

 

csrf() : ์ •์ƒ์ ์ธ ์‚ฌ์šฉ์ž๊ฐ€ ์˜๋„์น˜ ์•Š์€ ์œ„์กฐ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ๊ฒƒ์„ ์˜๋ฏธ

→ disable() : rest api๋ฅผ ์ด์šฉํ•œ ์„œ๋ฒ„๋ผ๋ฉด, ์„œ๋ฒ„์— ์ธ์ฆ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ๋ถˆ ํ•„์š”ํ•œ csrf ์ฝ”๋“œ ์ž‘์„ฑํ•  ํ•„์š” ์—†์Œ

์ฐธ๊ณ  >> https://velog.io/@woohobi/Spring-security-csrf%EB%9E%80

 

authorizeRequests() : ์‹œํ๋ฆฌํ‹ฐ ์ฒ˜๋ฆฌ์— HttpServletRequest๋ฅผ ์ด์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธ

antMatchers() : ํŠน์ •ํ•œ ๊ฒฝ๋กœ ์ง€์ •

permitAll() : ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘๊ทผ ๊ฐ€๋Šฅ

์ฐธ๊ณ  >> https://velog.io/@jayjay28/2019-09-04-1109-%EC%9E%91%EC%84%B1%EB%90%A8

 

3. controller ์ƒ์„ฑ

package com.example.everytime.controller;

import com.example.everytime.DTO.UserDto;
import com.example.everytime.entity.User;
import com.example.everytime.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;

@RequestMapping("/user")
@Controller
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    private final PasswordEncoder passwordEncoder;

    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping("/register")
    public void userRegister(@RequestBody UserDto userDto) {
        User user = User.createUser(userDto, passwordEncoder);
        User savedUser = userService.saveUser(user);
    }
}

/user/register๋กœ post ์š”์ฒญ์„ ํ•˜๋ฉด ํšŒ์›๊ฐ€์ž… ๋!

 

โ‘  @RequestBody์—์„œ dto๋กœ ํšŒ์› ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜จ๋‹ค

โ‘ก dto๋ฅผ  User.createUser์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฝ์ž…ํ•˜์—ฌ ์—”ํ‹ฐํ‹ฐ๋กœ ๋ณ€๊ฒฝ, ์ด๋•Œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์•”ํ˜ธํ™”ํ•œ๋‹ค

โ‘ข Serivce์— ๊ตฌํ˜„๋œ saveUser๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‚ฝ์ž…ํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ ํšŒ์› ์ •๋ณด๋ฅผ db์— ์ €์žฅ

* ์ด๋•Œ mysql์— ์‚ฝ์ž…ํ•˜๋Š” ๊ณผ์ •์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”๋ฐ (์ •ํ™•ํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ญ์˜€๋Š”์ง€ ๊ธฐ์–ต์ด ์•ˆ ๋‚จ...) ๊ทธ๊ฑด ๊ทธ๋ƒฅ ๋ฐ์ดํ„ฐ์— ๋น„ํ•ด ์ปฌ๋Ÿผ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ž‘์•„์„œ ์ƒ๊ธด ์˜ค๋ฅ˜๋กœ,  ํฌ๊ธฐ๋ฅผ ๋Š˜๋ ค์ฃผ๋ฉด ๋œ๋‹ค 

 

 

4. ๊ฒฐ๊ณผ

์„ฑ๊ณต!