-2

XMLの設定で私のコード:春のセキュリティのJava設定に認証プロバイダーを追加

<authentication-manager alias="authenticationManager"> 
    <authentication-provider ref="customAuthenticationProvider" /> 
</authentication-manager> 

がうまく働いたが、私は、Javaコードに変換することはできません。

エラー:

java.lang.NullPointerException 
org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:164) 
org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199) 
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94) 
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66) 
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) 
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) 
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) 
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) 

WebSecurityConfiguration

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    // @Autowired 
    private CustomAuthenticationProvider customAuthenticationProvider; 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .authenticationProvider(customAuthenticationProvider); // .build() 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web 
       .ignoring() 
       .antMatchers("/resources/**") 
       .antMatchers("/assets/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
     //  .authenticationProvider(customAuthenticationProvider) 
       .authorizeRequests() 
        .antMatchers("/login","/logout").permitAll().antMatchers("/admin/**", "/admin*").hasRole("ADMIN").antMatchers("/**").hasRole("USER") 
        .anyRequest().authenticated() 
        .and() 
       .formLogin() 
        .loginPage("/login") 
        .usernameParameter("username").passwordParameter("password") 
        .defaultSuccessUrl("/") 
        .permitAll() 
        .and() 
        .exceptionHandling().accessDeniedPage("/Access_Denied") 
        .and() 
       .logout() 
        .permitAll(); 

     http.csrf().disable(); 
    } 

    // @Bean 
    // DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler() { 
    //  return new DefaultWebSecurityExpressionHandler(); 
    // } 
} 

CustomAuthenticationProvider

// @Component 
public class CustomAuthenticationProvider implements AuthenticationProvider { 
    private final CustomUserDetailsService userDetailsService; 

    @Autowired 
    public CustomAuthenticationProvider(CustomUserDetailsService userDetailsService) { 
     this.userDetailsService = userDetailsService; 
    } 

    @Override 
    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
     String username = authentication.getName().toLowerCase(); 
     String password = (String) authentication.getCredentials(); 

     userDetailsService.setPassword(password); 
     User user = userDetailsService.loadUserByUsername(username); 

     if (user == null) { 
      throw new BadCredentialsException("Username not found."); 
     } 

     if (!password.equals(user.getPassword())) { 
      throw new BadCredentialsException("Wrong password."); 
     } 

     userDetailsService.setAuthorized(username); 

     Collection<?extends GrantedAuthority> authorities = user.getAuthorities(); 

     return new UsernamePasswordAuthenticationToken(user, password, authorities); 
    } 

    @Override 
    public boolean supports(Class<?> aClass) { 
     return true; 
    } 
} 
+1

あなたが投稿したコードは、 'CustomAuthenticationProvider'のインスタンスを取得する方法がありません。だから、あなたはどこかで非スプリング管理Beanとしてそれを作成しなければなりません。実際のコードを設定だけではなく投稿してください。 –

+0

M. Deinumコードを示しました! –

答えて

-1

あなたは春のブートを使用している場合は、

@configuration 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ 

@Override 
    protected void configure(final AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(customAuthenticationProvider()); 
    } 

//customAuthenticationProvider() method creates your bean 

} 
を行うことができます

//追加されたコードの後に​​編集

[OK]この問題は、認証プロバイダのインストラクションではなく、独自のコードで確認できます。 NPEはカスタム実装で何かが初期化されていないことを示します。提供されたオートワイヤーは正しくありますか、それはすべてのデープを持っていますか? What is a NullPointerException, and how do I fix it?

+1

私はSpring Bootを使用していません! –

+1

問題はありません、あなたの問題はNPEです。春の設定ではありません – Nadir

+0

良い解決策、私はそれが私のために働くことを望んだが、残念ながらそれはしません。私の設定はProviderManagerの作成後にロードされ、プロバイダのリストを設定するので、私の設定は無視されます... これをどのように解決しましたか? –

関連する問題