【问题标题】:I can't Persist data within Spring boot Authentication Provider我无法在 Spring Boot Authentication Provider 中保留数据
【发布时间】:2021-01-26 15:41:01
【问题描述】:

我无法在AuthenticationProvider 中持久化数据,cashierRepo.save(cashierDAO) 正在抛出nullPointerException

@Component
public class TACoreAuthProvider implements AuthenticationProvider {

    UsernamePasswordAuthenticationToken userToken;

    @Autowired
    CashierDAOServiceImpl cashierRepo;

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

        Mono<LoginResponseDAO> loginResponseDAO = tacoreAuth(username, password);
        loginResponseDAO.subscribe(responseDAO->{
            System.out.print(responseDAO.toString());
            if(!responseDAO.getStatus().equalsIgnoreCase("success")){
                throw new BadCredentialsException("External system authentication failed");
            }
            responseDAO.getData().setUserName(username);
            CashierDAO cashierDAO = responseDAO.getData();
            cashierRepo.save(cashierDAO);
            
            Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
            grantedAuthorities.add(new SimpleGrantedAuthority(responseDAO.getData().getUser_category()));
            userToken = new UsernamePasswordAuthenticationToken(username, password, grantedAuthorities);

        });

【问题讨论】:

  • 你能澄清一下null到底是什么吗?术语CrudRepository 未出现在您发布的代码中。

标签: java spring-boot spring-security spring-data-jpa


【解决方案1】:

感谢大家,我已经能够找出问题所在。

@Bean
public static TACoreAuthProvider getAuthProvider(CashierDAOServiceImpl cashierDAOService){
        return new TACoreAuthProvider(cashierDAOService);
    }

在实现它的静态类“ApiWebSecurityConfigurationAdapter”之外声明,因为“TACoreAuthProvider”实例为空。

@EnableWebSecurity
public class SecurityConfig  {
    @Bean
public static TACoreAuthProvider getAuthProvider(CashierDAOServiceImpl cashierDAOService){
        return new TACoreAuthProvider(cashierDAOService);
    }

 @Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends 
    WebSecurityConfigurerAdapter{

    @Bean
    public CustomBasicAuthenticationEntryPoint getBasicAuthEntryPoint(){
        return new CustomBasicAuthenticationEntryPoint();
    }

    @Override
    protected void configure(HttpSecurity http)throws Exception{
        http.csrf().disable()
                //.addFilterAfter(new JWTAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                .antMatcher("/api/**")
                .authorizeRequests().anyRequest().authenticated()
                .and()
                .httpBasic().authenticationEntryPoint(getBasicAuthEntryPoint())
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.authenticationProvider(getAuthProvider(cashierDAOService));

    }

}

下面的代码是工作版本

 @EnableWebSecurity
public class SecurityConfig  {

 @Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends 
    WebSecurityConfigurerAdapter{

    @Bean
public static TACoreAuthProvider getAuthProvider(CashierDAOServiceImpl cashierDAOService){
        return new TACoreAuthProvider(cashierDAOService);
    }

    @Bean
    public CustomBasicAuthenticationEntryPoint getBasicAuthEntryPoint(){
        return new CustomBasicAuthenticationEntryPoint();
    }

    @Override
    protected void configure(HttpSecurity http)throws Exception{
        http.csrf().disable()
                //.addFilterAfter(new JWTAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                .antMatcher("/api/**")
                .authorizeRequests().anyRequest().authenticated()
                .and()
                .httpBasic().authenticationEntryPoint(getBasicAuthEntryPoint())
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.authenticationProvider(getAuthProvider(cashierDAOService));

    }

}

【讨论】:

    猜你喜欢
    • 2017-05-24
    • 2019-06-02
    • 2023-02-10
    • 2022-01-07
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多