티스토리 뷰

@EnableWebSecurity

-@Configration 클래스에 @EnableWebSecurity 어노테이션을 추가하여 SpringSecurity 설정할 클래스라고 정의한다.

설정은 WebSecurityConfigurer인스 클래스를 상속받아메서드를 구현하는 것이 일반적인 방법이다.

 

WebSecurityConfigureer 클래스

-WebSecurityConfigurer 인스턴스를 편리하게 생성하기 위한 클래스이다.

 

passwordEncoder()

- BCryptPasswordEncoder는 SpringSecurity에서 제공하는 비밀번호 암호화 객체이다.

Service에서 비밀번호를 암호화할 수 있도록 Bean으로 등록한다.

 

configure()메서드를 오버라이딩하여 Secuity 서렁을 잡아준다.

configure(WebSecurty web)

- WebSecurity는filterChainProxy를 생성하는 필터입니다.

web.ignoring().antMatchers("/css/**","/js/**","/img/**","lib/**");

해당 경로의 파일들은 SpringSecurity가 무시할 수 있도록 설정합니다.

즉 이 파일들은 무조건 통화하며 파일 기준은 resources/static 디렉터리입니다.

 

configure(HttpSecurity http)

-HttpSecurity를 통해 HTTP요청에 대한 웹 기반의 보안을 구성할 수 있습니다.

authorizeRequests()

 HttpServletRequest에 따라 접근을 제한합니다.

 antMatchers()메서드로 ㅡㄱ정 경로를 지정하며 permitAll(),hasRole()메서드로 역할에 따른 접근 설정을 잡아줍니다

롤은 권한을 의미합니다,

 .anyRequest().authenticated()

 ->모든 요청에 대해, 인증된 사용자만 접근하도록 설정할 수 있습니다. 

 

formlogin()

->form 기반으로 인증을 하도록 합니다 로그인 정보는 기본적으로 HttpSession을 이용합니다.

 

.loginPage("/user/login")

 기본 제공되는 form말고 커스텀 로그인 폼을 사용하고 싶으면 loginPage() 메서드를 사용합니다.

 이 때 커스텀 로그인 form의 action경로와 loginPage()의 파라미터 경로가 정확히 일치해야 인증을 처리할 수 있습니다.

 

.defaultSuccessUrl("/user/login/result")

->로그인이 성공했을 때 이동되는 페이지이며, 마찬가지로 컨트롤러에서 URL매핑이 되어 있어야 합니다.

 

.usernameParameter("파라미터명")

- 로그인 form에서 아이디는 name=username 인 input을 기본적으로 인식하는데, usernamePatameter()메서드를 통해 파라미터명을 변경할 수 있습니다.

 

logout()

- 로그아웃을 지원하는 메서드이며 WebSecurityConfigurerAdapter를 사용할 때 자동으로 적용됩니다.

기본적으로 "/logout" 에 접근하면 HTTP세션을 제거합니다.

 

.logoutRequestMatcher(new AntPathRequestMatcher("/user/logout"))

 - 로그아웃의 기본 URL(/logout)이 아닌 다른 URL로 재저으이 합니다.

.invalidateHttpSession(true)

 -HTTP세션을 초기화하는작업입니다.

deleteCookies("KEY명")

- 로그아웃 시 특정 쿠키를 제거하고 싶을 때 사용하는 메서드입니다.

 

configure(AuthenticationManagerBuilder auth)

- 스프링시큐리티에서는 모든 인증은 AuthenticationManager를 통해 이루어지며 AuthenticationManager를 생성하기 위해서는 AuthenticationManagerBuilder를 사용합니다.

로그인 처리 즉, 인증을 위해서는 UserDetailService를 통해서 필요한 정보들을 가져옵니다.

ex)auth.userDetailsService(boardService).passwordEncoder(passwordEncoder());

->서비스 클래스(boardService)에서 이를 처리합니다.

서비스 클래스에서는 UserDetailsService인터페이스를 implement하여 loadUserByUsername()메서드를 구현하면 됩니다.

비밀번호 암호화를 위해 passwordEncoder를 사용하고 있습니다.

 

 

@NotBlank

- null을 허용하지 않음

- 적어도 White-space가 아닌 문자가 한개 이상 포함되어야 함

 

@Email

- 이메일 양식이어야함

 

@Pattern

- 정규 표현식에 맞는 문자열이어야함

 

@NotEmpty

- null과 공백 문자열("")을 허용하지 않음

 

 

'Spring' 카테고리의 다른 글

[Spring]Custom Annotation(커스텀 애노테이션)  (0) 2020.09.30
[spring]RestTemplate란?  (0) 2020.08.13
[springSecurity]스프링 시큐리티  (0) 2020.08.08
@ModelAttribute와@RequestParam  (0) 2020.07.26
@RequestBody,@ResponseBody  (0) 2020.07.25