본문 바로가기
[ JAVA ]/JAVA Spring Security

[ Spring ] Security 개념

by 환이s 2023. 5. 30.


Security란?

 

 

시큐리티(Security)는 소프트웨어 시스템의 보안과 관련된 개념입니다. 주로 웹 애플리케이션, 모바일 앱, 서버 등에서 사용되며 사용자 인증(Authentication), 권한 부여(Authorization), 데이터 보호 등의 기능을 제공하여 시스템의 안정성과 보안성을 강화합니다.

 

시큐리티는 다양한 보안 측면을 다루며, 주요한 목표는 다음과 같습니다.

 

  • 인증(Authentication) : 사용자의 신원을 확인하고 검증하는 과정입니다. 사용자가 자신을 식별할 수 있는 정보(예 : ID , PASSWORD)를 제공하여 인증을 거칩니다. 주로 사용되는 방법으로는 폼 기반 인증, 기본 인증, OAuth,LDAP 등이 있습니다.

 

  • 권한 부여(Authorization) : 인증된 사용자에 대한 권한을 부여하고, 특정 리소스 또는 기능에 대한 접근 권한을 제어하는 과정입니다. 사용자에게 적절한 권한을 부여하여 민감한 데이터와 기능에 대한 접근을 제한합니다.

 

  • 세션 관리(Session Management) : 사용자의 세션을 관리하고, 세션 유지를 통해 사용자 상태를 추적하고 보호하는 과정입니다. 세션 하이재킹(Session Hijacking)과 같은 공격으로부터 보호하기 위해 세션 식별자를 생성하고 관리합니다.

 

  • 데이터 보호(Data Protection) : 데이터의 기밀성과 무결성을 보장하는 과정입니다. 암호화, 해시 함수, 접근 제어 등을 통해 데이터를 보호합니다.

 

  • 보안 이벤트 및 로깅(Security Events and Logging) : 시스템에서 발생하는 보안 이벤트를 모니터링하고 기록하여 보안 사고를 예빵하고 추적할 수 있습니다.

 

 

시큐리티는 다양한 보안 측면을 다루며, 웹 프레임워크(Spring Security), 애플리케이션 프레임워크(Android Security), 클라우드 보안(AWS Security) 등 다양한 분야에서 사용됩니다. 시큐리티를 적절하게 구현하면 시스템의 안전성을 강화하고 사용자 데이터를 보호할 수 있습니다.

 

다음으로 Spring Security를 사용한 간단한 예제 소스 코드입니다. 이 예제는 Spring Framework Spring Security를 기반으로 코드 구현을 했습니다. 사용자 인증과 권한 부여를 구현한 간단한 보안 설정을 포함하고 있습니다.

 

 

의존성 설정

 

pom.xml 파일에 Spring Security 의존성을 추가합니다.

 

<dependencies>
    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>5.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>5.5.1</version>
    </dependency>
</dependencies>

 

Spring Security 설정 파일

 

Spring Security 설정을 위해 security-config.xml 파일을 작성합니다.

 

<!-- security-config.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">

    <!-- 보안 설정 -->
    <security:http>
        <security:intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
        <security:intercept-url pattern="/user/**" access="hasRole('ROLE_USER')" />
        <security:form-login />
        <security:logout logout-url="/logout" />
    </security:http>

    <!-- 사용자 인증을 위한 메모리 기반 UserDetailsService -->
    <security:user-service>
        <security:user name="admin" password="{noop}admin123" authorities="ROLE_ADMIN" />
        <security:user name="user" password="{noop}user123" authorities="ROLE_USER" />
    </security:user-service>

    <!-- 암호화를 위한 PasswordEncoder 설정 -->
    <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

    <!-- 인증 매니저 설정 -->
    <security:authentication-manager>
        <security:authentication-provider user-service-ref="userDetailsService">
            <security:password-encoder ref="passwordEncoder" />
        </security:authentication-provider>
    </security:authentication-manager>

</beans>

 

웹 애플리케이션 설정

 

web.xml 파일에 Spring Security 필터를 등록합니다.

 

<!-- web.xml -->
<web-app>
    <!-- Spring Security 필터 -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

보호된 페이지 및 접근 권한 설정

 

<Controller>

 

// HomeController.java
@Controller
public class HomeController {
    
    @GetMapping("/")
    public String home() {
        return "home";
    }
    
    @GetMapping("/admin")
    public String admin() {
        return "admin";
    }
    
