본문 바로가기
개발/Security

[SpringBoot] Security 로그인 인증, 인가(1) - 환경설정

by 코딩하는 흰둥이 2023. 4. 30.
반응형
  • Java 17
  • Maven
  • Spring Boot 3.0.3
  • Spring Security 3.0.4
  • Oracle 11g
  • Mybatis
  • IntelliJ Ultimate
  • DBeaver
  • Thymeleaf

 

폴더 구조

더보기
폴더 구조

 

Security 한번 구현해보겠다고 일주일 내내 작업한거 같다

거의 대부분의 예제들은 3.0.0 이하의 버전들로 SecurityConfig에서

WebSecurityConfigurerAdapter 을 상속받아야 하고 JPA를 사용하고 있다

 

WebSecurityConfigurerAdapter을 상속받아서 하는 것과 변경된 방식인 @BeanSecurityFilterChain 을 사용하는 것이

크게 다른 것은 없으나 Security에 대해 아무것도 모른다는 가정하에 이것은 큰 난항이었다

로그인 시 Security 만 사용해도 되는지, 어디까지가 Security인지, JWT까지 이용해야 할지

 이건 어떤 메서드인지 어떻게 동작하는지 다 모르기 때문에 하나하나 다 까보고 debug 걸어가며 작업하였다

 

그리고 JPA를 선호하지 않는 나에게는 JPA 예제들만 있다는 것은 너무 큰 문제 였다

단순한 CRUD를 사용하기에는 정말 간편하고 좋지만 내 마음대로 커스텀하고 간단한 Join 문만 이용하려고 해도

복잡해지는게 싫어서 Mybatis를 그대로 사용하려한다

 

현재 글은 JWT를 적용하지 않은 Spring Security 이며 

로그인 시 인증(AuthenticationEntryPoint) 을 해주며

권한이 없는 페이지로 이동하였을 때 인가(AccessDeniedHandler) 를 확인해준다

 

일주일간 구글링과 유튜브를 참고하며 간단하게 구현하였다

 

pom.xml
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>3.0.4</version>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        
        <!-- Oracle -->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>21.9.0.0</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Ms Sql -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>12.2.0.jre11</version>
            <scope>runtime</scope>
        </dependency>

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

        <!-- log4j -->
        <dependency>
            <groupId>org.bgee.log4jdbc-log4j2</groupId>
            <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
            <version>1.16</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

        <!-- spring-security-core -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>3.0.4</version>
        </dependency>

        <!-- jwt -->
<!--        <dependency>-->
<!--            <groupId>io.jsonwebtoken</groupId>-->
<!--            <artifactId>jjwt</artifactId>-->
<!--            <version>0.9.1</version>-->
<!--        </dependency>-->

        <!-- javax -->
<!--        <dependency>-->
<!--            <groupId>javax.xml.bind</groupId>-->
<!--            <artifactId>jaxb-api</artifactId>-->
<!--            <version>2.1</version>-->
<!--        </dependency>-->

		<!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

JWT , Javax , ms sql 은 사용하지 않았다

lombok은 취향에 맞게 사용하면 되겠다

 

 

application.properties
server.port=9090

# Database log4j
#spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
#spring.datasource.url=jdbc:oracle:thin:@localhost:1521/XE
spring.datasource.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.datasource.url=jdbc:log4jdbc:oracle:thin:@localhost:1521/XE
spring.datasource.username=system
spring.datasource.password=0000


#### thymeleaf
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false
spring.thymeleaf.order=0

# devtools
spring.devtools.livereload.enabled=true

# VO location
mybatis.type-aliases-package=com.example.practice.vo

# xml location
mybatis.mapper-locations=classpath:mappers/**/*.xml

 

댓글