2

我正在嘗試使用Spring MVC 3.1和REST API開發示例應用程序。Spring MVC中的基本身份驗證3.1

在進入應用程序之前,有一個登錄表單來驗證用戶身份。我想使用REST API進行基本身份驗證,使用用戶在登錄表單中提供的憑據(用戶名和密碼)。

我的問題是我不知道如何使用REST API進行基本身份驗證。

我搜查了很多,但沒有得到任何正確的教程,我可以從哪裏解決我的錯誤。

請有人指導我或告訴我任何好文章?

現在我正在接受以下教程的幫助 http://www.jpalace.org/docs/technotes/spring/rest-security.html但獲取空指針異常。

RestClient.java

import org.springframework.web.client.RestTemplate; 


public class RestClient { 

    RestTemplate restTemplate = new RestTemplate(); 
    static final String URL = "http://www.assembla.com/spaces/my_spaces"; 
    static final String username = "aaaaa"; 
    static final String password = "aaaaa"; 

    public RestClient() { 

     BasicSecureSimpleClientHttpRequestFactory requestFactory = new BasicSecureSimpleClientHttpRequestFactory(); 
     requestFactory.setCredentialsProvider(new SimpleCredentialsProvider(
       new Credentials(username, password))); 
     restTemplate.setRequestFactory(requestFactory); 
    } 

    public void exec() { 
     System.out.println("restTemplate :::::::::::::::::: " + restTemplate); 
     String response = restTemplate.getForObject(URL, String.class); 
     System.out.println("Response:::::::::::::: " + response); 
    } 

} 

BasicSecureSimpleClientHttpRequestFactory.java

import java.net.HttpURLConnection; 
    import sun.misc.BASE64Encoder; 


    public class BasicSecureSimpleClientHttpRequestFactory extends 
      SecureSimpleClientHttpRequestFactory { 

     public BasicSecureSimpleClientHttpRequestFactory() { 
     } 

     protected void prepareSecureConnection(HttpURLConnection connection) { 
      if (credentialsProvider == null) { 
       return; 
      } 
      Credentials credentials = credentialsProvider.getCredentials(null); 
      String token = credentials.getUsername() + ":" 
        + credentials.getPassword(); 
      BASE64Encoder enc = new sun.misc.BASE64Encoder(); 
      String encodedAuthorization = enc.encode(token.getBytes()); 
      connection.setRequestProperty("Authorization", "Basic " 
        + encodedAuthorization); 
     } 
    } 

SecureSimpleClientHttpRequestFactory的.java

import java.io.IOException; 
import java.net.HttpURLConnection; 
import java.net.URI; 

import org.springframework.http.HttpMethod; 
import org.springframework.http.client.ClientHttpRequest; 
import org.springframework.http.client.SimpleClientHttpRequestFactory; 

abstract public class SecureSimpleClientHttpRequestFactory extends 
     SimpleClientHttpRequestFactory { 

    protected CredentialsProvider credentialsProvider; 

    public SecureSimpleClientHttpRequestFactory() { 
    } 

    public CredentialsProvider getCredentialsProvider() { 
     return credentialsProvider; 
    } 

    public void setCredentialsProvider(CredentialsProvider credentialsProvider) { 
     this.credentialsProvider = credentialsProvider; 
    } 

    @Override 
    public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) 
      throws IOException { 
     HttpURLConnection connection = openConnection(uri.toURL(), null); 
     prepareConnection(connection, httpMethod.name()); 
     prepareSecureConnection(connection); 
     return new SecureSimpleClientHttpRequest(connection); 
    } 

    abstract protected void prepareSecureConnection(HttpURLConnection connection); 

} 

SecureSimpleClientHttpRequest的.java

import java.io.IOException; 
import java.io.OutputStream; 
import java.net.HttpURLConnection; 
import java.net.URI; 
import org.springframework.http.HttpHeaders; 
import org.springframework.http.HttpMethod; 
import org.springframework.http.client.ClientHttpRequest; 
import org.springframework.http.client.ClientHttpResponse; 
public class SecureSimpleClientHttpRequest implements ClientHttpRequest { 
    public SecureSimpleClientHttpRequest(HttpURLConnection connection) { 
     // TODO Auto-generated constructor stub 
    } 
    @Override 
    public HttpMethod getMethod() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public URI getURI() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public HttpHeaders getHeaders() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public OutputStream getBody() throws IOException { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public ClientHttpResponse execute() throws IOException { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

SimpleCredentialsProvider的.java

public class SimpleCredentialsProvider implements CredentialsProvider { 

    private Credentials credentials; 

    public SimpleCredentialsProvider(Credentials credentials) { 
     super(); 
     this.credentials = credentials; 
    } 

    @Override 
    public Credentials getCredentials(String str) { 

     return credentials; 
    } 

} 

CredentialsProvider的.java

public interface CredentialsProvider { 

    public Credentials getCredentials(String str); 
} 

Ç redentials.java

public class Credentials { 

    private String username; 
    private String password; 

    public Credentials(String username, String password) { 
     super(); 
     this.username = username; 
     this.password = password; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

} 

我收到以下錯誤:

Oct 16, 2012 12:56:04 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet spring threw exception java.lang.NullPointerException at org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback.doWithRequest(RestTemplate.java:530) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:444) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:207)

回答

2
+0

我想通過以下URL「http://www.jpalace.org/docs/technotes/spring/rest- security.html「但沒有成功。 –