2013-08-01 34 views
8

Tôi đã tự hỏi tôi đang làm gì sai ở đây để xác thực người dùng. Tôi có một ứng dụng mà người dùng thực hiện một vài bước để kích hoạt tài khoản của họ và khi làm như vậy, tôi muốn bỏ qua biểu mẫu đăng nhập và đưa họ trực tiếp đến trang tổng quan của họ.Làm cách nào tôi có thể xác thực người dùng bằng bảo mật mùa xuân bằng cách sử dụng DaoAuthenticationProvider

Đây là những gì chức năng đăng nhập tự động của tôi trông giống như:

protected void automatedLogin(String username, String password, HttpServletRequest request) { 

     try { 
      // Must be called from request filtered by Spring Security, otherwise SecurityContextHolder is not updated 
      CustomUserDetailsService udService = new CustomUserDetailsService(userDAO, request); 
      UserDetails uDetails = udService.loadUserByUsername(username); 
      UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(uDetails, password); 
      token.setDetails(new WebAuthenticationDetails(request)); 
      DaoAuthenticationProvider authenticator = new DaoAuthenticationProvider(); 
      Authentication authentication = authenticator.authenticate(token); 
      SecurityContextHolder.getContext().setAuthentication(authentication); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      SecurityContextHolder.getContext().setAuthentication(null); 
     } 

    } 

tôi phải sử dụng lớp DaoAuthenticationProvider là nhà cung cấp chứng thực của tôi. Tôi đã xác minh rằng tôi nhận được một mô hình UserDetails chứa các chứng chỉ đúng, ID, vai trò thẩm quyền vv

Khi nó gọi phương thức xác thực tôi chạy vào một Null Pointer đâu đó dọc theo đường trong lớp DaoAuthenticationProvider:

org.springframework.security.authentication.AuthenticationServiceException tại org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser (DaoAuthenticationProvider.java:109) tại org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider. xác thực (AbstractUserDetail sAuthenticationProvider.java:132) tại com.bosch.actions.BaseController.doAutoLogin (BaseController.java:659) . . . Gây ra bởi: java.lang.NullPointerException tại org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser (DaoAuthenticationProvider.java:101)

Tôi thực sự không chắc chắn những gì là null, như tôi don' t có sẵn mã nguồn.

Sửa tôi đã có thể tìm ra mã nguồn ở đây - https://github.com/SpringSource/spring-security/blob/master/core/src/main/java/org/springframework/security/authentication/dao/DaoAuthenticationProvider.java

tôi đã có thể để có được xung quanh Null Pointer bằng cách thiết lập một cách rõ ràng UserDetailsService trên đối tượng:

authenticator.setUserDetailsService(udService); 

Nhưng bây giờ Tôi nhận được thông tin đăng nhập xấu ngoại lệ khi tôi biết mật khẩu được cung cấp là chính xác, bởi vì tôi đã nhìn thấy nó trong trình gỡ rối trong đối tượng UserDetails được đặt trước đó trong mã.

org.springframework.security.authentication.BadCredentialsException: credentials Xấu tại org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks (DaoAuthenticationProvider.java:87) tại org.springframework.security. authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate (AbstractUserDetailsAuthenticationProvider.java:149)

+0

Xuân An là mã nguồn mở, bạn có mã nguồn có sẵn. Bạn có thể gặp vấn đề bởi vì DaoAuthenticationProvider được thiết kế để trở thành một bean được quản lý mùa xuân. – samlewis

Trả lời

9

tôi đã có thể để có được sự xác thực làm việc bằng cách ghép lại với nhau tất cả các thuộc tính được định nghĩa trong định nghĩa bean mùa xuân và se tting chúng lập trình trên đối tượng DaoAuthenticationProvider. Nhìn lại điều này có vẻ như nó có thể là một câu hỏi ngớ ngẩn, nhưng tôi hy vọng nó sẽ giúp một ai đó!

Mã Corrected:

protected void automatedLogin(String username, String password, HttpServletRequest request) { 

     try { 
      // Must be called from request filtered by Spring Security, otherwise SecurityContextHolder is not updated 
      CustomUserDetailsService udService = new CustomUserDetailsService(userDAO, request); 
      CustomMd5PasswordEncoder passEncoder = new CustomMd5PasswordEncoder(); 
      ReflectionSaltSource saltSource = new ReflectionSaltSource(); 
      saltSource.setUserPropertyToUse("salt"); 
      UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); 
      token.setDetails(new WebAuthenticationDetails(request)); 
      DaoAuthenticationProvider authenticator = new DaoAuthenticationProvider(); 
      authenticator.setUserDetailsService(udService); 
      authenticator.setPasswordEncoder(passEncoder); 
      authenticator.setSaltSource(saltSource); 
      Authentication authentication = authenticator.authenticate(token); 
      SecurityContextHolder.getContext().setAuthentication(authentication); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      SecurityContextHolder.getContext().setAuthentication(null); 
     } 

    } 
+3

Rất vui khi bạn tìm thấy bản sửa lỗi, nhưng thành thật mà nói, đây không phải là cách bảo mật của Spring Security được sử dụng. Bạn đang làm rất nhiều công việc một mình, mà bạn không cần phải làm. – Akshay

+0

Đây không phải là cách sử dụng bình thường của Spring Security trong ứng dụng. Đây là trường hợp một lần mà tôi cần phải đăng nhập người dùng mà không cần họ cung cấp thông tin xác thực trong biểu mẫu. Mỗi lần khác, họ sẽ đăng nhập thông qua biểu mẫu và bean bảo mật mùa xuân sẽ thực hiện công việc. Có giải thích rằng, nếu bạn là một trong những người downvoted câu trả lời tôi hy vọng bạn thay đổi tâm trí của bạn, bởi vì tôi nghĩ rằng nó không công bằng làm tổn thương danh tiếng của tôi trên StackOverflow. – rawkfist0215

+0

Hãy yên tâm với bạn tôi, tôi đã không bỏ phiếu cho bạn. Tôi không nghĩ câu trả lời là không chính xác, tôi chỉ gợi ý rằng Spring Security không được sử dụng theo cách này. Và P.S. Bỏ phiếu xuống bạn sẽ giảm danh tiếng của tôi trên SO quá ... :) – Akshay

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