2017-08-09 142 views
-1

在Spring 4.3.x中,我有一個自定義類,將其稱爲MyWebAuthenticationDetails,用於擴展WebAuthenticationDetails。我需要使用在application.properties中定義的那個類中的屬性。我通過一個名爲AuthenticationProperties的自定義類來獲取這些屬性,該類使用@ConfigurationProperties。通常我會在類構造函數的AuthenticationProperties中自動裝入,但MyWebAuthenticationDetails不可能。我如何從WebAuthenticationDetails的擴展中訪問屬性?如何在自定義WebAuthenticationDetails中使用我的自定義ConfigurationProperties?

+0

添加您的相關代碼質疑 – user7294900

回答

0

由於您MyWebAuthenticationDetails定製的細節對象將通過AuthenticationDetailsSource豆(你應該已經申報)來構建,您可以訪問AuthenticationProperties作爲注入豆因此,你將有你所有的屬性訪問。

一個簡單的Java配置模板將如下所示(請注意,這不是一個完整的功能配置,僅旨在突出重要的配置項):

@Configuration 
@EnableWebMvcSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authenticationDetailsSource(myAuthenticationDetailsSource())/* and all the missiong HTTP configuration*/; 
    } 

    @Bean 
    private AuthenticationDetailsSource<HttpServletRequest, MyWebAuthenticationDetails> myAuthenticationDetailsSource() { 
     return new MyAuthenticationDetailsSource<HttpServletRequest, MyWebAuthenticationDetails>(); 
    } 

    private final class MyAuthenticationDetailsSource extends AuthenticationDetailsSourceImpl<HttpServletRequest, MyWebAuthenticationDetails> { 

     @Autowired 
     private AuthenticationProperties authenticationProperties; 

     @Override 
     public MyWebAuthenticationDetails buildDetails(HttpServletRequest request) { 
      return new MyWebAuthenticationDetails(request, this.authenticationProperties); 
     } 
    } 
} 
+0

我完全忘記了這是通過AuthenticationDetailsS​​ource impl創建的。謝謝! – SpringGuy

相關問題