티스토리 뷰
이번에 SpringSecurity Configuration 을 이용해서 form 로그인 페이지, 실패 페이지, 로그인 이후 페이지, 권한 처리등을 간단하게 만들어보겠다.
1. S1SecurityConfiguration
@EnableWebSecurity
public class S1SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login-action")
.usernameParameter("userid")
.passwordParameter("userpassword")
.successForwardUrl("/login-ok")
.failureForwardUrl("/login-fail");
http.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/login-action").permitAll()
.antMatchers("/login-fail").permitAll()
.antMatchers("/login-ok").hasAnyAuthority("USER", "ADMIN")
.antMatchers("/admin-page").hasAuthority("ADMIN")
.anyRequest().authenticated();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("m2sj").password("{noop}1234").authorities("USER")
.and()
.withUser("admin").password("{noop}1234").authorities("ADMIN");
}
}
ht
1.1 로그인 form
스프링에서 제공하는 default form이 아닌 사용자가 작성한 form페이지를 이용한다. URI login에 해당하는 View이다.
.loginPage("/login")
해당 로그인 form 의 ID와 Password에 해당하는 input의 name을 설정한다.
.usernameParameter("userid")
.passwordParameter("userpassword")
로그인 form의 action에 들어갈 처리 URI이다. 해당 URI 에 해당하는 구현부는 별도로 필요하지 않다. 스프링 내부에 기본으로 구현돼있다.
.loginProcessingUrl("/login-action")
로그인 성공과 실패시에 redirect될 페이지 URI이다.
.successForwardUrl("/login-ok")
.failureForwardUrl("/login-fail");
1.2 페이지별 권한처리
로그인 form, 로그인 처리, 로그인 실패 페이지는 별도의 권한을 주면 안 된다. 그렇지 않으면 infinite loop에 빠진다.
antMatchers("/login").permitAll()
antMatchers("/login-action").permitAll()
antMatchers("/login-fail").permitAll()
login-ok는 USER, ADMIN권한 admin-ok페이지는 Admin 권한만 접근할 수 있다. 그외 기타 요청은 모두 인증후 접근이 가능하다.
.antMatchers("/login-ok").hasAnyAuthority("USER", "ADMIN")
.antMatchers("/admin-page").hasAuthority("ADMIN")
.anyRequest().authenticated();
1.3 사용자 정보 설정
AuthenticationManagerBuilder를 인자로 가지고 있는 configure메소드를 overrride해서 사용자 정보를 정의할 수 있다. 2명의 사용자를 메모리를 이용해서 저장하는 코드이다. password 항목에 {noob}는 기본적은 비밀번호에 사용되는 hasing을 사용하지 않겠다는 의미이다.
만약에 {noob}을 빼면 PasswordEncoder Bean을 설정해야 된다.
auth.inMemoryAuthentication()
.withUser("m2sj").password("{noop}1234").authorities("USER")
.and()
.withUser("admin").password("{noop}1234").authorities("ADMIN");
2. 테스트
테스트
2.1 로그인
로그인 http://localhost:8080/login
2.2 로그인 성공
m2sj/1234 입력
2.2 로그인 실패
m2sj/1111 입력
2.3 Admin 권한 페이지 접근 확인
m2sj 로그인후 /admin-page 접근 현재는 fallback page가 없어서 server default 403 페이지를 호출한다.
admin 로그인후 /admin-page 접근 성공
3. 전체소스
전체 소스는 주소에서 Clone 후 #3 tag로 checkout 후 확인이 가능하다.
https://github.com/warpgate3/sss.git
git clone https://github.com/warpgate3/sss.git
git checkout tags/#3
warpgate3/sss
spring security study. Contribute to warpgate3/sss development by creating an account on GitHub.
github.com
'Spring' 카테고리의 다른 글
Spring Security (Spring Boot) #4 (0) | 2020.02.28 |
---|---|
Spring boot 2.x + ehcache 3.x (1) | 2020.01.30 |
Spring Security (Spring Boot) #2 (0) | 2020.01.10 |
Spring Security (Spring Boot) #1 (0) | 2020.01.03 |
Spring Boot H2 Database Console forbidden Error (0) | 2019.12.27 |
- Total
- Today
- Yesterday