티스토리 뷰

Spring

Spring Security (Spring Boot) #1

§무명소졸§ 2020. 1. 3. 13:47

들어가면

Spring Boot에서 Spring Security를 사용하는법을 알아보려한다. 이론적인 부분은 제외하고 코드중심으로 아무런 설정도 없는 기본 상태에서 살을 붙혀가면서 작업을 할 예정이다.

설정

  • Spring Boot 2.2.2.RELEASE

  • Java 1.8

POM.xml

가장 기본적인 Spring WEB , Security 와 UI 처리를 위한 template engine 인 thymeleaf를 포함하고있다.

<dependencies>
  <dependency>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-security</artifactId>
  </dependency>
  <dependency>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <dependency>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

테스트 Controller

테스트를 위한 가장 기본적인 Controller 클래스 와 Html 파일을 생성한다.

S3Controller

@Controller
public class S3Controller {

    @RequestMapping(value = "/my-page")
    public String myPage() {
        return "my-page";
    }
}

 

my-page.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Page</title>
</head>
<body>
<h1>My Page!!</h1>
</body>
</html>

서버 기동 및 my-page 접근

애플리케이션 startup 후 http://localhost:8080/my-page를 접근 하면 아래와 같은 로그인 폼으로 이동한다. Spring Security 등록후 아무런 설정을 하지 않을때 제공하는 기본 로그인 폼이다. 기본 설정 ID는 USER 이고 패스워드는 애플리케이션 startup할 때 로그에서 확인할 수 있다.

로그인 폼

패스워드

 

로그인후 페이지 접근

로그인 계정 패스워드 입력후 정상적으로 my-page 에 접근이 된다.

 

첫번째 Spring Security Configuration

application.yml(properties) 파일에서 기본 로그인폼에 로그인 아이디와 비밀번호를 변경할 수 있다. 설정후 재기동을 하면 애플리케이션 로그에 자동으로 생성된 패스워드가 보이지 않는다. 변경한 로그인 정보를 입력하면 정상 로그인 처리되고 my-page 에 접근할 수 있다. 

application.yml

spring:
  security:
    user:
      name: m2sj
      password: 1234

 login

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크