“L'authentification est nécessaire pour accéder à cette ressource” sur le printemps oauth2 serveur d'autorisation sur /oauth/demande de jeton

Je suis en train de créer une page qui va contenir les données de l'utilisateur qui peut être accessible via un client web. Qui m'a amené à oauth2 (code d'autorisation) et de Printemps. Actuellement je travaille sur une sorte de preuve de concept pour ce baser mon code sur https://github.com/dynamind/spring-boot-security-oauth2-minimal/ et ce flux de travail https://techannotation.files.wordpress.com/2015/06/oauth2-0-authorization-code.png

J'ai déjà un serveur de ressources qui utilise la page de connexion pour l'authentification des utilisateurs et leur donne la possibilité d'une approbation pour le partage de leurs données. Ensuite, il redirige l'utilisateur vers une page pour le client.

@SpringBootApplication
public class AuthorizationServerApplication extends SpringBootServletInitializer {
private static final Logger log = LoggerFactory.getLogger(AuthorizationServerApplication.class);
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(AuthorizationServerApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AuthorizationServerApplication.class);
}
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
protected static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
@Autowired //<-- This is crucial otherwise Spring Boot creates its own
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
log.info("Defining inMemoryAuthentication (2 users)");
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER")
.and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.and()
.httpBasic().disable().anonymous().disable().authorizeRequests().anyRequest().authenticated();
}
}
@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Value("${config.oauth2.privateKey}")
private String privateKey;
@Value("${config.oauth2.publicKey}")
private String publicKey;
@Autowired
private AuthenticationManager authenticationManager;
@Bean
public JwtAccessTokenConverter tokenEnhancer() {
log.info("Initializing JWT with public key:\n" + publicKey);
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(privateKey);
converter.setVerifierKey(publicKey);
return converter;
}
@Bean
public JwtTokenStore tokenStore() {
return new JwtTokenStore(tokenEnhancer());
}
/**
* Defines the security constraints on the token endpoints
* /oauth/token_key and /oauth/check_token Client credentials are
* required to access the endpoints
*
* @param oauthServer
* @throws Exception
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("isAnonymous() || hasRole('ROLE_TRUSTED_CLIENT')") //permitAll()
.checkTokenAccess("hasRole('TRUSTED_CLIENT')"); //isAuthenticated()
}
/**
* Defines the authorization and token endpoints and the token services
*
* @param endpoints
* @throws Exception
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
//Which authenticationManager should be used for the
//password grant
//If not provided, ResourceOwnerPasswordTokenGranter is not
//configured
.authenticationManager(authenticationManager)
//Use JwtTokenStore and our jwtAccessTokenConverter
.tokenStore(tokenStore()).accessTokenConverter(tokenEnhancer());
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
//Public client where client secret is vulnerable (e.g.
//mobile apps, browsers)
.withClient("clientname") //No secret!
.authorizedGrantTypes("authorization_code").scopes("read")
.redirectUris("http://localhost:8080/client")
;
}
}
}

Actuellement, je fais face à un la plus simple possible client page web. J'ai créé une page avec un lien vers le serveur d'autorisation (localhost:8081/oauth/autoriser...). L'utilisateur clique sur elle et il est redirigé vers le serveur d'autorisation, les journaux de là, approuve le partage de ses/le sien données et est ensuite redirigé vers le site du client (localhost:8080/client, mais avec le code donné par le serveur d'autorisation), mais maintenant, avec l'option supplémentaire de cliquer sur un lien de plus (localhost:8081/oauth/jeton...) qui a retourné le code. Tout cela fonctionne, mais lorsque l'utilisateur clique sur ce deuxième lien et il est redirigé vers le serveur d'autorisation, ce auth serveur répond avec "Plein d'authentification est requis pour accéder à cette ressource".

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>client page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'oooo text oooo'" />
if you want it
<a
th:href="@{http://localhost:8081/oauth/authorize(response_type='code',client_id='client',key='value',scope='read',redirect_uri='http://localhost:8080/client')}">clickit</a>
<a th:if="${param.code != null}"
th:href="@{http://localhost:8081/oauth/token(grant_type='authorization_code',code=${param.code[0]},redirect_uri='http://localhost:8080/client')}">
approve
</a>
<div th:if=" ${param.code !=null}
"
th:text="${'requestParam: ' + param.code[0]}"></div>
</body>
</html>

Avez-vous des idées pour résoudre ce problème?

InformationsquelleAutor szeldon | 2016-06-07