Я реализую oauth2 sso, используя @EnableOAuth2Sso
в spring-boot:1.3.0.M1.
Я хочу использовать информацию о пользователе с моего сервера ресурсов (http://oauth2_resource_server/me).
Поэтому я пытаюсь реализовать свои собственные ResourceServerTokenServices, ссылаясь на UserInfoTokenServices
@Configuration @EnableWebSecurity @EnableOAuth2Sso public class OAuth2Config {
@Autowired ResourceServerProperties sso;
@Bean public ResourceServerTokenServices userInfoTokenServices() {
return new MyTokenService(sso.getUserInfoUri(), sso.getClientId());
}
}
public class MyTokenService implements ResourceServerTokenServices {
@Override public OAuth2Authentication loadAuthentication(String accessToken)
throws AuthenticationException, InvalidTokenException {
try {
MyUser user = getFromNetworkAndSaveDB(accessToken);
return extractAuthentication(user);
} catch (Exception e) {
throw new InvalidTokenException(e.getMessage(), e);
}
}
/**
* @param user retrieved and serialize from http://oauth2_resource_server/me
*/
private OAuth2Authentication extractAuthentication(MyUser user) {
List<GrantedAuthority> authorities =
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
OAuth2Request request =
new OAuth2Request(null, this.clientId, null, true, null, null, null, null, null);
UsernamePasswordAuthenticationToken token =
new UsernamePasswordAuthenticationToken(user.getId(), "N/A", authorities);
token.setDetails(user);
return new OAuth2Authentication(request, token);
}
}
приведенный выше код создает объект OAuth2Authentication
, и он работает.
Я хочу использовать объект MyUser
при входе в систему, но как мне это сделать? (я не знаю, что такое общий способ)