实现ServletConextAware后,ServletContext实例在AuthenticationProvider中不可用

时间:2016-04-11 04:34:10

标签: spring spring-mvc spring-boot

我有一个

    通过实施[Event "MT-Bielecki/Top (POL)"] [Site "ICCF"] [Date "2012.3.1"] [Round "-"] [White "Langeveld, Ron A. H."] [Black "Starke, Dr. René-Reiner"] [Result "1/2-1/2"] [WhiteElo "2681"] [BlackElo "2620"] 1.e4 c5 2.Nf3 Nc6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 e5 6.Ndb5 d6 7.Bg5 a6 8.Na3 b5 9.Bxf6 gxf6 10.Nd5 f5 11.Bd3 Be6 12.O-O Bxd5 13.exd5 Ne7 14.Nxb5 Bg7 15.Nc3 e4 16.Bc4 Ng6 17.Qh5 Bxc3 18.bxc3 Qf6 19.Qh6 Qxc3 20.Be2 Qe5 21.g3 Ke7 22.Rae1 Rac8 23.c4 f4 24.Bd3 f5 25.Be2 Rb8 26.Kh1 Kd8 27.gxf4 Nxf4 28.c5 dxc5 29.Qc6 Rc8 30.Qb6+ Ke7 31.f3 Nxd5 32.Qxa6 Rcd8 33.Bc4 Nc7 34.Qh6 Rd4 35.Bb3 Rf8 36.fxe4 Rxe4 37.Rb1 Nd5 38.Bc2 Re8 39.Rfd1 Rb4 40.Qxh7+ Kd6 41.Rxb4 cxb4 42.Bxf5 Re7 43.Qh3 Qe1+ 44.Rxe1 Rxe1+ 45.Kg2 Nf4+ 46.Kf2 Nxh3+ 47.Kxe1 1/2-1/2
  1. CustomAuthenticationProvider 通过实施AuthenticationProvider
  2. CustomUserDetailsService

    问题

    我已经在实施中实施了UserDetailsService,我只能在ServletContextAware而不是ServletContextAware获得CustomUserDetailsService

    背后的原因是什么?

    为了获得CustomAuthenticationProvider中的ServletContext,最好的方法是什么?

    示例如下。

    CustomAuthenticationProvider的示例

    CustomUserDetailsService

    CustomUserDetailsS​​ervice的示例

    public CustomAuthenticationProvider implements AuthenticationProvider, ServletContextAware{
    
          private ServletContext servletContext;
    
           @override
           public Authentication authenticate(Authentication authentication)
                         throws AuthenticationException{
    
                System.out.println("Context Path :: "+servletContext);  // Here the reference is NULL
           } 
    
           @Override 
           void setServletContext(ServletContext servletContext){
              this.servletContext = servletContext;
           }  
    }
    

    配置

    public CustomUserDetailsService implements UserDetailsService, ServletContextAware{
    
            private ServletContext servletContext;
    
            @Override
            public UserDetails loadByUserName(final String login){
    
                System.out.println("Context Path :: "+servletContext);  // Here the reference is NOT NULL
            }
    
    
           @Override 
           void setServletContext(ServletContext servletContext){
              this.servletContext = servletContext;
           }  
    
    
    }
    

    请建议。

0 个答案:

没有答案