1.在build.scala中添加依赖项
val appDependencies = Seq(
"org.pac4j" % "play-pac4j_java" % "1.1.0-SNAPSHOT",
"org.pac4j" % "pac4j-cas" % "1.4.0-SNAPSHOT"
)
配置所要添加的依赖项的url
val main = PlayProject(appName, appVersion, appDependencies).settings(
resolvers += "Sonatype snapshots repository" at "http://oss.sonatype.org/content/repositories/snapshots/"
)
2.修改conf/application.conf文件
# Global object class
# ~~~~~
# Define the Global object class for this application.
# Default to Global in the root package.
global=controllers.Global
同时删掉文件末尾填的这句话
logger.application=DEBUG
再添加上以下这两句
logger.org.pac4j.play=DEBUG
logger.controllers=DEBUG
3.修改routes文件,添加以下这几句话
GET /cas/index.html controllers.Application.casIndex()
GET /callback org.pac4j.play.CallbackController.callback()
POST /callback org.pac4j.play.CallbackController.callback()
GET /casProxyCallback org.pac4j.play.CallbackController.callback()
POST /casProxyCallback org.pac4j.play.CallbackController.callback()
GET /logout org.pac4j.play.CallbackController.logoutAndRedirect()
4.在controllers文件夹底下,新建Global.java文件,内容如下:
package controllers;
import org.pac4j.cas.client.CasClient;
import org.pac4j.core.client.Clients;
import org.pac4j.play.Config;
import play.Application;
import play.GlobalSettings;
//import play.mvc.Http.RequestHeader;
public class Global extends GlobalSettings {
/*@Override
public Result onError(final RequestHeader requestHeader, final Throwable t) {
return play.mvc.Controller.internalServerError(views.html.error500.render());
}*/
@Override
public void onStart(final Application app) {
// Config.setErrorPage401(views.html.error401.render().toString());
// Config.setErrorPage403(views.html.error403.render().toString());
// CAS
final CasClient casClient = new CasClient();
// casClient.setLogoutHandler(new PlayLogoutHandler());
// casClient.setCasProtocol(CasProtocol.SAML);
// casClient.setGateway(true);
/*final CasProxyReceptor casProxyReceptor = new CasProxyReceptor();
casProxyReceptor.setCallbackUrl("http://localhost:9000/casProxyCallback");
casClient.setCasProxyReceptor(casProxyReceptor);*/
//注意,这里的url要改为你自己cas服务器所在的url,否则会报错
casClient.setCasLoginUrl("https://qinshang-PC:8443/cas-sql/login");
final Clients clients = new Clients("http://localhost:9000/callback",casClient); // , casProxyReceptor);
Config.setClients(clients);
// for test purposes : profile timeout = 60 seconds
// Config.setProfileTimeout(60);
}
}
5.删掉系统产生的Application.java,重新建一个这个文件,内容如下:
package controllers;
import org.pac4j.core.exception.TechnicalException;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.play.Config;
import org.pac4j.play.java.JavaController;
import org.pac4j.play.java.RequiresAuthentication;
import play.mvc.Result;
public class Application extends JavaController {
public static Result index() throws TechnicalException {
// profile (maybe null if not authenticated)
final CommonProfile profile = getUserProfile();
final String urlCas = getRedirectionUrl("CasClient", "/?4");
System.out.println("urlCas......................:"+urlCas+'\n');
//重定向到/cas/index.html这个网址
return redirect(routes.Application.casIndex());
}
//执行casIndex()方法时,进行cas认证
@RequiresAuthentication(clientName = "CasClient")
public static Result casIndex() {
/*final CommonProfile profile = getUserProfile();
final String service = "http://localhost:8080/proxiedService";
String proxyTicket = null;
if (profile instanceof CasProxyProfile) {
final CasProxyProfile proxyProfile = (CasProxyProfile) profile;
proxyTicket = proxyProfile.getProxyTicketFor(service);
}
return ok(views.html.casProtectedIndex.render(profile, service, proxyTicket));*/
// return protectedIndex();
return TODO;
}
}
注意事项:如果直接从这上面复制代码的话,编码为UTF-8,如果你的play程序编码不是GBK的话,运行的时候会出现错误,这时候建议你先将代码复制粘贴到记事本中,然后再把记事本的内容复制到新建的文件中去。
测试:以上步骤一次执行完后,打开cmd,进入到程序所在目录,执行play run,然后打开浏览器,输入http://localhost:9000/,会跳转到cas的登入界面,输入用户名和密码,认证成功后,会重定向到http:localhost:9000/cas/index.html这个网址,显示的内容为TODO。