2011-10-25 2 views
3

je suis en train de mettre en œuvre oauth2 nom d'utilisateur flux de mot de passe sur la sécurité du printemps mais je ne peux pas trouver toute la documentation et des exemples de code je vais plus OAuth2 sparklr et TONR insode échantillons comment puis-je mettre en œuvre oauth2 2 pattes comment puis-je désactiver formulaire de connexion Comment créer OAuth 2 nom d'utilisateur flux de mot de passe sur la sécurité du printemps

<form-login authentication-failure-url="/login.jsp" default-target-url="/index.jsp" login-page="/login.jsp" 
     login-processing-url="/login.do" /> 
    <logout logout-success-url="/index.jsp" logout-url="/logout.do" /> 
    <anonymous /> 
    <custom-filter ref="oauth2ProviderFilter" after="EXCEPTION_TRANSLATION_FILTER" /> 
</http> 
+0

Il y a même un exemple à débordement de la pile: http://stackoverflow.com/questions/5431359/trying-to-protect-resources-with-oauth-in-spring-mvc/6085811#6085811 – Ralph

+0

Cette peut être utile: https://stackoverflow.com/a/48586779/1279002 – theINtoy

Répondre

8

Le sparklr par défaut prend également en charge le nom d'utilisateur et le mot de passe flux, c'est facile, vous devez écrire uniquement le client client est montré ci-dessous: j'ai réussi à la fin;

public class App { 

private static RestTemplate client=getRestTemplate(); 

    private static int DEFAULT_PORT = 8080; 

private static String DEFAULT_HOST = "localhost"; 

private static int port=DEFAULT_PORT; 

private static String hostName = DEFAULT_HOST; 


public static void main(String[] args) throws IOException { 
    try { 
     testHappyDayWithForm(); 
    } catch (Exception ex) { 
     Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 


public static void testHappyDayWithForm() throws Exception { 

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>(); 
    formData.add("grant_type", "password"); 
    formData.add("client_id", "my-trusted-client"); 
    formData.add("scope", "read"); 
    formData.add("username", "muhammed"); 
    formData.add("password", "1234"); 

    ResponseEntity<String> response = postForString("/sparklr/oauth/token", formData); 
    System.out.println(response.getStatusCode()); 
    System.out.println(response.getHeaders().getFirst("Cache-Control")); 

    DefaultOAuth2SerializationService serializationService = new DefaultOAuth2SerializationService(); 
    OAuth2AccessToken accessToken = serializationService.deserializeJsonAccessToken(new ByteArrayInputStream(
      response.getBody().getBytes())); 

    // now try and use the token to access a protected resource. 

    // first make sure the resource is actually protected. 
    //assertNotSame(HttpStatus.OK, serverRunning.getStatusCode("/sparklr/photos?format=json")); 

    // now make sure an authorized request is valid. 
    HttpHeaders headers = new HttpHeaders(); 
    headers.set("Authorization", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, accessToken.getValue())); 
    //assertEquals(HttpStatus.OK, serverRunning.getStatusCode("/sparklr/photos?format=json", headers)); 
} 

    public static ResponseEntity<String> postForString(String path, MultiValueMap<String, String> formData) { 
    HttpHeaders headers = new HttpHeaders(); 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED)); 
      System.out.println(getUrl(path)); 
    return client.exchange(getUrl(path), HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(formData, 
      headers), String.class); 
} 
    public static String getUrl(String path) { 
    if (!path.startsWith("/")) { 
     path = "/" + path; 
    } 
    return "http://" + hostName + ":" + port + path; 
} 

    public static RestTemplate getRestTemplate() { 
    RestTemplate client = new RestTemplate(); 
    CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory() { 
     @Override 
     protected void postProcessCommonsHttpMethod(HttpMethodBase httpMethod) { 
      httpMethod.setFollowRedirects(false); 
      // We don't want stateful conversations for this test 
      httpMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); 
     } 
    }; 
    client.setRequestFactory(requestFactory); 
    client.setErrorHandler(new ResponseErrorHandler() { 
     // Pass errors through in response entity for status code analysis 
     public boolean hasError(ClientHttpResponse response) throws IOException { 
      return false; 
     } 

     public void handleError(ClientHttpResponse response) throws IOException { 
     } 
    }); 
    return client; 
} 
Questions connexes