728x90
๋ฐ˜์‘ํ˜•

์‹ค์Šต ๋ชฉํ‘œ ๋ฐ ๊ฐ„๋‹จํ•œ ๋™์ž‘ ์›๋ฆฌ

 

์‹ค์Šต ๋ชฉํ‘œ

: ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ธ์ฆ, ์ธ๊ฐ€๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ํšŒ์› ์ •๋ณด ์ €์žฅ(์˜์†์„ฑ)์€ MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค

 

๊ตฌํ˜„

- ์ธ์ฆ : ๋กœ๊ทธ์ธ

- ์ธ๊ฐ€ : ๊ฒฝ๋กœ๋ณ„ ์ ‘๊ทผ ๊ถŒํ•œ

- ํšŒ์›๊ฐ€์ž…

 

์‹œํ๋ฆฌํ‹ฐ ๋™์ž‘ ์›๋ฆฌ

๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด ๋ณด์ž๋ฉด....

 

Spring Security Config๋ผ๋Š” filter๋ฅผ ๋งŒ๋“ค์–ด ๋†“์œผ๋ฉด client์˜ ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„์„œ client๊ฐ€ ๊ฐ€๊ณ ์‹ถ์€ ๋ชฉ์ ์ง€ ์ด์ „์— ํŠน์ •ํ•œ ๊ถŒํ•œ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๋ถ„์„์„ ํ•˜๊ณ  ๊ถŒํ•œ์„ ํ™•์ธ์„ ํ•˜์—ฌ ๋ง‰๋“ ์ง€ ํ—ˆ์šฉํ•˜๋“ ์ง€ ํ•ฉ๋‹ˆ๋‹ค(์„ธ์…˜์— ๋กœ๊ทธ์ธ ์ •๋ณด ๋ƒ„๊น€)

 

๋ฒ„์ „

- Spring Boot 3.1.5

- Security 6.1.5

- Spring Data JPA - MySQL

- mustache

- IntelliJ Ultimate

 

 

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

์˜์กด์„ฑ

- Spring Web

- Lombok

- Mustache

- Spring Security

- Spring Data JPA

- MySQL Driver

 

 

main page

 

package com.example.testsecurity.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MainController {

    @GetMapping("/")
    public String mainP() {

        return "main";
    }
}

 

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Main Page</title>
</head>
<body>
main page
</body>
</html>

 

์•„์ด๋”” : user / ๋น„๋ฐ€๋ฒˆํ˜ธ : ์ธํ…”๋ฆฌ์ œ์ด์—์„œ ์ค€๊ฑธ ๋“ค์–ด๊ฐ€๋ฉด ๋งŒ๋“  ํ™”๋ฉด์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!!

 

 

Security Config ํด๋ž˜์Šค

 

 

์ธ๊ฐ€

 

ํŠน์ • ๊ฒฝ๋กœ์— ์š”์ฒญ์ด ์˜ค๋ฉด Controller ํด๋ž˜์Šค์— ๋„๋‹ฌํ•˜๊ธฐ ์ „ ํ•„ํ„ฐ์—์„œ Spring Security๊ฐ€ ๊ฒ€์ฆ์„ ํ•จ

 

1. ํ•ด๋‹น ๊ฒฝ๋กœ์˜ ์ ‘๊ทผ์€ ๋ˆ„๊ตฌ์—๊ฒŒ ์—ด๋ ค ์žˆ๋Š”์ง€

2, ๋กœ๊ทธ์ธ์ด ์™„๋ฃŒ๋œ ์‚ฌ์šฉ์ž์ธ์ง€

3. ํ•ด๋‹น๋˜๋Š” role์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€

 

 

์‹œํ๋ฆฌํ‹ฐ Config ํด๋ž˜์Šค ์ž‘์„ฑ

 

package com.example.testsecurity.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.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{

        http
                .authorizeHttpRequests((auth) -> auth
                        .requestMatchers("/", "/login").permitAll() 		// permitAll() : ๋ชจ๋‘ ํ—ˆ์šฉ
                        .requestMatchers("/admin").hasRole("ADMIN")			// hasRole() : ํ•ด๋‹น ์—ญํ• ์„ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž๋งŒ
                        .requestMatchers("/my/**").hasAnyRole("ADMIN", "USER") // hasAnyRole() : ํ•ด๋‹น ์—ญํ• ๋“ค์„ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž๋“ค๋งŒ
                        .anyRequest().authenticated()	// anyRequest() : ์ด์™ธ์— ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•œ ๋กœ์ง๋“ค / authenticated : ๋กœ๊ทธ์ธํ•œ ๋ชจ๋“  ์‚ฌ์šฉ์ž
                );

        return http.build();
    }
}

 

์ƒ๋‹จ ๋ถ€ํ„ฐ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์„œ์— ์œ ์˜ํ•˜์—ฌ ์ ์–ด์•ผ ํ•œ๋‹ค(๋ฐ‘์— ์ฝ”๋“œ๋“ค์ด ์•ˆ ๋จน์„ ์ˆ˜ ์žˆ์Œ)

 

 

์ถ”๊ฐ€ ํŽ˜์ด์ง€ ์ƒ์„ฑ

 

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class AdminController {

    @GetMapping("/admin")
    public String admin() {

        return "admin";
    }
}

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>admin Page</title>
</head>
<body>
admin Page
</body>
</html>

 

admin ํŽ˜์ด์ง€์— ๋”ฐ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Œ... ๋”ฐ๋กœ role์„ ์„ค์ •ํ•ด ์ฃผ์ง€ ์•Š์•„์†Œ... 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts