Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
springboot中的spring安全性未引发UserNotFoundException_Spring_Spring Boot_Spring Security - Fatal编程技术网

springboot中的spring安全性未引发UserNotFoundException

springboot中的spring安全性未引发UserNotFoundException,spring,spring-boot,spring-security,Spring,Spring Boot,Spring Security,我正在使用启用了spring安全性的spring boot应用程序。我正在使用自定义jdbc身份验证。在我的用户详细信息服务impl中,我抛出usernamenotfound异常,但它不会被记录到任何地方。我也没有在控制台上看到任何东西 下面是我的UserDetailService impl import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory

我正在使用启用了spring安全性的spring boot应用程序。我正在使用自定义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;
}
UserDetailDaoImpl

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 boot 2.4.2

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;
    });
}

我应该得到例外,但它不来,那么什么来了?问题应该用当前的行为和调试日志更新。嗨@Toerktumlare,我在控制台里什么也没得到。仅获取已完成的dispatcher servlet请求。日志--初始化Spring DispatcherServlet'DispatcherServlet'o.s.web.servlet.DispatcherServlet:初始化servlet'DispatcherServlet'o.s.web.servlet.DispatcherServlet:在0毫秒内完成初始化请发布完整的调试服务器日志。1。你的问题很危险,2。为什么要在UserExtractor中使用模板?3.UserExtractor具体做什么?UserExtractor将结果集的结果映射到UserDetails类impl。jdbc模板被传递,因为在用户提取器中,如果我们找到了用户,那么我将获得用户的角色,然后构建应用程序用户进行身份验证。它也不起作用。Repo url-@Sahil您的Repo仅包含自述。如果您正在打印堆栈跟踪,但什么也没有发生,那么您的代码可能没有被调用。代码位于主分支下。现在github默认创建主分支,但不知道为什么。代码在主分支下。