为什么 GlobalAuthenticationConfigurerAdapter.init() 不可见?
Why is GlobalAuthenticationConfigurerAdapter.init() invisible?
我正在尝试使用 GlobalAuthenticationConfigurerAdapter
将自定义 UserDetailsService
连接到 AuthserverApplication.java
root class of this sample Spring Boot OAuth2 sample app。
Spring OAuth2 Developer Guide says to use a GobalAuthenticationConfigurerAdapter
to hook up the UserDetailsService
. And I modified code from the example in this blog post。
但是,当我尝试使用 mvn spring-boot:run
启动修改后的 authserver
应用程序时,报告了以下根错误:
nested exception is java.lang.NoSuchMethodException:
demo.AuthserverApplication$WebSecurityConfiguration$$EnhancerBySpringCGLIB$683c11.<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
class添加到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$683c11.<init>()
错误。
第一个错误只是因为您的 WebSecurityConfiguration
不可见。您可以添加 protected static
关键字来解决这个问题。
我正在尝试使用 GlobalAuthenticationConfigurerAdapter
将自定义 UserDetailsService
连接到 AuthserverApplication.java
root class of this sample Spring Boot OAuth2 sample app。
Spring OAuth2 Developer Guide says to use a GobalAuthenticationConfigurerAdapter
to hook up the UserDetailsService
. And I modified code from the example in this blog post。
但是,当我尝试使用 mvn spring-boot:run
启动修改后的 authserver
应用程序时,报告了以下根错误:
nested exception is java.lang.NoSuchMethodException:
demo.AuthserverApplication$WebSecurityConfiguration$$EnhancerBySpringCGLIB$683c11.<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
class添加到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$683c11.<init>()
错误。
第一个错误只是因为您的 WebSecurityConfiguration
不可见。您可以添加 protected static
关键字来解决这个问题。