๐Ÿ“Œ Spring Security ๊ธฐ์ดˆ ์„ค์ •

My Pale Blue Dotยท2025๋…„ 5์›” 15์ผ
0

SPRING BOOT

๋ชฉ๋ก ๋ณด๊ธฐ
34/40
post-thumbnail

๐Ÿ“… ๋‚ ์งœ

2025-05-15


๐Ÿ“ ํ•™์Šต ๋‚ด์šฉ

0๏ธโƒฃ STS3 vs Spring Boot 3.x ํ™˜๊ฒฝ ๋น„๊ต

๊ตฌ๋ถ„STS3 (Spring 5) ํ™˜๊ฒฝSpring Boot 3.x ํ™˜๊ฒฝ
์„ค์ • ์Šคํƒ€์ผXML ๋˜๋Š” Java Config ํ˜ผํ•ฉJava Config ์ค‘์‹ฌ, ์ตœ์†Œ XML ์‚ฌ์šฉ
๋ถ€ํŠธ์ŠคํŠธ๋žฉ ๋ฐฉ์‹์ˆ˜๋™ ์„ค์ • (DispatcherServlet ๋“ฑ ์ˆ˜๋™ ๊ตฌ์„ฑ)์ž๋™ ์„ค์ • (Spring Boot Auto Configuration)
Spring Security๋ณดํ†ต 5.x ๋ฒ„์ „ ์‚ฌ์šฉ๊ธฐ๋ณธ์ ์œผ๋กœ 6.x ์ด์ƒ ์‚ฌ์šฉ
์„œ๋ธ”๋ฆฟ ์ŠคํŽ™Servlet 3.0 ๋˜๋Š” 3.1Servlet 5.0 ์ด์ƒ ์š”๊ตฌ
JDK ๋ฒ„์ „8 ~ 1117 ์ด์ƒ (๊ธฐ๋ณธ ์š”๊ตฌ์‚ฌํ•ญ)

1๏ธโƒฃ ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ์„ค์ •

โœ… build.gradle ์ฃผ์š” ์„ค์ •

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.4.5'
	id 'io.spring.dependency-management' version '1.1.7'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(21)
	}
}

dependencies {
	// DB
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-jdbc'
	runtimeOnly 'com.mysql:mysql-connector-j'

	// Security
	implementation 'org.springframework.boot:spring-boot-starter-security'
	implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'

	// Web, Thymeleaf
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

	// Lombok
	annotationProcessor 'org.projectlombok:lombok'
	compileOnly 'org.projectlombok:lombok'

	// Test
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.springframework.security:spring-security-test'
}

2๏ธโƒฃ application.properties

spring.application.name=demo
server.port=8090

# UTF-8 ํ•„ํ„ฐ ์„ค์ •
spring.servlet.filter.encoding.filter-name=encodingFilter
spring.servlet.filter.encoding.filter-class=org.springframework.web.filter.CharacterEncodingFilter
spring.servlet.filter.encoding.init-param.encoding=UTF-8
spring.servlet.filter.encoding.init-param.forceEncoding=true
spring.servlet.filter.encoding.url-pattern=/*

3๏ธโƒฃ ๋ฐ์ดํ„ฐ์†Œ์Šค ์„ค์ • (HikariCP)

@Configuration
public class DataSourceConfig {

    @Bean
    public HikariDataSource dataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/testdb");
        dataSource.setUsername("root");
        dataSource.setPassword("1234");
        return dataSource;
    }
}

4๏ธโƒฃ JPA ์„ค์ •

@Configuration
@EntityScan(basePackages = {"com.example.demo.domain.entity"})
@EnableJpaRepositories(
        basePackages = {"com.example.demo.domain.repository"},
        transactionManagerRef = "jpaTransactionManager"
)
public class JpaConfig {
    @Autowired
    private DataSource dataSource;

    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setDataSource(dataSource);
        factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        factory.setPackagesToScan("com.example.demo.domain.entity");

        Map<String, Object> props = new HashMap<>();
        props.put("hibernate.hbm2ddl.auto", "update");
        props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        props.put("hibernate.show_sql", true);
        props.put("hibernate.format_sql", true);
        factory.setJpaPropertyMap(props);

        return factory;
    }
}

5๏ธโƒฃ ํŠธ๋žœ์žญ์…˜ ์„ค์ •

@Configuration
@EnableTransactionManagement
public class TxConfig {

    @Autowired
    private DataSource dataSource;

    @Bean(name="jpaTransactionManager")
    public JpaTransactionManager jpaTransactionManager(EntityManagerFactory emf) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(emf);
        txManager.setDataSource(dataSource);
        return txManager;
    }
}

6๏ธโƒฃ ํด๋” ๊ตฌ์กฐ

src/
 โ””โ”€โ”€ main/
      โ”œโ”€โ”€ java/com/example/demo/
      โ”‚    โ”œโ”€โ”€ config/
      โ”‚    โ”‚    โ”œโ”€โ”€ DataSourceConfig.java
      โ”‚    โ”‚    โ”œโ”€โ”€ JpaConfig.java
      โ”‚    โ”‚    โ””โ”€โ”€ TxConfig.java
      โ”‚    โ”œโ”€โ”€ controller/
      โ”‚    โ””โ”€โ”€ domain/
      โ”‚         โ”œโ”€โ”€ dto/
      โ”‚         โ”œโ”€โ”€ entity/
      โ”‚         โ””โ”€โ”€ repository/
      โ””โ”€โ”€ resources/
           โ”œโ”€โ”€ static/
           โ”œโ”€โ”€ templates/
           โ”‚    โ”œโ”€โ”€ home.html
           โ”‚    โ”œโ”€โ”€ join.html
           โ”‚    โ””โ”€โ”€ login.html
           โ””โ”€โ”€ application.properties

7๏ธโƒฃ Spring Security ๊ธฐ๋ณธ ๋™์ž‘ ํ™•์ธ

  • ๋ธŒ๋ผ์šฐ์ € ์ ‘์† ์‹œ (http://localhost:8090) Spring Security ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ ์ฐฝ ์ถœ๋ ฅ
  • ๊ธฐ๋ณธ ์‚ฌ์šฉ์ž ID: user
  • ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์„œ๋ฒ„ ์‹คํ–‰ ์‹œ ์ฝ˜์†”์—์„œ ์ถœ๋ ฅ๋จ
    Using generated security password: **************
    

๐Ÿ”ฅ ์ •๋ฆฌ

  • Spring Boot 3.x + Spring Security 6.x ๊ธฐ์ค€ ์ดˆ๊ธฐ ์„ค์ • ์™„์„ฑ
  • ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์ถœ๋ ฅ๊นŒ์ง€ ํ™•์ธ๋จ
  • ๋‹ค์Œ ๋‹จ๊ณ„์—์„œ๋Š” SecurityConfig ์„ค์ • ๋ฐ ์‚ฌ์šฉ์ž ์ธ์ฆ ์ฒ˜๋ฆฌ๋กœ ํ™•์žฅ ์˜ˆ์ •

๐Ÿ”— ์ฐธ๊ณ  ์ž๋ฃŒ


profile
Here, My Pale Blue.๐ŸŒ

0๊ฐœ์˜ ๋Œ“๊ธ€