본문 바로가기
▶ Back-End/Java

SpringBoot Security Login 기능 추가

by 오늘도 코딩 2023. 9. 26.
728x90
반응형

아주 간단한 Spring Security Login 기능 추가 방법

*Maven 사용

*Spring Boot Project 생성 과정 생략

*자세한 설명 생략

 

 

▷ pom.xml

*의존성 추가

 

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

 

 

▷ application.properties

*사용자 지정 ID/PWD 설정

* Default user ID user

* Default user Pwd : 서버 실행 시 제공( Using generated security password: fd67f8d9-652b-46cd-a147-4b6681292d50 )

 

spring.security.user.name = test
spring.security.user.password = test

 

 

▷ SecurityConfig.java

*Spring Security Config 파일 생성

 

/**
 * Spring Security Config
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin(__-> {}); // Default login form
        http.httpBasic(__-> {}); // Basic Auth
        http.csrf(__-> __.disable()); // CSRF
        http.authorizeRequests(__-> __.anyRequest().authenticated());// 모든 요청 인증 필수
    }
}

 

 

▷ 결과 확인

 

① web 접속 확인 

http.formLogin() : Spring에서 제공하는 LoginForm

 

② PostMan API 확인

인증 없이 요청 401 Error

 

http.httpBasic() : Basic Auth 사용

 

 

728x90
728x90

'▶ Back-End > Java' 카테고리의 다른 글

VERIFY JWT(nimbus)  (0) 2023.10.12
Create JWT(nimbus)  (0) 2023.10.12
Objcet to String  (0) 2023.09.21
@Value 사용법  (0) 2023.09.21
LocalTime AVG  (0) 2023.09.21

댓글