본문 바로가기

반응형

Backend/Spring | SpringBoot

(51)
[Spring Security] 인증 표현식 @Bean SecurityFilterChain web(HttpSecurity http) throws Exception { http // ... .authorizeHttpRequests(authorize -> authorize .dispatcherTypeMatchers(FORWARD, ERROR).permitAll() .requestMatchers("/static/**", "/signup", "/about").permitAll() .requestMatchers("/admin/**").hasRole("ADMIN") .requestMatchers("/db/**").access(allOf(hasAuthority("db"), hasRole("ADMIN"))) .anyRequest().denyAll() ); ret..
[Spring Security] Survlet 기반 Application 아키텍쳐(2) 이전글 : Survlet 기반 Application 아키텍쳐(1) @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests((authz) -> authz .anyRequest().permitAll() ) .httpBasic(withDefaults()); return http.build(); } SecurityFilterChain를 사용하는 이유는 HttpSecurity를 통해 HTTP보안 설정 구성하기 위함이다. 보안 필터는 SecurityFilterChain API를 통해 FilterChainProxy에 삽입된다. 필터들은 인증, 권한 부여, 취약점 보호 등 다양..
[Spring Security] Survlet 기반 Application 아키텍쳐(1) 공식문서 : https://docs.spring.io/spring-security/reference/servlet/architecture.html#servlet-filters-review Architecture :: Spring Security The Security Filters are inserted into the FilterChainProxy with the SecurityFilterChain API. Those filters can be used for a number of different purposes, like authentication, authorization, exploit protection, and more. The filters are executed in a spec docs..
[Spring Security] SpringSecurityConfig 적용 클래스 생성 공식 문서를 확인해보니 Spring Security 5.7.0-M2버전 부터 WebSecurityConfigurerAdapter가 deprecated되었다고 한다. (공식 문서 : https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter) 사용 예제 : https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/index.html#publish-authentication-manager-bean 위 링크 안에 deprecated에 대해 대처하는 방법도 있으니 참고 하기 바람. 이 글에서는 Spring S..
[Spring Security] Spring Security 적용 의존성 추가 # Maven org.springframework.boot spring-boot-starter-security # Gradle implementation 'org.springframework.boot:spring-boot-starter-security' 의존성을 추가하고 Controller로 요청을 하면 서버에서 Security가 동작한다. 기본적으로 계정과 비밀번호를 설정하지 않으면 1회성 비밀번호를 발급된다. 초기 계정은 user / 비밀번호는 log에서 확인이 가능하다. 하지만 비밀번호는 매번 서비스가 재시작 될때 마다 새롭게 발급된다. 게다가 로그인을 하게 되면 root로 이동하게 되고 URI를 입력하면 400에러가 발생한다. 두가지 모두 해결하는 방법은 Spring Security ..
[Spring] @RequestBody 프론트엔드에서 AJAX 요청을 할 때 데이터가 직렬화 되어 형식이 JSON으로 된다. 이 JSON 데이터를 @RequestBody가 Java객체로 역직렬화하여 Http요청 본문을 DTO인 Java객체로 mapping할 수 있게 한다. 그리고 돌아온 응답을 토대 window.location.href를 이용해 페이지를 이동할 수 있다. 사용 코드 Controller // ... @RequestMapping(value = "/loginFail") public String loginFail() { return "/web/common/loginFail"; } @RequestMapping(value = "/main") public String mainPage() { return "/web/common/main"; ..
[Spring] MessageSource를 이용한 Exception처리 보통 MessageSource는 국제화(i18n)를 목적으로 사용한다. 하지만 이를 이용하면 예외처리가 좀 더 간편해진다. build.gradle에 의존성 추가 implementation 'net.rakugakibox.util:yaml-resource-bundle:1.1' MessageConfiguration 파일 생성 import net.rakugakibox.util.YamlResourceBundle; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import or..
[Spring] redirect경로 redirect시 URL이 원하는 경로로 되지 않는다. redirect메커니즘 : redirect: 사용 -> 스프링 MVC는 HTTP 상태 코드 302 Found 반환 -> Location 헤더에 지정된 URL로 리디렉션 지시 여기서 발생한 문제는 redirect: 뒤에 지정된 경로가 상대 경로로 해석되기 때문에, 현재 경로(/ajax/loginProcess)를 기준으로 리디렉션 URL이 구성됨 Sol) return "redirect:/web/common/main";

반응형