[Servlet Application] : 자바에서 servlet filter로 사용된다.
springSecurityFilterChain이란 이름으로 servlet Filter를 생성한다.
처음 시작 username: user, password는 스프링부트실행 후 콘솔창에서 확인할 수 있다.(참고, 실행마다 바뀜)
스프링부트 실행 후 로그인 모습
<아키텍쳐>
https://docs.spring.io/spring-security/reference/servlet/architecture.html
Architecture :: Spring Security
Spring Security’s Servlet support is based on Servlet Filters, so it is helpful to look at the role of Filters generally first. The picture below shows the typical layering of the handlers for a single HTTP request. The client sends a request to the appl
docs.spring.io
<예외관리>
-> ExceptionTranslationFilter
<Authentication>
-Architecture:https://docs.spring.io/spring-security/reference/servlet/authentication/architecture.html
Servlet Authentication Architecture :: Spring Security
ProviderManager is the most commonly used implementation of AuthenticationManager. ProviderManager delegates to a List of AuthenticationProviders. Each AuthenticationProvider has an opportunity to indicate that authentication should be successful, fail, or
docs.spring.io
//SecurityContext: Spring Security’s authentication의 핵심
SecurityContext context = SecurityContextHolder.createEmptyContext(); // SecurityContext를 생성 후
Authentication authentication = //principal, credential, authority가 담긴 authentication 생성
new TestingAuthenticationToken("username", "password", "ROLE_USER");
context.setAuthentication(authentication); //authentication을 context에 담고
SecurityContextHolder.setContext(context); //context를 다시 SecurtyContextHolder에 담는다.
//접근방법
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
String username = authentication.getName();
Object principal = authentication.getPrincipal();
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
<Username/Password Authenticaiton>: username과 password로 검증하는 가장 대중적인 방법
- Form Login: html의 form을 통해 받은username과 password로 검증하는 방법 -> 인증되지 않은 이용자가 요청을 보내면 FilterSecurityInterceptor가 접근을 막고 로그인 페이지로 redirect해준다. 이후 로그인을 하면 UsernamePasswordAuthenticationFilter로 인해 로그인 성공/ 실패 가 실행된다.
package tnut.springsecurity.config;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
public class SpringSecurityConfig {
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.formLogin(form->form
.loginPage("/login")
.permitAll()
);
return null;
}
}
package tnut.springsecurity.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SpringSecurityController {
@GetMapping({"","/"})
public String index(){
return "index";
}
@GetMapping("/login")
public String login() {
return "login";
}
}
localhost:8080으로 인덱스 페이지로 가려고 해도 /login페이지로 이동하는 모습을 볼 수 있다.
- Basic Authentication : http 기본인증
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.httpBasic();
return http.build();
}
실행 화면은 위와 똑같다.
<Password Storage>
-db에 user의 password를 해시하여 저장할 예정이다.
<Session Management>
-세션을 어떻게 관리 설정부분
<Remember Me>
-로그인 유지 설정
<Logout>
'SpringSecrity' 카테고리의 다른 글
SessionCreationPolicy.STATELESS (0) | 2022.06.26 |
---|---|
Spring Security(5.7.2) 공식문서 탐방(1) (0) | 2022.06.25 |
스프링 시큐리티 환경 설정 (0) | 2022.06.24 |