728x90
728x90
Spring Security는 Spring 기반의 Java 웹 응용 프로그램에서 보안과 인증을 관리하는 데 사용되는 강력한 보안 프레임워크입니다. Spring Security는 사용자 인증, 권한 부여, 보안 설정, 세션 관리 등과 관련된 기능을 제공하여 웹 응용 프로그램의 보안을 강화합니다.
Spring Security의 주요 기능은 다음과 같습니다.
- 사용자 인증 (Authentication): 사용자가 누구인지 확인하고 사용자의 자격 증명을 검증합니다.
- 권한 부여 (Authorization): 인증된 사용자가 어떤 리소스나 기능에 접근할 수 있는지 결정합니다.
- 보안 설정 (Security Configurations): 보안 규칙을 정의하고 설정하여 어떤 URL에 어떤 보안 수준을 적용할지 결정합니다.
- 보안 이벤트 및 리스너 (Security Events and Listeners): 보안 이벤트에 대한 리스너를 등록하여 특정 이벤트에 대한 작업을 수행할 수 있습니다.
- 세션 관리 (Session Management): 세션 관리 기능을 제공합니다.
먼저, Maven 또는 Gralde 프로젝트에 Spring Security를 추가해야 합니다.
Maven을 사용한다면 `pom.xml` 파일에 다음 의존성을 추가하세요
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Gradle을 사용한다면 `build.gralde` 파일에 다음 의존성을 추가하세요
implementation 'org.springframework.boot:spring-boot-starter-security'
다음으로 Spring Security 구성 파일을 생성합니다.
`SecurityConfig` 클래스를 만들고 여기에 보안 구성을 정의합니다.
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll() // 특정 URL은 모든 사용자에게 허용
.anyRequest().authenticated() // 나머지 URL은 인증된 사용자에게만 허용
.and()
.formLogin()
.loginPage("/login") // 사용자 정의 로그인 페이지
.permitAll()
.and()
.logout()
.permitAll();
}
}
이제 사용자 정의 로그인 페이지를 만들어보겠습니다. `login.html` 파일을 만들고 웹 페이지에 대한 로그인 폼을 정의합니다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<form action="/login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">Login</button>
</form>
</body>
</html>
이제 Spring Security를 사용하여 기본적인 인증 및 권한 부여를 설정한 예제를 만들었습니다.
물론 이것은 매우 간단한 예제일 뿐이며 실제 프로젝트에서는 더 많은 설정이 필요합니다.
필요에 따라 Spring Security의 다양한 기능을 사용하여 웹 응용 프로그램의 보안을 강화할 수 있습니다.
https://spring.io/projects/spring-security
728x90
300x250
'Spring' 카테고리의 다른 글
[Spring; 스프링] Scheduler Cron ( 크론 / 스케줄러 ) (0) | 2023.10.20 |
---|---|
[Spring; 스프링] RequestRejectedException - Spring Security (0) | 2023.10.18 |
[Spring; 스프링] mappedBy 연관관계 (0) | 2023.10.04 |
[Spring; 스프링] SpringBoot JPA Entity (0) | 2023.10.04 |
[Spring; 스프링] @Transactional(readOnly=true) (0) | 2023.10.04 |