공부/Spring Boot
Spring Security 활용 회원 관리 (2) - 커스텀 로그인 뷰 설정
오피스엑소더스
2019. 9. 17. 20:16
소스코드 :
참고 : https://a1010100z.tistory.com/entry/Spring-Spring-boot-security-%EB%A1%9C%EA%B7%B8%EC%9D%B8-%EB%B7%B0-%EB%B3%80%EA%B2%BD%ED%95%98%EA%B8%B0%EA%B0%9C%EB%B0%9C%EC%9D%BC%EA%B8%B0A4
1. 로그인 뷰 설정
1. 요약
> WebSecurityConfigurerAdapter를 상속받은 @Configuration 파일에 로그인 뷰 경로 설정
> 컨트롤러 작성
> 뷰 작성
2. SpringSecurityConfiguration extends WebSecurityConfigurerAdapter
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | package com.exam.app.config.security; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.session.HttpSessionEventPublisher; @Configuration @EnableWebSecurity public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private DataSource dataSource; private final String LOGIN_PAGE_URI = "/logingo"; /** * 인증 설정 관련. * * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .jdbcAuthentication() .dataSource(dataSource) .passwordEncoder(new BCryptPasswordEncoder()) ; } /** * security filter와 연결하기 위한 설정 관련. * * @param web * @throws Exception */ @Override public void configure(WebSecurity web) throws Exception { // 해당 패턴은 security filter 제외 web.ignoring().antMatchers("/favicon.ico", "/css/**", "/image/**", "/js/**", "/webjars/**"); } /** * request를 안전하게 보호하기 위한 설정 관련. * * @param http * @throws Exception */ @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/member","/member/*").authenticated() .antMatchers("/logingo","/logingo/*").authenticated() .antMatchers("/login","/login/*").authenticated() .anyRequest().permitAll() .and() .formLogin() .loginPage(LOGIN_PAGE_URI) .permitAll() .defaultSuccessUrl("/welcome") .and() .logout() .logoutUrl("/logout") .logoutSuccessUrl(LOGIN_PAGE_URI) .invalidateHttpSession(true) .permitAll(); } @Bean public HttpSessionEventPublisher httpSessionEventPublisher(){ return new HttpSessionEventPublisher(); } } | cs |