springboot中的spring security未引发usernotfoundexception

k97glaaz  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(289)

我正在使用启用了Spring Security 的spring启动应用程序。我使用自定义jdbc身份验证。在我的用户详细信息服务impl中,我抛出usernamenotfound异常,但它不会被记录到任何地方。我也没在控制台上看到任何东西。
下面是我的userdetailservice impl

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class ApplicationUserDetailService implements UserDetailsService{

    private ApplicationUserDao applicationUserDao;

    @Autowired
    public ApplicationUserDetailService(@Qualifier("db") ApplicationUserDao applicationUserDao) {
        this.applicationUserDao=applicationUserDao;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return applicationUserDao.loadUserByUsername(username)
                .orElseThrow(()->{
                    System.out.println("here...");
                    throw new UsernameNotFoundException("dsdsds");
                });
        }

}

下面是jdbc身份验证的安全配置

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

@Bean
public DaoAuthenticationProvider provider() {
    DaoAuthenticationProvider pr=new DaoAuthenticationProvider();
    pr.setUserDetailsService(applicationUserDetailService);
    pr.setPasswordEncoder(encoder);
    return pr;
}

用户详细信息

import java.util.Optional;    
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

@Repository("db")
public class DBUserDaoImpl implements ApplicationUserDao{

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public DBUserDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate=jdbcTemplate;
    }

    @Override
    public Optional<ApplicationUser> loadUserByUsername(String username) throws UsernameNotFoundException {
        return Optional.ofNullable(jdbcTemplate.query("select * from users where username='"+username+"'"
                ,new UserExtractor(jdbcTemplate)));
    }

}

当我发送错误的细节,我应该得到例外,但它不会来。请帮帮我。Spring Boot2.4.2

dkqlctbz

dkqlctbz1#

UsernameNotFoundException 通常情况下,运行服务器的人并不需要知道,因此Spring Security 不会为它打印消息。这是一个错误,是用户的错误(他们输入了一个不存在的帐户)。
不管怎样,如果您想打印消息,可以执行以下操作:

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    return applicationUserDao.loadUserByUsername(username).orElseThrow(() -> {
        // Create an exception, but don't throw it yet
        var exception = new UsernameNotFoundException("No account found with name " + username);
        // Print the exception
        exception.printStackTrace();
        // Return the exception (throwing works too, but everything I can find says to return it)
        return exception;
    });
}

相关问题