为什么GlobalAuthenticationConfigurerAdapter.init()不可见?

时间:2016-04-21 00:31:36

标签: java spring spring-security spring-boot spring-security-oauth2

我正在尝试使用GlobalAuthenticationConfigurerAdapter将自定义UserDetailsServiceAuthserverApplication.javathis sample Spring Boot OAuth2 sample app根类挂钩。

Spring OAuth2 Developer Guide表示使用GobalAuthenticationConfigurerAdapter来挂钩UserDetailsService。并且I modified code from the example in this blog post

但是,当我尝试使用authserver启动修改后的mvn spring-boot:run应用时,会报告以下根错误:

nested exception is java.lang.NoSuchMethodException:  
demo.AuthserverApplication$WebSecurityConfiguration$$EnhancerBySpringCGLIB$$43683c11.<init>()  

来自mvn spring-boot:run的完整日志,包括错误消息can be read at a file sharing site by clicking on this link的完整堆栈跟踪。

然后我读了the documentation for GlobalAuthenticationConfigurerAdapter,并确认有init()方法。

需要对下面的代码(或the GitHub sample app中的其他代码)进行哪些具体更改才能解决此错误并成功将自定义UserDetailsService连接到示例GitHub app?

在您的机器上重现问题:

您可以在几分钟内通过重新创建我的步骤在任何计算机上重现此问题:

1。)键入以下命令将Spring Oauth2示例应用程序下载到您的计算机上:

git clone https://github.com/spring-guides/tut-spring-security-and-angular-js/  

2。)导航至tut-spring-security-and-angular-js/tree/master/oauth2/authserver。这是破解的应用程序。当您键入mvn spring-boot:run而不进行任何更改时,它应该正确启动。但是,然后进行以下更改以重新创建问题:

3.。)将以下内容添加到AuthserverApplication.java以连接自定义UserDetailsService

@Configuration
class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {

    @Autowired
    Users users;

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(users);
    }

}

4.。)将以下Users.java课程添加到the demo package of the authserver app,以便上面的 3 GlobalAuthenticationConfigurerAdapter指向真正的自定义UserDetailsService 。请注意,我取消了JDBC,因此在此代码示例中没有数据库依赖项:

package demo;

import java.util.List;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;

@Service
class Users implements UserDetailsManager {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String password;
        List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
        if (username.equals("Samwise")) {
            auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_HOBBIT");
            password = "TheShire";
        }
        else if (username.equals("Frodo")){
            auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_HOBBIT");
            password = "MyRing";
        }
        else{throw new UsernameNotFoundException("Username was not found. ");}
        return new org.springframework.security.core.userdetails.User(username, password, auth);
    }

    @Override
    public void createUser(UserDetails user) {// TODO Auto-generated method stub
    }

    @Override
    public void updateUser(UserDetails user) {// TODO Auto-generated method stub
    }

    @Override
    public void deleteUser(String username) {// TODO Auto-generated method stub
    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean userExists(String username) {
        // TODO Auto-generated method stub
        return false;
    }
}

修改后的AuthserverApplication.java的完整代码,包括新的GlobalAuthenticationConfigurerAdapter can be found at a file sharing site by clicking on this link

5.。)键入mvn spring-boot:run以尝试启动authserver应用。这将触发上述nested exception is java.lang.NoSuchMethodException: demo.AuthserverApplication$WebSecurityConfiguration$$EnhancerBySpringCGLIB$$43683c11.<init>()错误。

1 个答案:

答案 0 :(得分:2)

第一个错误仅仅是因为WebSecurityConfiguration不可见。您可以添加protected static关键字来修复此问题。