loadUserByUsername(String username)中的username参数为空-Spring Boot

阿什温

这是我的UserDetailService

public class StockUserDetailService implements UserDetailsService {
    @Autowired
    private UserRepository userRepository;
    private static final Logger logger = Logger.getLogger(StockUserDetailService.class);
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.debug("entering loadByUserName");
        // MongoDatabase database = mongoClient.getDatabase("springsecurity");
        User user = userRepository.findOne(username);
        logger.debug("this is teh user ibzect ="+user);
        if (user == null) {
            logger.info("----------------------------------------------user name is "+username);
            throw new UsernameNotFoundException("NAT FOUND");
        }
        return user;
    }
}

但是username争论总是这样来的null我在Stackoverflow上看到大量其他帖子,都遇到了同样的问题。他们中的一些人说这是因为它期望usernamepassword作为HTTP标头而不是JSON的一部分。

但是我认为这是不对的,因为Spring Boot的默认登录页面只有一种简单的形式,可以向POST发送请求/login这是我在做什么。

我不明白这里的问题。

PS:我已经正确配置了usernamepassword键名:

protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .authorizeRequests()
            .antMatchers("/signup**").permitAll()
            .antMatchers("/login/**").permitAll()
            .antMatchers("/logout/**").permitAll()
            .and()
        .authorizeRequests()
            .antMatchers("/admin/**").hasAuthority("ADMIN")
            .and()
        .formLogin()
            .usernameParameter("username").
            .passwordParameter("password");
}

以JSON(内容类型)的形式发送usernamepassword参数application/json,但是必须发送URL编码的参数(内容类型application/x-www-form-urlencoded),请参阅Spring Security Reference

4用户名必须作为名为username的HTTP参数存在

5密码必须作为名为password的HTTP参数存在

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我在哪里可以从Spring Security中获取“用户名”值,以传递给UserDetailsService接口的loadUserByUsername(String username)方法

Spring Boot @Value为空

Spring Boot Actuator Trace HTTP POST请求参数为空

Spring Boot中的请求参数在提交JavaScript请求时为空,但适用于正常请求

Spring Boot StackOverFlowError:空

Angular 8 + Spring Boot:不存在必需的String参数

在Spring Boot中自动接线时为空指针

Spring Boot错误响应中的“ message”字段为空

来自 Spring Boot 的 @Autowired 服务在 WAR 中为空

Spring Boot中的数据库字段为空的问题

Spring boot @Value 属性在测试用例中为空

如何从数据库中获取spring.boot.admin.client.username / password的值?

Spring Boot-源不得为空

Spring Boot-服务类为空

Spring Boot中的JSP

Spring Boot中的WebMvcTest

如何使用GenericStoredProcedure在Spring Boot中将String设置为输入参数?

Spring Boot bean空异常

Spring boot 获取请求参数

Spring Boot中构造函数的参数0

查询参数中的Spring Boot offsetDateTime

无法在Spring Boot中读取JVM参数

带有Spring Boot的Feign客户端:RequestParam.value()在参数0上为空

无法使用 Spring Boot jar 访问信任库文件:trustAnchors 参数必须为非空

在UsernamePasswordAuthenticationFilter Spring Boot中@autowired为null

Spring Boot中Spring Security的XML配置

Spring Boot 中 Spring Security 的困惑

在Spring Boot中检索bean?

Spring Boot中的异常处理