[Spring Security] UserDetails Class에 대하여
·
Server/Security
UserDetails 클래스 public interface UserDetails extends Serializable { /** * Returns the authorities granted to the user. Cannot return null. * @return the authorities, sorted by natural key (never null) */ Collection
[Spring Security] Security 종속성 추가 후, 자동 로그인 화면이 뜨는 이유
·
Server/Security
종속성 추가 implementation 'org.springframework.boot:spring-boot-starter-security' Spring Security를 사용하기 위해, 위의 종속성을 추가함과 동시에 아래와 같은 로그인 화면이 뜨는 것을 볼 수 있다. 로그인 화면이 뜨는 이유 package org.springframework.boot.autoconfigure.security.servlet; 시큐리티 종속성 추가 후, 다음 패키지로 이동하면 SpringBootWebSecurityConfiguration라는 하나의 클래스를 확인할 수 있다. @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = Type.SER..
[Spring Security] UserDetails를 User에 구현하여 사용하기.
·
Server/Spring&Spring Boot
Spring Seucurity를 설정하다 보면 UserDetailsService를 통해 가입된 회원인지 확인하여 반환하는 메서드를 구현하게 된다. Spring Security Config @EnableWebSecurity @Configuration @RequiredArgsConstructor public class SecurityConfig { ... @Bean public UserDetailsService userDetailsService() { return email -> memberRepository.findByMemberEmail(email) .orElseThrow(() -> new UsernameNotFoundException("존재하지 않는 회원입니다.")); } } 이렇게 작성하고 나면, 바..
[Spring Security] Spring Security을 추가하면 왜 로그인 화면으로 넘어가는 걸까?
·
Server/Spring&Spring Boot
의문 build.gradle에 Spring Security를 추가하게 되면 로그인 화면으로 넘어가는 것을 볼 수 있다. dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' } 그렇다면 왜 로그인 화면으로 넘어가게 되는 걸까? 원인을 찾아라 우리는 로그인 화면으로 넘어가지 않도록 SpringConfig라는 것을 만들고, SeuciryFilterChain를 재정의하며 permitAll() 메서드를 사용해서 로그인 창이 뜨지 않고 원하는 페이지에 접속할 수 있도록 설정하고 있다. 그래서 SecurityFilterChain에 대해 알아보기로 했다. SecurityFilterChain public interfac..
[Swagger Error] Spring boot 3.0.x 이상 springdoc-openapi Swagger 화면이 로드되지 않을 때.
·
Server/Error
다음과 같이 swagger-ui에 접속은 되는데, 내용들이 불러와지지 않고 하얀 화면만 보이는 증상이 발생했다. Spring Security 경로도 열어줬고, 설정도 다 했는데 왜 안될까 고민하며 F5만 누르다가 혹시 몰라 F12를 눌러 콘솔을 찾아봤다. 다음 사진을 보면 /swagger-ui/ 다음으로 하위 경로에 있는 .css, .js 파일들이 로드되지 못하고 있는 것을 알 수 있었다. 기존 Security Config에서 Swagger의 경로 지정을 아래와 같이 지정했었는데 private final String[] SWAGGER = {"/v3/api-docs", "/swagger-ui", "/swagger-ui/index.html"}; 하위 경로까지 모두 허용하는 코드로 변경했다. private f..
[Spring Boot] SpringBoot 3.0.x 이상에서 Swagger 사용
·
Server/Spring&Spring Boot
Java 17, Spring Boot 3.0.x 이상은 도대체 무슨 격동이 일어난 것일까.. 기존에 사용하던 세팅이 지원이 안 되는 경우가 많다. Swagger 역시 springfox로 설정하여 접속하게 되면 403 에러가 발생하면서 접속 자체가 막혀버리는 현상이 일어났다. 스택오버플로우를 찾아보니 springfox-boot-start 3.0.0, springdoc-openapi-ui 1.6.13 등이 아직 spring boot 3.0을 지원하지 않고 있는 것 같다. 우리가 사용할 수 있는 방법은 springdoc-openapi-starter-webmvc-ui 사용하는 방법이었다 의존성 추가 implementation 'org.springdoc:springdoc-openapi-starter-webmvc-..
[Spring Boot] Spring Security 6.0 Configuration.
·
Server/Spring&Spring Boot
새로운 프로젝트를 시작하면서 Spring Boot 3.0.1, Java 17을 사용하게 되었다. 기존 Spring Boot. 2.7.x 버전에서 사용하던 Spring Security 설정을 그대로 복사 붙여 넣기를 했더니 @deprecated된 명령어도 있고, 아예 사라진(변경된) 명령어가 존재하여 새로운 버전에 맞춰 코드를 수정하였다. 기존 Security 설정 @Configuration @EnableWebSecurity @RequiredArgsConstructor public class SecurityConfig { private final UserService userService; @Value("${jwt.secret}") private String secretKey; @Bean public Se..
[JWT] JWT Exception 처리
·
프로젝트/Archive
JWT의 토큰 만료를 확인하면서 HTTP 500 에러가 발생한다는 것을 확인했다. 요구사항에서 주어진 ERROR_CODE를 확인하면 토큰 에러의 경우에는 401, Unauthorized 에러를 반환하라고 지침하고 있기 때문에 토큰과 관련한 에러를 따로 잡아주는 과정이 필요할 것 같다. 첫 번째 시도 isExpired 메서드에서 try-catch @Slf4j public class JwtUtil { ... public static boolean isExpired(String token, String secretKey) { try { return Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token) .getBody().getExpiration().bef..
코드플리
'Spring Security' 태그의 글 목록