【问题标题】:Spring boot custom user with authentication具有身份验证的 Spring Boot 自定义用户
【发布时间】:2019-07-14 17:59:47
【问题描述】:

我正在尝试使用我想要的属性进行自定义用户并使用身份验证,CustomUser 扩展了 spring User,用户由实现 UsersDetailsS​​ervice 的 CustomProvider 返回

@Service
@Qualifier("UserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    @Transactional
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        User user=userRepository.findByEmail(email);
        return new CustomUser(user.getName(),user.getPassword(),buildUserAuthority(user.getRoles()));
    }

    private List<GrantedAuthority> buildUserAuthority(Set<Role> userRoles) {

        Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

        // add user's authorities
        for (Role userRole : userRoles) {
            setAuths.add(new SimpleGrantedAuthority(userRole.getRole()));
        }

        List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);

        return Result;
    }


    @Getter
@Setter
public class CustomUser extends User {

    public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
    }

    public String firstName;
    public String lastName;
}


@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
 @Autowired
    @Qualifier("UserDetailsService")
    private UserDetailsService customUserDetailsService;


    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
     /*   auth.
                jdbcAuthentication()
                .usersByUsernameQuery(usersQuery)
                .authoritiesByUsernameQuery(rolesQuery)
                .dataSource(dataSource)
                .passwordEncoder(bCryptPasswordEncoder);
*/
        auth.userDetailsService(customUserDetailsService);
    }
}

我有 2 个问题:

1- 我已经评论了 auth.jdbcAuthentication,因为我无法让身份验证和 customProvider 一起工作,我该如何使用 customuser 的数据库身份验证?

2-如果我评论 jdbcAuthentication customuser 可以工作,但是当我获得委托人时,密码为空: authentication.getPrincipal().getPassword()


更新:

我已经通过 eraseCredentials(false) 解决了 2,但仍然无法同时执行这两个操作(使用自定义用户进行身份验证)

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    旧答案:

    我已经解决了:

    auth.eraseCredentials(false).userDetailsService(customUserDetailsService).and().jdbcAuthentication()
                    .usersByUsernameQuery(usersQuery)
                    .authoritiesByUsernameQuery(rolesQuery)
                    .dataSource(dataSource)
                    .passwordEncoder(bCryptPasswordEncoder);
    

    更新:

    我发现不需要使用2种身份验证,因为spring会与返回的用户验证密码。

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2017-03-17
      • 2021-04-21
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 2017-05-10
      • 2022-11-04
      • 1970-01-01
      相关资源
      最近更新 更多