Securing a Web Application 가이드 번역(2) 시작하기

전주은·2023년 5월 8일
0

👀들어가기 전에..

그룹웨어를 스프링 시큐리티를 적용하여 만들기 위해 공부 목적으로 작성된 글입니다. 공식 가이드 문서를 보고 이해한 후 번역하여 적어 오류가 생길 수 있습니다. 만약 문제사항을 댓글을 남겨주신다면, 확인 후 수정하겠습니다. 중간에 생략해도 되는 부분은 과감하게 삭제했습니다.

🚩 무엇을 만들 것인가?

사용자 리스트가 지원하는 login form으로 웹 페이지를 보호하는 Spring MVC 어플리케이션

🙋‍♀️ 준비물

  • 약 15분
  • 선호하는 텍스트 편집기 또는 IDE
  • 자바 17 이상
  • Gradle 7.5+ 또는 Maven 3.5+

Spring Initializr로 시작하기

Step1. https://start.spring.io 으로 이동
Step2. Gradle-Groovy, Java17, spring Boot 2.7.11, Jar로 설정
Step3. Dependencies에서 종속성 추가
- Spring Security
- Spring Web
Step4. Generate 클릭
step5. ZIP파일 다운로드 받기

Secure을 적용할 웹 페이지 만들기

2개의 웹 페이지를 생성한다.

  • home.html (src/main/resources/templates/home.html)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
    <head>
        <title>Spring Security Example</title>
    </head>
    <body>
        <h1>Welcome!</h1>

        <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
    </body>
</html>
  • hello.html (src/main/resources/templates/hello.html)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h1>Hello world!</h1>
    </body>
</html>

이 웹 프로젝트는 Spring MVC를 바탕으로 만들어져 MvcConfig.java 파일을 만들어 주겠다. (src/main/java/com/example/securingweb/MvcConfig.java)

package com.example.securingweb;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/home").setViewName("home");
		registry.addViewController("/").setViewName("home");
		registry.addViewController("/hello").setViewName("hello");
		registry.addViewController("/login").setViewName("login");
	}

}
  • addViewControllers() 메소드: 4개의 view controller 추가: "Run the Application"과 연결되어 로그인 없이 application을 실행할 수 있다.

Spring Security 시작하기

  • Build.gradle
plugins {
	id 'java'
	id 'org.springframework.boot' version '3.0.0'
	id 'io.spring.dependency-management' version '1.1.0'
}

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

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-security'
	//  Temporary explicit version to fix Thymeleaf bug
	implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.1.RELEASE'
	implementation 'org.springframework.security:spring-security-test'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
	useJUnitPlatform()
}
  • WebSecurityConfig.java
package com.example.securingweb;

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.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		http
			.authorizeHttpRequests((requests) -> requests
				.requestMatchers("/", "/home").permitAll()
				.anyRequest().authenticated()
			)
			.formLogin((form) -> form
				.loginPage("/login")
				.permitAll()
			)
			.logout((logout) -> logout.permitAll());

		return http.build();
	}

	@Bean
	public UserDetailsService userDetailsService() {
		UserDetails user =
			 User.withDefaultPasswordEncoder()
				.username("user")
				.password("password")
				.roles("USER")
				.build();

		return new InMemoryUserDetailsManager(user);
	}
}
  • @EnableWebSecurity : Spring Security의 웹 보안 지원을 활성화하고 Spring MVC와의 통합을 제공한다.

  • SecurityFilterChain Bean : 어떤 URL 경로가 보안되어야 하는지, 어떤 것이 보안되지 않아도 되는지 정의하는 데 사용된다.

  • UserDetailsService 빈 : 단일 사용자로 구성된 인메모리 사용자 저장소를 설정하는 데 사용된다. 이 사용자는 "user" 사용자 이름, "password" 비밀번호 및 "USER" 역할을 부여받는다.

  • login.html(src/main/resources/templates) 로그인 화면 구현

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
    <head>
        <title>Spring Security Example </title>
    </head>
    <body>
        <div th:if="${param.error}">
            Invalid username and password.
        </div>
        <div th:if="${param.logout}">
            You have been logged out.
        </div>
        <form th:action="@{/login}" method="post">
            <div><label> User Name : <input type="text" name="username"/> </label></div>
            <div><label> Password: <input type="password" name="password"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
        </form>
    </body>
</html>

0개의 댓글