2016-04-19 32 views
7

Bir REST tabanlı web uygulaması kurmaya çalışıyorum; burada ön uç Reactjs'yi kullanıyor ve arka uç Spring Boot kullanıyor. Ayrıca özel bir kimlik doğrulama sağlayıcısı kurmaya çalışıyorum ve sorunların başladığı yer burası. Oturum açma API çağrısını test etmeye çalışırken, CustomAuthenticationProvider hiçbir zaman çağrılmaz ve bunun yerine varsayılan DaoAuthenticationProvider kullanılır. Bu, girişin "Kötü kimlik bilgileri" bildirmesine neden olur. spring-boot-auth-demoBahar Yapılandırma Özelleştirme Kimlik Doğrulaması Java Yapılandırması ile FIXED

aşağıdaki curl kullandığınız giriş API test etmek için::

github için küçük bir örnek uygulama yüklemek zorunda

curl -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"admin"}' http://localhost:8080/api/users/login 

CustomAuthenticationProvider basit kullanıcı adı/şifre kontrolünü yapar ve bir UsernamePasswordAuthenicationToken döndürür nesne.

package no.bluebit.demo; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.security.authentication.AuthenticationProvider; 
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 
import org.springframework.security.core.Authentication; 
import org.springframework.security.core.AuthenticationException; 
import org.springframework.security.core.GrantedAuthority; 
import org.springframework.security.core.authority.SimpleGrantedAuthority; 
import org.springframework.stereotype.Component; 

import java.util.ArrayList; 
import java.util.List; 

@Component 
public class CustomAuthenticationProvider implements AuthenticationProvider { 

private static final Logger logger =  LoggerFactory.getLogger(CustomAuthenticationProvider.class); 

public CustomAuthenticationProvider() { 
    logger.info("*** CustomAuthenticationProvider created"); 
} 

@Override 
public Authentication authenticate(Authentication authentication) throws AuthenticationException { 

    if(authentication.getName().equals("admin") && authentication.getCredentials().equals("admin")) { 
     List<GrantedAuthority> grantedAuths = new ArrayList<>(); 
     grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); 
     grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN")); 
     return new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuths); 
    } else { 
     return null; 
    } 

} 

@Override 
public boolean supports(Class<?> authentication) { 
    return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication); 
} 

} 

CustomAuthenticationProvider

SecurityConfiguration sınıfını kullanarak kablolu. Kodun üzerinden geçerken, CustomAuthenicationProvider'ın gelen isteği doğrulamak için kullanılan sağlayıcılar listesinde olmadığını görebiliyorum.

package no.bluebit.demo; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private CustomAuthenticationProvider customAuthenticationProvider; 

    @Autowired 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .authenticationProvider(this.customAuthenticationProvider); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/api/users/login").permitAll() // Permit access for all to login REST service 
       .antMatchers("/").permitAll()     // Neccessary to permit access to default document 
      .anyRequest().authenticated().and()     // All other requests require authentication 
      .httpBasic().and() 
      .logout().and() 
      .csrf().disable(); 
    } 
} 

Bu neden çalışmıyor? defaut biridir null döndürür, sonra bir sonraki AuthenticationProvider adı verilecek

* Performs authentication with the same contract as 
* {@link org.springframework.security.authentication.AuthenticationManager#authenticate(Authentication)} 
* @return a fully authenticated object including credentials. May return 
* <code>null</code> if the <code>AuthenticationProvider</code> is unable to support 
* authentication of the passed <code>Authentication</code> object. In such a case, 
* the next <code>AuthenticationProvider</code> that supports the presented 
* <code>Authentication</code> class will be tried. 

: AuthenticationProvider sınıfta

+1

Şuna bir bakın: http://stackoverflow.com/questions/22453550/custom-authentication-provider-not-being-called/22457561#22457561 – franDayz

+0

Teşekkür ederiz! Eksik @ Otomatik kablolu açıklama sorun oldu. Sorun çözüldü! –

+0

@franDayz yorumunuzu cevap olarak ekleyebilir, böylece Håvard Bakke bir cevap olarak kabul edebilir mi? – demaniak

cevap

-1

Bak

yöntem kimlik doğrulaması bekliyor (sırasıyla bu java, Doktor) .

Bunun sorun olduğundan emin değilim, ancak bu bir şey olabilir. AuthenticationManager sınıf Yapmanız anlatır gibi BadCredentialsException atmak deneyin:

* <li>A {@link BadCredentialsException} must be thrown if incorrect credentials are 
* presented. Whilst the above exceptions are optional, an 
* <code>AuthenticationManager</code> must <B>always</B> test credentials.</li> 
0

Sen başka bir şekilde kimlik bilgilerini belirlemek zorunda. Kullanıcı adı parolası belirtecinin çalışan bir örneğini görmeye çalışın. Ama "kimlik doğrula" işlevinin kimlik bilgilerini ayarlayan biri olması gerekir.

İlgili konular