23 Nisan 2019 Salı

SpringSecurity WebSecurityConfigurerAdapter Sınıfı - Deprecate Edildi

Giriş
Şu satırı dahil ederiz.
import org.springframework.security.config.annotation.web.configuration
.WebSecurityConfigurerAdapter;
Multiple Entry Points - Farklı Ream'ler
Çoklu Realm yazısına taşıdım

Multiple Authentication Providers
Bir realm'e birden fazla AuthenticationProvider takılabilir. Bir örnek burada 

Örnek
Elimizde şöyle bir kod olsun.
@Autowired
private CustomAuthenticationProvider1 customProvider1;

@Autowired
private CustomAuthenticationProvider2 customProvider2;

@Autowired
private SAMLAuthenticationProvider samlProvider;
Şöyle yaparız
authenticationManagerBuilder.authenticationProvider(customProvider1);
authenticationManagerBuilder.authenticationProvider(customProvider2);
authenticationManagerBuilder.authenticationProvider(samlProvider);
Multiple Form Login
Eğer farklı kullanıcılar için farklı form login adresleri vermek istiyorsak yine bu sınıftan birden fazla yaratmak gerekebilir. Bir örnek burada.

Tanımlama
Bu sınıfı @Configuration anotasyonu ile birlikte kullanmak gerekir.
Örnek
Şöyle yaparız.
@Configuration
@EnableWebSecurity
public class SecurityAdapter extends WebSecurityConfigurerAdapter {
  ...
}
Bu tanımlama sonunda bir tane SecurityFilterChain arayüzünü gerçekleştiren nesne yaratılır. Bu nesne de FilterChainProxy sınıfının yaratılmasında kullanılır. 

Spring Hangi Filtreyi Kullanacağına Nasıl Karar Verir
Açıklaması şöyle
Authentication Flow
...
When an incoming request reaches our system, Spring Security starts by choosing the right security filter to process that request (Is the request a POST containing username and password elements? => UsernamePasswordAuthenticationFilter is chosen. Is the request having a header “Authorization : Basic base64encoded(username:password)”? => BasicAuthenticationFilter is chosen… and so the chaining goes on). When a filter had successfully retrieved Authentication information from the request, the AuthenticationManager is invoked to authenticate the request. via its implementation, the AuthenticationManager goes through each of the provided AuthenticationProvider(s) and try to authenticate the user based on the passed Authentication Object. when the Authentication is successful, and a matching user if found, an Authentication Object containing the user Authorities (which will be used to manage the user access to the system’s resources) is returned and set into the SecurityContext.
Şeklen şöyle. Burada önemli olan doğru filtreyi seçebilmek.


authenticationManagerBean metodu
Şöyle yaparız.
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
  return super.authenticationManagerBean();
}
configure metodu - AuthenticationManagerBuilder 
İmzası şöyle
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception;
AuthenticationManagerBuilder nesnesi ayarları yapılır. Yani kendi custom AuthenticationProvider nesnemizi takabiliriz. Custom AuthenticationProvider'lardan birisi

configure metodu - HttpSecurity 

İmzası şöyleHttpSecurity nesnesi ayarları yapılır.
@Override
protected void configure(HttpSecurity http) throws Exception;
Açıklaması şöyle
If we do not override the configure() method, a default filter chain is created as follows

protected void configure(HttpSecurity http) throws Exception {
  this.logger.debug("Using default configure(HttpSecurity). "
      + "If subclassed this will potentially override subclass configure(HttpSecurity).");
  http.authorizeRequests((requests) -> requests.anyRequest().authenticated());
  http.formLogin();
  http.httpBasic();
}
Neticede şu filtreler yaratılır
Each of these methods on the http object would lead to the addition of respective filters in the SecurityFilterChain. Inspect the methods, to know the individual filters being applied.

Here is a list of default filters that are applied:
WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter
HeaderWriterFilter
CsrfFilter
LogoutFilter
UsernamePasswordAuthenticationFilter
DefaultLoginPageGeneratingFilter
DefaultLogoutPageGeneratingFilter
BasicAuthenticationFilter
RequestCacheAwareFilter
SecurityContextHolderAwareRequestFilter
AnonymousAuthenticationFilter
SessionManagementFilter
ExceptionTranslationFilter
FilterSecurityInterceptor
FilterSecurityInterceptor ile "authorization" yapılır

configure metodu - WebSecurity 
Açıklaması şöyle. Yani WebSecurity ile ignoring() yapılan adresler SpringSecurity filtrelerine uğramazlar.
General use of WebSecurity ignoring() method omits Spring Security and none of Spring Security’s features will be available. WebSecurity is based above HttpSecurity.
Aslında hem WebSecurity hem de HttpSecurity sınıflarının builder oldukları paket isimlerinden görülebilir.
org.springframework.security.config.annotation.web.builders.WebSecurity
org.springframework.security.config.annotation.web.builders.HttpSecurity
Örnek
Şöyle yaparız
@Override
public void configure(WebSecurity web) throws Exception {
  web.ignoring().antMatchers("/resources/**","/static/**");
}
userDetailsServiceBean metodu
Şöyle yaparız.
@Override
@Bean
public UserDetailsService userDetailsServiceBean() throws Exception {
  return super.userDetailsServiceBean();
}

Hiç yorum yok:

Yorum Gönder