스프링 시큐리티 - 폼 로그인, 로그아웃

스프링 시큐리티 - 폼 로그인, 로그아웃

728x90

GET 방식의 Default Filter

DefaultLoginPageGeneratingFilter

GET 방식의 /login 을 처리하는 필터다. 별도의 로그인 페이지 설정을 하지 않았을때 제공되며, 기본 로그인 폼을 제공한다.

DefaultLogoutPageGeneratingFilter

GET 방식의 /logout 을 처리하는 필터다. POST 방식의 /logout을 요청할 수 있는 UI를 제공한다. DefaultLoginPageGeneratingFilter 를 사용하는 경우에 같이 제공된다.

POST 방식의 Filter

LogoutFilter

POST 방식의 /logout 을 처리하는 필터다. processingUrl 변경을 통해 바꿀 수 있으며, 로그아웃을 처리한다. 기본적으로 session, SecurityContext, csrf, 쿠키, remeberMe 쿠키 등을 삭제 처리하고 로그인 페이지로 redirect 시킨다.

UsernamePasswordAuthenticationFilter

POST 방식의 /login 을 처리하는 필터다. processingUrl 을 변경하면 주소를 변경할 수 있다. form 인증을 처리해주는 필터로 스프링 시큐리티에서 가장 일반적으로 쓰인다.

설정정보 설명 filterProcessingUrl 로그인을 처리해줄 POST 방식의 URL username parameter POST 에 username 에 대한 값을 넘겨줄 인자의 이름 password parameter POST에 password에 대한 값을 넘겨줄 인자의 이름 로그인 성공 - defaultSuccessUrl : alwaysUse 옵션 설정 중요

- successHandler 로그인 실패 - failUrl

- failureHandler

SecurityConfig.java

package com.sp.fc.web.config; import lombok.RequiredArgsConstructor; import org.springframework.boot.autoconfigure.security.servlet.PathRequest; import org.springframework.context.annotation.Bean; import org.springframework.security.access.hierarchicalroles.RoleHierarchy; import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.User; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @EnableWebSecurity(debug = true) @EnableGlobalMethodSecurity(prePostEnabled = true) // ROLE 에 맞게 접근 가능하도록 @RequiredArgsConstructor public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests(request->{ request .antMatchers("/").permitAll() // root page 허용 .anyRequest().authenticated() // 그 외는 권한을 받고 들어오라는 의미 //.antMatchers("/**").permitAll() // 모든 URL 에 대한 권한 허용 ; }) .formLogin( // 로그인이 안되어있을 경우 로그인 페이지로 이동 (default security 로그인 페이지가 뜸) // login page 지정해야 우리가 만든 login 페이지로 이동됨 login -> login.loginPage("/login") .permitAll() // permitAll 을 해줘야 허용됨 .defaultSuccessUrl("/", false) // false 로 해줘야 로그아웃 후 유저페이지 접근하려고하면 로그인 페이지로 이동되는데, 이때 로그인 후 유저페이지로 바로 갈수있게된다. .failureUrl("/login-error") // 로그인 실패시 이동될 페이지 .authenticationDetailsSource(customAuthDetails) // /auth api 호출시 details 가 우리가 만든 형식에 맞게 보여준다. ) // 로그인을 특정해주지않으면 DefaultLoginPageGenerationFilter, DefaultLogoutPageGeneratingFilter 가 동시에 작동한다 ... ; } }

위 코드에서 .formLogin 부분을 보면 loginPage 를 설정해주고 있는데, 이런 경우에는 위에서 본 DefaultLoginPageGeneratingFilter, DefaultLogoutPageGeneratingFilter 가 적용되지 않는다.

from http://devfunny.tistory.com/618 by ccl(A) rewrite - 2021-11-21 05:02:01