programing

Http Security, Web Security 및 Authentication Manager Builder

yoursource 2022. 12. 21. 23:07
반응형

Http Security, Web Security 및 Authentication Manager Builder

언제 이 시스템을 덮어쓸지 누가 설명해주실 수 있나요?configure(HttpSecurity),configure(WebSecurity)그리고.configure(AuthenticationManagerBuilder)?

configure(AuthenticationManagerBuilder)는 AuthenticationProviders를 쉽게 추가할 수 있도록 함으로써 인증 메커니즘을 확립하기 위해 사용됩니다.다음은 내장된 'user' 및 'admin' 로그인을 사용한 메모리 내 인증을 정의합니다.

public void configure(AuthenticationManagerBuilder auth) {
    auth
        .inMemoryAuthentication()
        .withUser("user")
        .password("password")
        .roles("USER")
    .and()
        .withUser("admin")
        .password("password")
        .roles("ADMIN","USER");
}

configure(HttpSecurity)를 사용하면 선택 일치에 따라 리소스 수준에서 웹 기반 보안을 구성할 수 있습니다.다음 예제에서는 /admin/로 시작하는 URL을 ADMIN 역할을 가진 사용자로 제한하고 다른 URL을 정상적으로 인증해야 함을 선언합니다.

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/admin/**").hasRole("ADMIN")
        .anyRequest().authenticated()
}

configure(WebSecurity)는 글로벌보안에 영향을 주는 설정(리소스 무시, 디버깅모드 설정, 커스텀방화벽 정의 구현에 의한 요구 거부)에 사용됩니다.예를 들어, 다음 메서드는 /resources/로 시작하는 모든 요청을 인증 목적으로 무시합니다.

public void configure(WebSecurity web) throws Exception {
    web
        .ignoring()
        .antMatchers("/resources/**");
}

Spring Security Java Config Preview 자세한 내용은 다음 링크를 참조하십시오. 웹 보안

Web Security의 일반적인 사용ignoring()메서드는 Spring Security를 생략하고 Spring Security 기능을 사용할 수 없습니다.Web Security는 Http Security를 기반으로 합니다.

@Override
public void configure(WebSecurity web) throws Exception {
    web
        .ignoring()
        .antMatchers("/resources/**")
        .antMatchers("/publics/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/admin/**").hasRole("ADMIN")
        .antMatchers("/publics/**").hasRole("USER") // no effect
        .anyRequest().authenticated();
}

위의 예에서 Web Security는 Spring이 무시하도록 합니다./resources/**그리고./publics/**따라서,.antMatchers("/publics/**").hasRole("USER")HttpSecurity는 무시됩니다.

이렇게 하면 보안 필터 체인에서 요청 패턴이 완전히 생략됩니다.이 경로와 일치하는 것은 인증 또는 인가 서비스가 적용되지 않으며 자유롭게 액세스할 수 있습니다.

configure(HttpSecurity)그럼 선택 일치에 따라 리소스 수준에서 웹 기반 보안을 설정할 수 있습니다.다음 예시는 로 시작하는 URL을 제한하고 있습니다./admin/다른 URL을 정상적으로 인증할 필요가 있음을 선언합니다.

configure(WebSecurity)글로벌 보안에 영향을 주는 설정(리소스 추가, 디버깅모드 설정, 커스텀방화벽 정의 구현에 의한 요구 거부)에 사용됩니다.예를 들어, 다음 방법은 다음과 같이 시작하는 모든 요청을 발생시킵니다./resources/무시됩니다.

AuthenticationManagerBuilder
extends AbstractConfiguredSecurityBuilder<AuthenticationManager,AuthenticationManagerBuilder>
implements ProviderManagerBuilder<AuthenticationManagerBuilder>

는 SecurityBuilder 되었습니다.AuthenticationManager메모리 인증, LDAP 인증, JDBC 기반 인증, User Details Service 추가Authentication Provider 추가가 용이합니다.

@Override
     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); 
        auth.userDetailsService(customUserDetailService).passwordEncoder(new BCryptPasswordEncoder());
     }

언급URL : https://stackoverflow.com/questions/22998731/httpsecurity-websecurity-and-authenticationmanagerbuilder

반응형