[Spring Security] 기본 설정하기

yesjm·2022년 12월 13일
0
post-thumbnail

인프런 스프링부트 시큐리티 & JWT 강의를 수강하며 작성하는 내용입니다.


기본 설정

MySQL 설치

> brew install mysql
> brew services start mysql

DataGrip에 추가

DB 생성

create user 'cos'@'%' identified by 'cos1234';
GRANT ALL PRIVILEGES ON *.* TO 'cos'@'%';
create database security;
use security;

프로젝트 생성 및 설정

  • kotlin, gradle 선택해주었습니다.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.7.6"
    id("io.spring.dependency-management") version "1.0.15.RELEASE"
    kotlin("jvm") version "1.6.21"
    kotlin("plugin.spring") version "1.6.21"
    kotlin("plugin.jpa") version "1.6.21"
}

group = "com.cos"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-mustache")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("com.mysql:mysql-connector-j")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.security:spring-security-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}
## application.yaml
server:
  port: 8080
  servlet:
    context-path: /
    encoding:
      charset: UTF-8
      enabled: true
      force: true

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/security?serverTimezone=Asia/Seoul
    username: cos
    password: cos1234

  jpa:
    hibernate:
      ddl-auto: create #create update none
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    show-sql: true

소스코드

@Controller // view를 리턴하겠다
class IndexController {

    // localhost:8080
    @GetMapping("/")
    fun index(): String {
        // 머스테치 기본폴더 src/main/resources/
        // view resolver 설정: templates (prefix), .mustache (suffix) 생략가능
        return "index" // src/main/resources/templates/index.mustache
    }
}
@Configuration
class WebMvcConfig : WebMvcConfigurer{

    override fun configureViewResolvers(registry: ViewResolverRegistry) {
        val resolver = MustacheViewResolver()
        resolver.setCharset("UTF-8")
        resolver.setContentType("text/html; charset=UTF-8")
        resolver.setPrefix("classpath:/templates/")
        resolver.setSuffix(".html")

        registry.viewResolver(resolver)
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>인덱스페이지</title>
</head>
<body>
    <h1>인덱스페이지입니다.</h1>
</body>
</html>

결과화면

  • http://localhost:8080 로 접근할 시 login 화면이 출력됩니다.
  • 로그인에 필요한 정보는 Username: user, password는 스프링 부트가 실행될 때 콘솔창에 출력되는 비밀번호를 입력해야합니다.


profile
yesjm's second brain

0개의 댓글