관리 메뉴

bright jazz music

[Spring security] 1.1 프로젝트 구성과 의존성 추가 본문

Framework/Spring Security

[Spring security] 1.1 프로젝트 구성과 의존성 추가

bright jazz music 2022. 9. 8. 22:09

1.1. 프로젝트 구성

 

1.2. 테스트를 위한 컨트롤러 생성(SecurityController.java)

package io.security.basicsecurity;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SecurityController {

    @GetMapping("/")
    public String index(){
        return "home";
    }
}

 

 

1.3. main 메서드 실행

 

브라우저를 열고 아래의 주소 입력

http://localhost:8080

 

==> 아직 스프링 시큐리티를 적용하지 않았기 때문에 자유롭게 진입할 수 있다,

 

 

 

1.4. Spring Security 의존성 추가

pom.xml 파일에 아래의 코드를 삽입하여 의존성 추가

<!-- pom.xml -->


<!-- Spring security 의존성 추가 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

 

아래는 pom.xml 파일 전문

<!-- pom.xml 전문 -->

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.security</groupId>
    <artifactId>basicsecurity</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>basicsecurity</name>
    <description>basicsecurity</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring security 의존성 추가 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

1.5. 스프링 시큐리티 적용 후 확인

서버를 재구동한다. 메인 메소드를 다시 시작해도 된다.

그러면 콘솔창에 아래와 같은 로그가 발생한다.

 

예시)
Using generated security password: 90a415b2-3ab7-4630-9b18-ac94835e0ecc

이것은 추후 기본 로그인 화면에서 사용할 비밀번호이다.

 

구동이 완료되면 브라우저의 주소창에 localhost:8080을 기입하고 접근한다.

 

기본 아이디는 user이다.

비밀번호는 콘솔창에 생성된 로그를 이용한다.

 

ID: user

PW: 90a415b2-3ab7-4630-9b18-ac94835e0ecc

 

Sign in 버튼을 누르면 이전과 동일한 화면에 진입한다.

 

Comments