2017-08-09 7 views
-1

Spring 4.3.xでは、WebAuthenticationDetailsを拡張するMyWebAuthenticationDetailsというカスタムクラスがあります。私は、そのクラスでapplication.propertiesに定義されているプロパティを使用する必要があります。 @ConfigurationPropertiesを使用するAuthenticationPropertiesというカスタムクラスを介してこれらのプロパティを取得します。通常は、クラスコンストラクタのAuthenticationPropertiesにautowireしますが、MyWebAuthenticationDetailsではこれを行うことはできません。 WebAuthenticationDetailsの拡張機能からプロパティにアクセスするにはどうすればよいですか?カスタムWebAuthenticationDetailsでカスタム設定プロパティを使用するにはどうすればよいですか?

+0

疑問をあなたの関連するコードを追加します。 – user7294900

答えて

0

詳細オブジェクトをあなたのMyWebAuthenticationDetailsカスタムは(すでに宣言している必要があります)AuthenticationDetailsSource豆により構築されますので、あなたは、このようにすべてのあなたのプロパティにアクセスしています注入されたBeanとして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

関連する問題