build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
}
스프링 시큐리티 Configuration Class를 작성하기 위해 WebSecurityConfigurerAdaper 를 상송하여 configure 을 override 한다.
@Configuration
@RequiredArgsConstructor
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
@EnableWebSecurity
WebSecurityConfiguration.class, SpringWebMvcImportSelector.class, OAuth2ImportSelector.class, HttpSecurityConfiguration.class 을 import 하여 Security를 활성화 시킨다.
@EnableGlobalMethodSecurity
특정 페이지에 특정권한이 있는 유저의 접근을 허용한다.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic().disable()
.cors().configurationSource(corsConfigurationSource())
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/", "/auth/**", "/css/**", "/js/**").permitAll()
.antMatchers(HttpMethod.PUT, "/put/**").hasAnyRole("USER")
.antMatchers(HttpMethod.POST, "/post/**").hasAnyRole("USER")
.antMatchers(HttpMethod.GET, "/get/**").hasAnyRole("USER")
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/auth/loginForm") // 로그인 페이지
.loginProcessingUrl("/auth/loginProc")
.defaultSuccessUrl("/")
.failureUrl("/auth/loginForm")
.and()
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider),
UsernamePasswordAuthenticationFilter.class);
}
메소드 정리
httpBasic() | Http basic Auth 로그인 인증창 사용 | .disable() 사용시 만든 폼으로 로그인 |
csrf() | tag를 이용한 공격 | .disable() 사용하여 테스트 진행 |
sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) | 인증정보를 서버에 담아두지 않는다. | |
authorizeRequest() | HttpServletRequest 요청 URL에 따라 접근 권한 설정 | |
antMatchers() | 요청 URL 경로 패턴을 지정합니다. | |
authenticated() | 인증된 유저만 접근을 허용합니다. | |
anyRequest() | 다른 요청들은 인증이나 권한 없이 사용한다. | .authenticated() 가 붙을 시 권한이 설정된 권한이 부여된 사용자만 접근가능 |
formLogin() | form Login 설정 | |
loginPage() | 로그인 페이지 설정한다 | |
loginProcessUrl() | post 받을 주소 | |
defaultSeccesUrl() | 로그인 성공시 이동할 페이지 | |
failureUrl() | 로그인 실패시 이동할 페이지 |
Cors(), Jwt 은 다음에 정리해야지
'🌱 프로젝트 > 소방알리미' 카테고리의 다른 글
Vue Framework에서 Build 하기 - 소방서 알림 #6 (0) | 2022.09.18 |
---|---|
Firestation Spring - 소방서 알림 #5 (0) | 2022.09.17 |
로그인 오류 2 - 소방서 알림 #4 (0) | 2022.09.14 |
로그인 오류 1 - 소방서 알림 #3 (0) | 2022.09.12 |
미니 프로젝트 - 소방서알림 #1 (0) | 2022.09.06 |