2013-05-24 34 views
5

H i đang sử dụng an ninh mùa xuânmùa xuân an ninh 3 http-cơ bản chứng thực thành công-handler

cho hình thức đăng nhập tôi có

<http auto-config="true"> 
     <intercept-url pattern="/pages/**" access="ROLE_USER" /> 
     <form-login authentication-success-handler-ref="authenticationSuccessHandler" login-page="/login.html" default-target-url="/pages/index.html" 
      always-use-default-target="true" authentication-failure-url="/login.html" /> 
     <logout logout-success-url="/login.html" invalidate-session="true" /> 
     <anonymous enabled='false'/> 
</http> 

đây tôi có thể thiết lập một authentication-success-handler-ref, làm thế nào tôi có thể thêm một vào xác thực cơ bản của tôi:

<http pattern="/REST/**" realm="REALM" entry-point-ref="authenticationEntryPoint"> 
    <intercept-url pattern="/**" access="ROLE_USER" /> 
    <http-basic /> 
    <logout logout-url="/REST/logout" success-handler-ref="restLogoutSuccessHandler" /> 
</http> 

tôi nghĩ rằng hãy ghi đè lên BasicAuthenticationFilter, nhưng làm cách nào tôi có thể tiêm lớp cutom cho <http-basic />

Trả lời

5

Bạn không thể đặt trình xử lý thành công xác thực để xác thực BASIC. Bạn có thể, tuy nhiên, mở rộng BasicAuthenticationFilter và ghi đè onSuccessfulAuthentication phương pháp:

@Component("customBasicAuthFilter") 
public class CustomBasicAuthFilter extends BasicAuthenticationFilter { 

    @Autowired 
    public CustomBasicAuthFilter(AuthenticationManager authenticationManager) { 
     super(authenticationManager); 
    } 

    protected void onSuccessfulAuthentication(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Authentication authResult) { 
     // Do what you want here 
    } 
} 

Tiêm nó trong cấu hình bảo mật của bạn với một cái gì đó như:

<http entry-point-ref="basicEntryPoint"> 
    <custom-filter ref="customBasicAuthFilter" position="BASIC_AUTH_FILTER"/> 
</http> 
<authentication-manager alias="authenticationManager"> 
    ... 
</authentication-manager> 

Cập nhật: Hoặc với Java cấu hình thay vì XML:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .addFilterAt(customBasicAuthFilter, BasicAuthenticationFilter.class) 
     .exceptionHandling().authenticationEntryPoint(basicEntryPoint); 
} 
+0

Có thể cung cấp cấu hình Java cho XML này không? –

+0

@Jadda Có, xem cập nhật. – holmis83

3

Là một workaround bạn có thể sử dụng http-cơ bản trong conjuction với hình thức đăng nhập:

<http auto-config="true"> 
    ... 
    <http-basic /> 
    <form-login authentication-success-handler-ref="authenticationSuccessHandler" ... /> 
    ... 
</http> 

BasicAuthenticationFilter sẽ làm việc.

EDIT. Nếu bạn muốn thiết lập phiên bản overriden lại BasicAuthenticationFilter Tôi nghĩ rằng bạn cần phải:

  1. Thêm nó để lọc chuỗi ở vị trí BASIC_AUTH_FILTER như được giải thích here
  2. Thiết lập tương ứng với điểm vào BasicAuthenticationEntryPoint qua entry-point- ref thuộc tính của http thẻ.
+0

hi với điều này tôi luôn nhận được biểu mẫu đăng nhập và không có challange xác thực – wutzebaer

+0

chính xác, nhưng cơ bản xác thực phải được sử dụng sau khi gửi biểu mẫu –

+0

nhưng tôi cần hàm xác thực cơ bản trên url/REST của tôi – wutzebaer

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