본문 바로가기

Spring Boot/Security

[SpringBoot Secyruty] 환경 설정

 

1.데이터베이스, 사용자, 권한 생성

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

 

2.SpringBoot 프로젝트 생성

Finish 클릭 ㄱㄱ

 

2.Application 파일 설정 변경

Application.properties -> properties.yml 변경

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
    
  mvc:
    view:
      prefix: /templates/
      suffix: .mustache

  jpa:
    hibernate:
      ddl-auto: update #create 시작할 때 마다 DB 초기화, update 시작할 때 마다 DB 초기화하지 않음, none
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    show-sql: true

 

3.Contrroller 생성 ㄱㄱ

@Controller
public class IndexController {

	// localhost:8080
    // localhost:8080/
	@GetMapping({"","/"})
	public String index() {
		
		return "index";
	}
}

 

4.View 화면 생성 ㄱㄱ

 

5.Web 설정 ㄱㄱ

package com.goodbam.security1.config;


import org.springframework.boot.web.servlet.view.MustacheViewResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		MustacheViewResolver resolver = new MustacheViewResolver();
		resolver.setCharset("UTF-8"); // 인코딩
		resolver.setContentType("text/html; charset=UTF-8");
		resolver.setPrefix("classpath:/templates/"); // 폴더
		resolver.setSuffix(".html"); // 확장자 이름
		
		registry.viewResolver(resolver); 
	}
}

 

6.프로젝트 실행

콘솔창에 보이는 Password 복사 ㄱㄱ
Chrome 브라우저 주소창에 localhost:8080 ㄱㄱ
아이디 : user,  비밀번호 : 아까 복사해둔 password 붙여넣기 ㄱㄱ

 

 

-끝-