Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- A. Steed 2: Cruise Control
- codejam
- 안동국시
- 레스토랑
- 발산
- coffee
- 스테이크
- 고양이는 언제나 귀엽다
- 데이트
- 소호정
- 소호정본점
- 냥스토리
- 스파게티
- 스코티쉬 스트레이트
- CodeJam 2017 Round 1B
- 부모님과
- 발산맛집
- 먹기좋은곳
- 냥이
- 파버스
- 양재맛집
- 치명적 귀여움
- RED CAT COFFEE X LOUNGE
- 고양이
- 커플
- CDJ
- 발산역 근처 카페
- 파머스테이블
- 냥냥
- 카페
Archives
- Today
- Total
hubring
Spring boot - Security 본문
Security dependencies
spring boot starter security 의존성 추가
maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Gradle
implementation 'org.springframework.boot:spring-boot-starter-security'
이후 spring을 실행시켜 localhost로 접속하면 아래와 같이 로그인 화면으로 접근한다.
기본적으로 접근 가능한 계정은
ID : user
Password : 디버그 창에 표시된 password
Spring Boot Security 사용시 추가되는 기능
1. 서버 기동 시 시큐리트 초기화 작업 및 보안 설정
2. 별도의 설정 및 구현 없이 기본적 웹 보안 기능이 연동되어 작동
- 모든 요청은 인증 후 접근 가능
- 인증 방식은 폼 로그인 방식 httpBasic 로그인 방식을 제공
- 기본 로그인 페이지 제공
- 기본 계정 제공 ( username : user / password : 랜덤 password )
사용자 정의 보안 기능 구현
SecurityConfig 설정
사용자 정의 Config
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.anyRequest()
.authenticated() // 인가정책
.and()
.formLogin(); //인증정책
}
}
기본 자동으로 설정된 Default Config 설정은 아래와 같다
이 설정으로 인하여 의존성만 추가하더라도 인증요청하는 login 화면이 표시된 것이다.
protected void configure(HttpSecurity http) throws Exception {
this.logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");
((HttpSecurity)((HttpSecurity)((AuthorizedUrl)http.authorizeRequests().anyRequest()).authenticated().and()).formLogin().and()).httpBasic();
}
사용자 계정 설정
기본 계정이 아닌 지정된 사용자 계정을 설정할 경우
application.properties 파일에 사용자 계정 정보를 작성하여 설정할 수 있다.
http.formLogin()
From 로그인 인증 기능 제공
formLogin 상세 설정 예시
@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/loginPage")
.defaultSuccessUrl("/")
.usernameParameter("userId")
.passwordParameter("passwd")
.loginProcessingUrl("/login_proc")
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
System.out.println(authentication.getName());
httpServletResponse.sendRedirect("/");
}
})
.failureHandler(new AuthenticationFailureHandler(){
@Override
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
System.out.println(e.getMessage());
httpServletResponse.sendRedirect("/login");
}
})
.permitAll();;
}
참고
본 내용은 아래 인프런 강의 내용을 간략히 정리한 것입니다.
인프런 - 스프링 시큐리티 - Spring Boot 기반으로 개발하는 Spring Security
https://www.baeldung.com/spring-security-login
'Spring Boot' 카테고리의 다른 글
직접 빌드하기 (0) | 2020.08.04 |
---|---|
Spring Boot Documentation (0) | 2020.08.04 |
IntelliJ Gradle 대신 자바 직접 실행 (0) | 2020.08.04 |
Spring boot - properties 분리 (0) | 2020.07.14 |
Spring Boot - Swagger 적용 (0) | 2020.07.11 |