728x90
설계
index.html에 접속해서 로그인 시도를 한다.
user권한이 있을 경우 "/user-page"로 접속하고, admin권한이 있는 경우 "/admin-page"로 접속한다.
소스코드
Back-End
Spring Security를 configuration하기 위해서는 WebSecurityConfigurerAdapter를 상속받고, configure를 overriding해준다.
@EnableWebSecurity(debug = true)
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final CustomAuthDetails customAuthDetails;
public SecurityConfig(CustomAuthDetails customAuthDetails) {
this.customAuthDetails = customAuthDetails;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser(
User.withDefaultPasswordEncoder()
.username("user1")
.password("1111")
.roles("USER")
).withUser(
User.withDefaultPasswordEncoder()
.username("admin")
.password("2222")
.roles("ADMIN")
);
}
@Bean
RoleHierarchy roleHierarchy() {
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER"); // ROLE_ADMIN은 ROLE_USER가 가지고 있는 모든 권한을 행할 수 있다.
return roleHierarchy;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests(request->{
request
.antMatchers("/").permitAll()
.anyRequest().authenticated()
;
})
.formLogin(
login -> login.loginPage("/login")
.permitAll()
.defaultSuccessUrl("/", false) // 디폴트 url로 가게 할 것인가? false일경우 최근 방문한 페이지에 남아있는다.
.failureUrl("/login-error") // 로그인에 실패했을 경우
.authenticationDetailsSource(customAuthDetails)
)
.logout(logout -> logout.logoutSuccessUrl("/")) // logout시 페이지
.exceptionHandling(exception->exception.accessDeniedPage("/access-denied")) //
;
}
// 모든 페이지에 대해서 Seurity가 작용하였기 때문에
// 기존 CSS파일을 읽지 못한다.
// 무시해주는 코드
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.requestMatchers(
PathRequest.toStaticResources().atCommonLocations()
);
}
}
메소드 설명
- configure
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests(request->{
request
.antMatchers("/").permitAll()
.anyRequest().authenticated()
;
})
.formLogin(
login -> login.loginPage("/login")
.permitAll()
.defaultSuccessUrl("/", false) // 디폴트 url로 가게 할 것인가? false일경우 최근 방문한 페이지에 남아있는다.
.failureUrl("/login-error") // 로그인에 실패했을 경우
.authenticationDetailsSource(customAuthDetails)
)
.logout(logout -> logout.logoutSuccessUrl("/")) // logout시 페이지
.exceptionHandling(exception->exception.accessDeniedPage("/access-denied")) //
;
}
메소드 이름 | 기 능 |
antMatchers("/") | 접속할 페이지를 설정 |
permitAll() | 모든 권한 부여 |
anyRequest | Chain에 들어온 Object를 return |
authenticated() | 허가 여부를 return |
loginPage("/login") | loginpage url 지정 |
defaultSuccessUrl("/", false) | 로그인 한 후 default page 지정 |
failureUrl("login-error") | 로그인 실패 했을 경우 url 지정 |
accessDeniedPage("/access-denied") | 로그인 거부 되었을 경우 url 지정 |
logoutSuccessUrl("/") | 로그아웃 page 지정 |
- roleHierarchy
@Bean
RoleHierarchy roleHierarchy() {
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER"); // ROLE_ADMIN은 ROLE_USER가 가지고 있는 모든 권한을 행할 수 있다.
return roleHierarchy;
}
728x90
'Spring > Security' 카테고리의 다른 글
Authentication 메커니즘 (0) | 2022.06.11 |
---|---|
폼 로그인 (0) | 2022.05.30 |
SpringSecurity구조 (0) | 2022.05.30 |
Spring Security (0) | 2022.05.30 |
댓글