Spring Security登录时的错误信息显示不出来

2023-12-14 12:51:00

最近学习 Spring,书中示例如下:

<div th:if = "${error}">
    Unable to login. Check your username and password.
</div>

实测输入错误的用户名或密码,上面的提示信息不显示,参照这篇文章解决

<div th:if="${param.error}">
    <span th:text="${session?.SPRING_SECURITY_LAST_EXCEPTION?.message}" style="color:red;"></span>
</div>

另外还有个问题,装配了自定义的 UserDetailsService,但是没生效:

@Configuration
public class SecurityConfig {

    @Bean
    public UserDetailsService userDetailsService(UserRepository repository) {
        return username -> {
            User user = repository.findByUsername(username);
            System.out.println("################################# user=" + user);
            if(user == null) {
                throw new UsernameNotFoundException("User '" + username + "' not found");
            }
            return user;
        };
    }
}

解决方式:给?SecurityConfig 添加 @EnableWebSecurity?注解

文章来源:https://blog.csdn.net/jjf19891208/article/details/134915692
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。