java 模仿客户端向spring security base认证发请求

使用jersey框架的客户端部分。模拟调用。代码如下 。
package jersey.authbase;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
/**
 * 
<dependencies>
     <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
     </dependency>
     <dependency> 
	    <groupId>org.glassfish.jersey.core</groupId> 
	    <artifactId>jersey-client</artifactId> 
	    <version>2.13</version> 
	</dependency> 
  </dependencies>
  jersey 客户端介绍
  https://jersey.java.net/documentation/latest/client.html
 *
 */
public class DemoClient {
	
	public static void main(String[] args) {
		new DemoClient().metho2Test();
		new DemoClient().metho4Test();
		new DemoClient().metho3Test();
	}
	public void metho1Test(){
	 
		final Client  client =  ClientBuilder.newClient();
		client.register(DemoClient.getHttpAuthenticationFeatureWithNonPreemptive("admin", "1126"));
		 String userName = "admin";  
		 String appUrl = "http://127.0.0.1:8080";  
         String url = String.format("%s/auth/rest/users/user/%s", appUrl,  userName);  
		 
		 final Response response = client.target(url).request().get();
		 System.out.println(response.getStatus()); 
		 System.out.println(response.readEntity(String.class));
	}
	public void metho2Test(){
		String username = "admin";
		String password = "1126";
		final Client  client =  ClientBuilder.newClient();
		 client.register(DemoClient.getHttpAuthenticationFeatureWithNonPreemptive(username, password));
		 
		 String appUrl = "http://127.0.0.1:8080";  
         String url = String.format("%s/auth/rest/users/getCurrentUser", appUrl); 
         final Response response = client.target(url).request().get();
		 System.out.println(response.getStatus()); 
		 System.out.println(response.readEntity(String.class));
	}
	public void metho3Test(){
		String username = "admin";
		String password = "1126";
		final Client  client =  ClientBuilder.newClient();
		 client.register(DemoClient.getHttpAuthenticationFeatureWithNonPreemptive(username, password));
		 
		 String appUrl = "http://127.0.0.1:8080";  
         String url = String.format("%s/auth/rest/userGroups/children", appUrl); 
         final Response response = client.target(url).request().get();
		 System.out.println(response.getStatus()); 
		 System.out.println(response.readEntity(String.class));
	}
	
	public void metho4Test(){
		String username = "admin";
		String password = "1126";
		final Client  client =  ClientBuilder.newClient();
		 client.register(DemoClient.getHttpAuthenticationFeatureWithNonPreemptive(username, password));
		 
		 String appUrl = "http://127.0.0.1:8080";  
         String url = String.format("%s/auth/rest/users/current", appUrl); 
         
         final Response response = client.target(url).request().get();
		 System.out.println(response.getStatus()); 
		 System.out.println(response.readEntity(String.class));
	}
	
	/**
	 * 每次调用都要更换用户名\密码
	 */
	public void test(){
		final Client  client =  ClientBuilder.newClient();
		final Response response = client.target("http://localhost:8080/rest/homer/contact").request()
			    .property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAME, "homer")
			    .property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD, "p1swd745").get();

	}
	
	public static  HttpAuthenticationFeature getHttpAuthenticationFeatureWithNonPreemptive(String username,String password){
		HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder() 
			    .nonPreemptive().credentials(username, password).build();
		return feature;
		
	}
	public HttpAuthenticationFeature getHttpAuthenticationFeatureWithNonPreemptive(){
		HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder().build();
		return feature;
	}

}

 

你可能感兴趣的:(Spring Security)