    @GetMapping("/user")
    public String user() {
        return "user";
    }
    
    @GetMapping("/login")
    public String login() {
        return "login";
    }
    
    @GetMapping("/logout")
    public String logout() {
        return "logout";
    }
    
}

 

<VIEW>

 

- home.jsp

 

<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1>Welcome to Home Page</h1>
    <a href="/admin">Admin Page</a>
    <a href="/user">User Page</a>
</body>
</html>

// admin.jsp
<!DOCTYPE html>
<html>
<head>
    <title>Admin Page</title>
</head>
<body>
    <h1>Welcome to Admin Page</h1>
    <a href="/">Home Page</a>
    <a href="/logout">Logout</a>
</body>
</html>

 

- user.jsp

 

<!DOCTYPE html>
<html>
<head>
    <title>User Page</title>
</head>
<body>
    <h1>Welcome to User Page</h1>
    <a href="/">Home Page</a>
    <a href="/logout">Logout</a>
</body>
</html>

 

- login.jsp

 

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h1>Login Page</h1>
    <form action="/login" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required>
        <br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required>
        <br>
        <input type="submit" value="Login">
    </form>
</body>
</html>

 

- logout.jsp

 

<!DOCTYPE html>
<html>
<head>
    <title>Logout</title>
</head>
<body>
    <h1>Logout Successful</h1>
    <a href="/">Home Page</a>
</body>
</html>

 

위의 예제는 간단한 Spring Security 설정을 보여줍니다. 사용자의 권한에 따라 접근할 수 있는 페이지가 제한되고, 로그인과 로그아웃 기능을 구현하였습니다. 설정 파일에 정의된 사용자 정보를 기반으로 인증을 수행하며, BCryptPasswordEncoder를 사용하여 비밀번호를 암호화합니다.

 

또 다른 예시로는 Spring BootThymeleaf를 기반으로 알아봅시다.

 

의존성 설정

 

pom.xml 파일에 Spring Security 의존성을 추가합니다.

 

<dependencies>
    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>

 

보안 설정

 

 SecurityConfig.java 파일을 생성하여 보안 설정을 구성합니다.

 

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN") // "/admin/**" 경로는 ADMIN 역할을 가진 사용자만 접근 가능
            .antMatchers("/user/**").hasAnyRole("ADMIN", "USER") // "/user/**" 경로는 ADMIN 또는 USER 역할을 가진 사용자만 접근 가능
            .anyRequest().authenticated() // 그 외의 모든 요청은 인증을 필요로 함
            .and()
            .formLogin(); // 로그인 폼 사용
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("admin").password("{noop}admin123").roles("ADMIN") // 사용자 "admin"에게 ADMIN 역할 부여
            .and()
            .withUser("user").password("{noop}user123").roles("USER"); // 사용자 "user"에게 USER 역할 부여
    }
}

 

웹 페이지 작성

 

Spring Security를 적용한 웹 페이지를 작성합니다.

 

<!-- index.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <h1>Welcome to the Homepage!</h1>
    <a th:href="@{/admin}">Admin Page</a> <!-- ADMIN 역할 사용자만 접근 가능한 링크 -->
    <a th:href="@{/user}">User Page</a> <!-- ADMIN 또는 USER 역할 사용자만 접근 가능한 링크 -->
    <a th:href="@{/logout}">Logout</a> <!-- 로그아웃 링크 -->
</body>
</html>

<!-- admin.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <h1>Welcome to the Admin Page!</h1>
    <a th:href="@{/}">Back to Homepage</a>
</body>
</html>

<!-- user.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <h1>Welcome to the User Page!</h1>
    <a th:href="@{/}">Back to Homepage</a>
</body>
</html>

 

위 예제는 간단한 웹 애플리케이션을 구성하고, "/admin" 경로는 ADMIN 역할을 가진 사용자만 접근할 수 있도록 제한하고, "/user" 경로는 ADMIN 또는 USER 역할을 가진 사용자만 접근할 수 있도록 제한하는 예제입니다. 사용자 인증은 메모리에 미리 정의된 사용자와 역할을 사용하며, 로그인 폼을 사용하여 인증합니다.

 

실제로 실행하려면 Spring Boot 애플리케이션을 실행하고, 웹 브라우저에서 http://localhost:8080/에 접속하시면 동작을 확인할 수 있습니다.

 


마치며

 

오늘은 시큐리티에 대해서 알아보았습니다.

다음 포스팅에서 뵙겠습니다.

 

728x90