HttpSecurity, WebSecurity 및 AuthenticationManagerBuilder


답변:


125

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


2
좋은 대답 닉. spring-security-config-5.0.3 (spring-boot 2.0.0과 함께 제공됨)을 사용하면 메소드를 찾을 수 없었습니다 http.authorizeUrls(). 아마 http.authorizeRequests()얼마 전에 이름이 변경되었을 수 있습니다 .
— Yi Ou

5
이것이 오래되었다는 것을 알고 있지만 여기서 가장 좋은 방법은 무엇입니까? http.antMatchers ( "/ foo"). permitAll () "을 호출하는 configure (HttpSecurity http) 메서드 구현의 예를 찾았습니다. 웹) 방법.
— chrisinmtown

좋은 대답입니다. HttpSecurity에서 permitAll을 호출해야하는지 궁금합니다. WebSecurity를 ​​사용하여 / register 또는 / login과 같은 모든 열린 URL을 무시할 수 없습니까? 그렇다면 모든 자습서 또는 답변이 / register 및 / login에는 HttpSecurity.permitAll을 사용하고 / publics of / resources에는 WebSecurity.ingore를 사용하는 이유는 무엇입니까? –
— Mohd Waseem

3

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

@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();
}

위의 예제에서 WebSecurity는 Spring이 /resources/**및 /publics/**. 따라서 .antMatchers("/publics/**").hasRole("USER")HttpSecurity에 unconsidered .

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

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

configure(WebSecurity)전역 보안에 영향 을 주는 구성 설정에 사용됩니다 (자원 무시, 디버그 모드 설정, 사용자 지정 방화벽 정의를 구현하여 요청 거부). 예를 들어, 다음과 같은 방법으로 시작하는 모든 요청 원인이 /resources/될 인증 무시 목적.

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

SecurityBuilder는 AuthenticationManager. 메모리 인증, LDAP 인증, JDBC 기반 인증, UserDetailsService 추가 및 AuthenticationProvider의 추가 를 쉽게 구축 할 수 있습니다 .

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

좋은 대답입니다. HttpSecurity에서 permitAll을 호출해야하는지 궁금합니다. WebSecurity를 ​​사용하여 / register 또는 / login과 같은 모든 열린 URL을 무시할 수 없습니까? 그렇다면 모든 자습서 또는 답변이 / register 및 / login에는 HttpSecurity.permitAll을 사용하고 / publics of / resources에는 WebSecurity.ingore를 사용하는 이유는 무엇입니까?
— Mohd Waseem
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.