Back-end/Spring-Security 8

SpringSecurity를 사용할 때 일관적인 Exception 처리를 위해

1. Spring Security Filter chain의 구조 따라서 @Controller나 @Service에서 GlobalCustomException처리하면 (@ControllerAdvice, @ExceptionHandler) 해당 메소드로 넘어가지만, Filter에서 똑같이 Exception 터트려도 GlobalCustomException로 넘어가지 않는다. 해결방법은 Filter영역에서 Exception 처리는 아래와 같이. @Component // 유효한 자격증명을 제공하지 않고 접근하려 할 때 401Unauthorized 에러를 리턴 public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override p..

OAuth 2.0(Open Authorization 2.0)

1. OAuth 2.0란? OAuth 2.0을 사용하면 대부분의 로그인, 개인정보 관리 책임을 서드파티 애플리케이션(Google, Facebook, Kakao 등)에게 위임할 수 있다.(단, 사용자가 기존에 서드파티 서비스에 회원가입 되어있어야 함.) 뿐만 아니라 각 서드파티가 가지고 있는 사용자의 리소스를 조회 등을 내 애플리케이션에서 수행할 수 있다. 2. OAuth 2.0의 구성요소 1) Client : OAuth 2.0을 사용해 서드파티 로그인 기능을 구현할 자사 또는 개인 애플리케이션 서버다. 2) Resource Owner : 서드파티 애플리케이션 (Google, Facebook, Kakao 등)에 이미 개인정보를 저장(회원가입)하고 있으며 Client가 제공하는 서비스를 이용하려는 사용자를 ..

Spring Security- JWT AccessToken, RefreshToken

AccessToken : 인증 -> 인가로 특정 리소스 접근 용도 RefreshToken : 만료된 AccessToken 발급받는 용도(DB 저장) 과정 사용자는 access_token이 만료되었을 때 만료 됐다는 응답을 받으면 클라이언트에서 다시, access_token과 refresh_token을 서버에 보냅니다. 서버에서는 refresh_token이 유효하다면, 클라이언트에게 다시 새로운 access_token을 발급해줍니다. [글로벌 회사들의 토큰 정책] 액세스 토큰을 발급할 때, Refresh 도 새로 발급해서, 계속계속 보안성을 가져가면서 무한 로그인을 유지한다던가 Refresh 의 기간이 끝나면 새로 로그인을 하도록 한다던가 Refresh를 아예 사용하지 않는다거나 (애플의 정책) Refre..

변경된 WebSecurityConfigurerAdapter

1.설정 방법 https://velog.io/@pjh612/Deprecated%EB%90%9C-WebSecurityConfigurerAdapter-%EC%96%B4%EB%96%BB%EA%B2%8C-%EB%8C%80%EC%B2%98%ED%95%98%EC%A7%80 Deprecated된 WebSecurityConfigurerAdapter, 어떻게 대처하지? 스프링 버전이 업데이트 됨에 따라 WebSecurityConfigurerAdapter와 그 외 몇 가지들이 Deprecated 됐습니다.스프링에서는 다른 방식으로 시큐리티 설정을 권장하고 있는 듯 해보였는데요. 방식이 바뀐 탓 velog.io 2. 공식 문서 https://spring.io/blog/2022/02/21/spring-security-with..

페이지 수준 권한 부여

1. 스프링 시큐리티를 이용하여 페이지 내에서 특정 권한 or 특정 이름을 가지고 있는 사용자에게만 특정 목록 or 특정 버튼 보이게 설정하기 1. Controller에서 아래와 같이 @ModleAttribute("showCounselLink")를 이용해서 현재 사용 중인 접속자의 이름이 "user"를 포함해야지만 특정 버튼 보이게 끔 설정할 수 있다. @ModelAttribute의 역할에 따라 /main페이지와 /admin페이지에서 showCounselLink의 리턴 값을 활용할 수 있다는 개념을 이용한다. ' ${showCounselLink}'와 같은 형태로 @Controller public class SecurityContoller { @ModelAttribute("showCounselLink") ..

Spring Security의존제거, HttpServletRequest이용

1. HttpServletRequest의 쓰임 스프링 시큐리티를 사용해서 인증, 권한 기능을 구현할 때 스프링 시큐리티는 Controller단에서 HttpServletRequest를 매개변수로 넣고 현재 로그인한 사용자를 인식한다. 이 경우 스프링 시큐리티의 API에 의존하지 않고 사용자가 속한 역할을 확인할 수 있다. 이때의 장점은 스프링 시큐리티의 API가 변경되거나 보안 구현 방식을 변경할 때 업데이트해야 할 코드가 줄어든다. (스프링 시큐리티의 API에 의존하지 않으니) 다른 방법으로 Authentication을 메서드의 파라미터로 넣을 경우 스프링 시큐리티가 현재의 Authentication 객체를 HttpServletRequest.getPrincial() 매서드(시큐리티에 의존하지 않고 현재 ..

+a, SpringSecurity 인증, 인가 처리 과정 정리

1. Spring Security 구조도 : 2. 인증, 인가 처리 과정 - 대략적인 과정 : SecurityContextPersistence Filter를 가장 먼저 적용해서 SecurityContext에 Authentication 객체를 보관한다. 이후, 인증을 위해 SecurityContext를 이용해 Authentication객체를 구하고 Authentication에 포함되어있는 권한 목록(GrantedAuthority)을 확인해 특정경로에 접근할 권한이 있는지 파악한다. UsernamePasswordAuthenticationToken클래스 : Authetication(interface)을 implement한 AbstractAuthenticationToken의 하위 클래스로 첫 번째 생성자 : 인..

- SecurityContext, SecurityContextHolder, Authentication간의 관계, 현재 로그인 중인 사용자 가져오기

1. Authentication : Authentication에는 사용자의 인증 여부(enabled), 사용자가 가진 권한(authority), 이름 및 접근 주체(username=principa)에 대한 정보를 담고 있다 - 관계 : SecurityContextHolder class의 getContext() 메서드는 SecurityContext 객체를 리턴하고, 이 SecurityCotnext 객체의 getAuthentication() 메서드는 Authentication을 리턴함 따라서 현재 로그인한 사용자명을 획득하기 위해서는 //String username = SecurityContextHolder.getContext().getAuthentication().getName(); public Calen..