【问题标题】:Spring OAuth2 Custom Authentication Manager ClassCastExceptionSpring OAuth2 自定义身份验证管理器 ClassCastException
【发布时间】:2018-10-25 13:21:54
【问题描述】:

我有一个大问题,不知道如何解决它...... 我需要在我的 Spring Boot 应用程序中使用 customAuthenticationManager 进行第三方登录,但是当我声明自定义身份验证器时,我得到:

处理错误:

ClassCastException, java.lang.String cannot be cast to com.nexus.demooauth.models.User

如果我使用默认身份验证管理器(spring boot 附带的)一切正常。

这里是 Websecurity.java

@Configuration 
public class WebSecurity extends WebSecurityConfigurerAdapter {

@Bean
public AuthenticationManager customAuthenticationManager() throws Exception {
    return new CustomAuthenticationManager();
}

AuthorizationServerConfig.java

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {


@Autowired
UserDetailsService customUserDetailsService;

@Autowired
DataSource dataSource;

@Autowired
private AuthenticationManager authenticationManager;        

@Bean
public PasswordEncoder passwordEncoder() {
    return new Plainencoder();
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer configurer) throws Exception {
    //configurer.userDetailsService(customUserDetailsService);
    configurer.authenticationManager(authenticationManager);
    configurer.tokenEnhancer(tokenEnhancer());
}

@Bean
public TokenEnhancer tokenEnhancer() {
    return new CustomTokenEnhancer();
}


@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory().withClient("gigy").secret("secret").accessTokenValiditySeconds(8400)
    .scopes("read", "write").authorizedGrantTypes("password", "refresh_token");
}

CustomAuthenticationManager.java

 @Service
    public class CustomAuthenticationManager implements AuthenticationManager{

private final Logger logger = LoggerFactory.getLogger(CustomAuthenticationManager.class);

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    String username = authentication.getName();
    String pw       = authentication.getCredentials().toString();
    logger.info("was here" + username.toString() + " , " + pw.toString());

    return new UsernamePasswordAuthenticationToken(username, pw, authentication.getAuthorities());

}

它实际上在记录器中打印

2018-05-15 17:58:34.453  INFO 7212 --- [nio-8089-exec-1] c.n.d.s.CustomAuthenticationManager      : was heretest , test

在调试时,在某些混淆类中返回新的 UsernamePasswordAuthenticationToken 时会中断。

【问题讨论】:

  • 您调用的这个端点正在尝试获取当前用户?
  • 也使用最新版本的 spring-oauth 和 spring-boot
  • 不,它正在尝试获取 access_token /oauth/token。

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


【解决方案1】:

居然找到了答案。 问题出在导致此无效转换的 CustomTokenEnhancer 中。

【讨论】:

    猜你喜欢
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 2013-02-24
    • 2021-04-21
    • 2014-06-16
    • 2017-05-25
    相关资源
    最近更新 更多