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
- 백엔드개발자
- Redux
- spring boot
- Front-End
- 정보처리기사
- 큐넷
- spring
- JavaScript
- Node.js
- TypeScript
- frontend
- TS
- React
- 프론트엔드
- JWT
- useState
- 스프링부트
- It
- 정보처리기사 실기
- spring boot security
- 자바스크립트
- 리액트
- 웹개발자
- Authentication
- 타입스크립트
- JS
- 수제비
- security
- 백엔드
- VUE
Archives
- Today
- Total
솔적솔적
SpringBoot기반으로 Security 개발(자세히 파해쳐보자)_2 본문
✊ 학습목표
1) UsernamePasswordAuthenticationFilter 알아보기
2) LogoutFilter 알아보기
3) Remember Me 인증
4) RememberMeAuthenticationFilter
5) AnonymousAuthenticationFilter - 인증 사용자필터 알아보기
1) Form Login 인증 필터 : UsernamePasswordAuthenticationFilter

2) Logout 처리, LogoutFilter
클라이언트가 로그아웃하면
스프링시큐리티가 로그아웃처리를 할 때
세션 무효화, 인증토큰삭제, 쿠키정보 삭제, 로그인 페이지로 다이렉트로간다.
http.logout() : 로그아웃 기능이 작동함
protected void configure(HttpSecurity http) throws Exception{
http.logout() //로그아웃 처리
.logoutUrl("/logout") //로그아웃 처리 URL
.logoutSuccessUrl("/login") //로그아웃 성공 후 이동페이지
.deleteCookes("JSESSIONID", "remember-me") //로그아웃 쿠키삭제
.addLogoutHandler(logoutHandler()) //로그아웃 핸들러
.logoutSuccessHandler(logoutSuccessHandler()) //로그아웃 성공 후 핸들러
}
직접 작성
SecurityConfig
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated();
http
.formLogin();
http
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.addLogoutHandler(new LogoutHandler() {
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
HttpSession session = request.getSession();
session.invalidate();
}
})
.logoutSuccessHandler(new LogoutSuccessHandler() {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
response.sendRedirect("/login");
}
})
.deleteCookies("remember-me");
}
}


3) Remember Me 인증
설정 클래스에서 리멤버미 설정을 활성화 시키면 로그인페이지에
인증 기능을 동시에 사용할 수 있도록나온다.
- 서버에서 Remember Me 인증을 게속 사용할 수 있도록 쿠키를 발급 → 우리는 계속 접근 할 수 있는 것이다.
- (세션이 만료되고 웹 브라우저가 종료된 후에도 어플리케이션이 사용자를 기억하는 기능)
- Remember-Me 쿠키에 대한 Http요청을 확인한 후 토큰 기반인증을 사용해 유효성을 검사하고 토큰이 검증되면 사용자는 로그인 된다.
- 사용자 라이프 사이클
- 인증성공 (Remember-Me 쿠키 설정)
- 인증실패 (쿠키가 존재하면쿠키 무효화)
- 로그아웃 (로크아웃 존재하면 쿠키 무효화)
http.remeberMe() : rememberMe 기능이 작동함
protected void configure(HttpSecurtiy http) throws Exception{
http.rememberMe()
.rememberMeParameter("remember") //기본 파라미터명은 remember-me
.tokenValiditySeconds(3600) // Default는 14일
.alwaysRemember(true) //리멤버 미 기능이 활성화 되지 않아도 항상 실행
.userDetailsService(userDetailsService)
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
UserDetailsService userDetailsService;
protected void configure(HttpSecurity http, UserDetailsService userDetailsService) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated();
http
.formLogin();
http
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.addLogoutHandler(new LogoutHandler() {
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
HttpSession session = request.getSession();
session.invalidate();
}
})
.logoutSuccessHandler(new LogoutSuccessHandler() {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
response.sendRedirect("/login");
}
})
.and()
.rememberMe()
.rememberMeParameter("remeber")
.tokenValiditySeconds(3600)
.userDetailsService(userDetailsService);
}
}
4)Remember Me 인증 필터 : RememberMeAuthenticationFilter

5) 익명사용자 인증 필터 : AnonymousAuthenticationFilter
이 필터가 존재하는 이유,
어떤 사용자가 인증을 받으면 세션에 인증 받은 사용자의 유지객체를 저장 → 사용자가 어떤 자원에 접근하려할 시 세션에서 유저객체가 존재? 아님? 하는 것 중 null이면 그 자원에 접근 못하게,
아닐 시 접근 가능하도록 구분하는처리
인증객체가 있는지 Authentiation에서 유무를 확인
- 익명 사용자 인증 처리 필터
- 익명 사용자와 인증 사용자를 구분해서 처리하기 위한 용도로 사용
- 화면에서 인증 여부를 구현할 때 isAnonymous()와 isAuthenticated()로 구분해서 사용
- 인증객체를 세션에 저장하지 않는다.
[참고 강의] 인프런 - 스프링 시큐리티 - Spring Boot 기반으로 개발하는 Spring Security
'Back-end > Spring Boot Security' 카테고리의 다른 글
| SpringBoot기반으로 Security 개발(자세히 파해쳐보자)_3 (0) | 2022.02.22 |
|---|---|
| SpringBoot기반으로 Security 개발(자세히 파해쳐보자)_1 (0) | 2022.02.21 |
| Spring Boot Security - NAVER(네이버) 로그인 완료하기 (0) | 2022.02.09 |
| Spring Boot Security - 구글 로그인 및 자동 회원가입 진행 완료 (0) | 2022.02.09 |
| Spring Boot Security - Authentication 객체가 가질 수 있는 2가지 타입 (0) | 2022.02.09 |