2016-04-19 14 views
7

Tôi đang cố gắng thiết lập ứng dụng web dựa trên REST, nơi giao diện người dùng đang sử dụng Reactj và chương trình phụ trợ đang sử dụng Khởi động mùa xuân. Tôi cũng đang cố gắng thiết lập nhà cung cấp xác thực tùy chỉnh và đây là nơi các sự cố của tôi bắt đầu. Khi cố gắng kiểm tra cuộc gọi API đăng nhập, CustomAuthenticationProvider không bao giờ được gọi và thay vào đó, DaoAuthenticationProvider mặc định được sử dụng. Điều này khiến thông tin đăng nhập báo cáo "Thông tin đăng nhập không hợp lệ".Nhà cung cấp xác thực tùy chỉnh khởi động Spring với Cấu hình Java FIXED

Tôi có tải lên một ứng dụng mẫu nhỏ để github: spring-boot-auth-demo

Để kiểm tra API đăng nhập tôi sử dụng curl sau:

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

Các CustomAuthenticationProvider hiện một đơn giản username/kiểm tra mật khẩu và trả về một UsernamePasswordAuthenicationToken vật.

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 được kết nối bằng lớp SecurityConfiguration. Khi bước qua mã, tôi có thể thấy rằng CustomAuthenicationProvider không có trong danh sách các nhà cung cấp được sử dụng để xác thực yêu cầu gửi đến.

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

Tại sao tính năng này không hoạt động?

+1

Hãy xem xét điều này: http://stackoverflow.com/questions/22453550/custom-authentication-provider-not-being-called/22457561#22457561 – franDayz

+0

Cảm ơn bạn! Chú thích @Autowired bị thiếu là vấn đề. Đã giải quyết được vấn đề! –

+0

@franDayz có thể thêm bình luận của bạn như là một câu trả lời, để Håvard Bakke có thể chấp nhận như một câu trả lời? – demaniak

Trả lời

-1

Nhìn vào lớp AuthenticationProvider (tương ứng đó là java doc)

Phương pháp xác thực hy vọng sẽ:

* 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. 

Nếu bạn quay trở lại null, thì AuthenticationProvider tiếp theo sẽ được gọi, đó là một defaut .

Tôi không chắc đây là vấn đề, nhưng điều này có thể là một cái gì đó. Hãy thử ném BadCredentialsException, vì lớp AuthenticationManager cho bạn biết:

* <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

Bạn phải đặt bằng chứng theo một cách khác. Hãy thử xem ví dụ hoạt động của mã thông báo Mật khẩu tên người dùng. Nhưng chức năng "xác thực" của bạn cần phải là chức năng đặt thông tin đăng nhập.

Các vấn đề liên quan