CAS4.0 version restful API deployment and call (http version)

I have just started to work in the background recently, and there is a lot of knowledge that needs to be supplemented.

 

Several platforms need a unified login interface. Since the investor's system uses CAS for single sign-on, we can only deploy CAS. The architect said that there are many better frameworks than CAS, such as openAM and the like.

 

As a result, the official tutorials found on the Internet seem to be slightly unreliable. Many website tutorials are still version 3.n. The package of com.noelios.restlet is required for getticket, and Nima at least 4.0 does not need it. These packages, it is unscientific to configure them.

 

The environment is win10 64bit

 

To use restful API, you only need to do the following actions on the server side:

One: Download the source code (https://www.apereo.org/projects/cas/download-cas I chose cas server 4.0.0, and the source code is required: https://github.com/apereo/cas /archive/v4.0.0.zip), decompress the zip and configure cas-server-integration-restlet in cas-4.0.0\cas-server-webapp\pom.xml

 

<dependency>
    <groupId>org.jasig.cas</groupId>
    <artifactId>cas-server-integration-restlet</artifactId>
    <version>${project.version}</version>
     <scope>compile</scope>
</dependency>

or also
<!-- https://mvnrepository.com/artifact/org.jasig.cas/cas-server-integration-restlet -->
<dependency>
    <groupId>org.jasig.cas</groupId>
    <artifactId>cas-server-integration-restlet</artifactId>
    <version>4.0.0</version>
     <type>jar</typee>
</dependency>

 

 

Two: compile

I was very stupid and imported the source code into Eclipse. I wanted Eclipse to compile it myself and have a good look at the code. As a result, Eclipse has been verifying, and the environment has been hung up. I don't know how many times. Then my colleague said that it would be good to use maven's own command to execute it directly. When compiling, you only need to compile the webapp.

Open cmd and execute mvn clean package in the cas-4.0.0\cas-server-webapp directory. The generated file is in cas-4.0.0\cas-server-webapp\target .

 

Three: Deploy the compiled cas.war to tomcat, this should be all, so I won't go into details. The point is, configure webapps\cas\WEB-INF\web.xml. Here servlet-name is restlet, then the corresponding configuration already exists in WEB-INF\ restlet-servlet.xml, the two names are corresponding, if servlet-name is api, the corresponding configuration is api-servlet.xml

 

    <servlet>
	 <servlet-name>restlet</servlet-name>
        <servlet-class>org.restlet.ext.spring.RestletFrameworkServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>restlet</servlet-name>
        <url-pattern>/v1/*</url-pattern>
    </servlet-mapping>

 

 

Restart, I did not configure https, directly adjust http://localhost:8090/cas/v1/tickets, if the result is as shown below, the deployment of restful is successful



You can request the result with postman, as shown below

  

 

Four: test code

The code refers to http://www.cnblogs.com/sunshineatnoon/p/4119565.html, but she uses https, I use http, and the forced conversion classes are different, everyone takes what they need.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class CasTest {

	public static void main(String... args) throws Exception {
		String username = "casuser";
		String password = "Mellon";
		validateFromCAS(username, password);
	}

	public static boolean validateFromCAS(String username, String password) throws Exception {

		String url = "http://localhost:8090/cas/v1/tickets";
		try {
			HttpURLConnection hsu = (HttpURLConnection) openConn(url);
			String s = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
			s += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

			System.out.println(s);
			OutputStreamWriter out = new OutputStreamWriter(hsu.getOutputStream());
			BufferedWriter bwr = new BufferedWriter(out);
			bwr.write(s);
			bwr.flush();
			bwr.close();
			out.close();

			String tgt = hsu.getHeaderField("location");
			System.out.println(hsu.getResponseCode());
			if (tgt != null && hsu.getResponseCode() == 201) {
				System.out.println(tgt);

				System.out.println("Tgt is : " + tgt.substring(tgt.lastIndexOf("/") + 1));
				tgt = tgt.substring(tgt.lastIndexOf("/") + 1);
				bwr.close();
				closeConn(hsu);

				String serviceURL = "http://localhost:8080/CasClient";
				String encodedServiceURL = URLEncoder.encode("service", "utf-8") + "="
						+ URLEncoder.encode(serviceURL, "utf-8");
				System.out.println("Service url is : " + encodedServiceURL);

				String myURL = url + "/" + tgt;
				System.out.println(myURL);
				hsu = (HttpURLConnection) openConn(myURL);
				out = new OutputStreamWriter(hsu.getOutputStream());
				bwr = new BufferedWriter(out);
				bwr.write(encodedServiceURL);
				bwr.flush();
				bwr.close();
				out.close();

				System.out.println("Response code is:  " + hsu.getResponseCode());

				BufferedReader isr = new BufferedReader(new InputStreamReader(hsu.getInputStream()));
				String line;
				System.out.println(hsu.getResponseCode());
				while ((line = isr.readLine()) != null) {
					System.out.println(line);
				}
				isr.close();
				hsu.disconnect();
				return true;

			} else {
				return false;
			}

		} catch (MalformedURLException mue) {
			mue.printStackTrace();
			throw mue;

		} catch (IOException ioe) {
			yes.printStackTrace ();
			throw ioe;
		}

	}

	static URLConnection openConn(String urlk) throws MalformedURLException, IOException {

		URL url = new URL(urlk);
		HttpURLConnection hsu = (HttpURLConnection) url.openConnection();
		hsu.setDoInput(true);
		hsu.setDoOutput(true);
		hsu.setRequestMethod("POST");
		return hsu;

	}

	static void closeConn(HttpURLConnection c) {
		c.disconnect();
	}

}

 Username, password, sub-service, etc., fill in it yourself.

 

No problem with the above :)

 

If you think the article is good, please encourage it :D




 
 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326925383&siteId=291194637