是否可以将这段代码从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/

10-09 05:30