Spring MVC 3.1中的基本身份验证

时间:2012-10-15 08:42:23

标签: rest spring-mvc spring-security basic-authentication restful-authentication

我正在尝试使用Spring MVC 3.1和REST API开发一个示例应用程序。

在进入应用程序之前,有一个登录表单可以对用户进行身份验证。我想使用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);
}

Credentials.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;
    }

}

我收到以下错误:

  

2012年10月16日下午12:56:04 org.apache.catalina.core.StandardWrapperValve   调用SEVERE:servlet Spring的Servlet.service()抛出异常   java.lang.NullPointerException at   org.springframework.web.client.RestTemplate $ AcceptHeaderRequestCallback.doWithRequest(RestTemplate.java:530)     在   org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:444)     在   org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409)     在   org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:207)

1 个答案:

答案 0 :(得分:2)

如果您已经使用spring MVC,那么一种方法是将其与Spring Security结合使用:http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity.html

因为它以各种方式支持基本身份验证: http://static.springsource.org/spring-security/site/docs/3.1.x/reference/ns-config.html#ns-form-and-basic

另请查看本文以了解如何与REST一起使用它: http://www.baeldung.com/2011/11/20/basic-and-digest-authentication-for-a-restful-service-with-spring-security-3-1/