gpt4 book ai didi

spring - 将 Java 8 代码移植到 Java 7

转载 作者:行者123 更新时间:2023-12-01 23:55:04 32 4
gpt4 key购买 nike

是否可以将这段代码从 Java 8 移植到 Java 7?

    protected UserDetailsService userDetailsService() {
return (username) -> {
User u = crmService.findUserByUsername(username);
return new org.springframework.security.core.userdetails.User(
u.getUsername(), u.getPassword(), u.isEnabled(),
u.isEnabled(), u.isEnabled(), u.isEnabled(),
AuthorityUtils.createAuthorityList("USER", "write"));
};
}

最佳答案

你可以这样做:

protected UserDetailsService userDetailsService() {
return new UserDetailsService() {
@Override
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User u = crmService.findUserByUsername(username);
return new org.springframework.security.core.userdetails.User(
u.getUsername(), u.getPassword(), u.isEnabled(),
u.isEnabled(), u.isEnabled(), u.isEnabled(),
AuthorityUtils.createAuthorityList("USER", "write"));
}
}

}

但是你绝对应该考虑将它提取到普通类并在那里返回新对象

关于spring - 将 Java 8 代码移植到 Java 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24266736/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com