简体   繁体   English

Spring OAuth2自定义身份验证管理器ClassCastException

[英]Spring OAuth2 Custom Authentication Manager ClassCastException

i've a big problem and no idea how to solve it... I need to use customAuthenticationManager for third party log-in in my spring boot application, but when i declare custom authenticator i get : 我有一个大问题,不知道如何解决...我需要在春季启动应用程序中使用customAuthenticationManager进行第三方登录,但是当我声明自定义身份验证器时,我会得到:

Handling error: 处理错误:

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

If i use default authentication manager (the one that comes with spring boot) everything works fine. 如果我使用默认的身份验证管理器(Spring Boot附带的那个),则一切正常。

Here is Websecurity.java 这是Websecurity.java

@Configuration 
public class WebSecurity extends WebSecurityConfigurerAdapter {

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

AuthorizationServerConfig.java 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 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());

}

It actually prints in logger 它实际上在记录器中打印

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

When debugging it breaks when returning new UsernamePasswordAuthenticationToken in some obfuscated class. 调试时,在某些混淆的类中返回新的UsernamePasswordAuthenticationToken时,它会中断。

Actually found the anserw. 居然找到了anserw。 The problem was in CustomTokenEnhancer that was making this invalid conversion. 问题出在进行这种无效转换的CustomTokenEnhancer中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM