15 Eylül 2020 Salı

SpringSecurity ExpressionUrlAuthorizationConfigurer - HttpSecurity İle Kullanılır, AuthorizedRequests İçindir

Giriş
Bu sınıfın içinde iki tane sınıf daha var. Bunlar ExpressionInterceptUrlRegistry ve AuthrorizedUrl sınıfları. Bu yazıda her ikisinin metodlarını sanki birmiş anlattım.

Bu sınıfı httpSecurity.authorizedRequests() çağrısı ile elde ederiz.

Genel Kullanım
Önce herkesin doğrulamaya gerek kalmadan erişebileceği adresler belirtilir
Daha sonra belli rollere sahip kullanıcıların erişebileceği adresler belirtilir
Daha sonra doğrulanmış herhangi bir kullanıcının erişebileceği ortak adresler belirtilir.

Bu adımlar şöyle
1. antMatcher().permitAll() ile adrese herkesin erişmesi sağlanır
Örneğin OAuth doğrulaması yaptıktan sonra redirect url için permitAll() yapmak gerekir.

2. antMatcher().hasRole(), antMatcher().access() ile adrese doğrulama yapılması sağlanır
3. anyRequest().authenticated()  ile geri kalan adreslere doğrulama yapılması sağlanır.

Role ve Authority Farkı
Aslında bu ikisi aynı şey gibi anlatılıyor. Açıklaması şöyle
Roles and authorities are similar in Spring.

The main difference is that, roles have special semantics – starting with Spring Security 4, the ‘ROLE_‘ prefix is automatically added (if it's not already there) by any role related method.

So hasAuthority(‘ROLE_ADMIN') is similar to hasRole(‘ADMIN') because the ‘ROLE_‘ prefix gets added automatically.
Ancak aslında biraz farklı şeyler. Açıklaması şöyle.
ROLE_ prefix is used by spring security, to identify that it is as a role. A role has a set of privileges a.k.a Authorities, these authorities define varies permissions for a role. ex:- EDIT_PROFILE, DELETE_PROFILE

You can define both the roles and authorities, if you are defining a role then it must be prefixed with "ROLE_"
Role isimleri ROLE_ öneki ile başlamıyorsa Spring otomatik ekler. Spring dokümanındaki açıklama şöyle.
By default if the supplied role does not start with 'ROLE_' it will be added. This can be customized by modifying the defaultRolePrefix on DefaultWebSecurityExpressionHandler.
access metodu
Şöyle yaparız
antMatchers("/some-action-**").access("hasAuthority('ADMIN')")
antMatchers  metodu - URL
ANT tarzı URL kullanabilmeyi sağlar.
Örnek
Şöyle yaparız.
http
  .authorizeRequests()
  .antMatchers("/index.html", "/", "/home", "/login").permitAll()
  .anyRequest().authenticated()
antMatchers  metodu - HttpMethod + URL
Örnek
Şöyle yaparız
@Override
protected void configure(HttpSecurity http) throws Exception{
  http.authorizeRequests()
    .antMatchers(HttpMethod.POST,"/file/file-upload").hasAuthority("ADMIN")
    .antMatchers(HttpMethod.POST,"/data/add-data").hasAuthority("ADMIN")
    .antMatchers(HttpMethod.GET,"/data").hasAnyAuthority("ADMIN","USER")
    .antMatchers(HttpMethod.POST, SecurityConstants.SIGN_UP_URL).permitAll()
    .anyRequest().authenticated()
  .and()
    ...
}
anyRequest metodu
Genellikle authenticated() veya hasRole() metodu çağrılır.

authenticated() metodu ile kullanıcının bir yöntem ile doğrulanmış olması gerekir.
hasRole() metodu ile kullanıcının hem doğrulanmış hem de belirtilen role sahip olması gerekir.

Örnek
Şöyle yaparız.
http.authorizeRequests()
  // api security is handled elsewhere (See OAuth2ServerConfiguration)
  .antMatchers("/api/**", "/oauth/**", "/management/**")
  .permitAll()
  // end api security
  .anyRequest().hasRole(UserRole.ADMIN.name())
  .and()
  .formLogin().loginPage("/login")
  .permitAll()
  .and()
  .logout().permitAll();
mvcMatchers metodu
Örnek
Şöyle yaparız
http.authorizeRequests()
  .mvcMatchers("/books").hasRole("admin")
  .mvcMatchers("/book").hasRole("admin")
  .mvcMatchers("/").permitAll()
permitAll metodu
Belirtilen adreslere herkesin erişebilmesini sağlar. Açıklaması şöyle
...will configure the authorization so that all requests are allowed on that particular path:
...
This is achieved without disabling the security filters – these still run, so any Spring Security related functionality will still be available.
Örnek
Şöyle yaparız.
 http.authorizeRequests()
     .antMatchers("/restservice/**").permitAll();

Hiç yorum yok:

Yorum Gönder