编程语言
首页 > 编程语言> > 春季-将Java 8代码移植到Java 7

春季-将Java 8代码移植到Java 7

作者:互联网

是否可以将这段代码从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"));
        }
    }

}

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

标签:java-8,java-7,spring
来源: https://codeday.me/bug/20191121/2053065.html