实现登录方式的改变
一、新建一个hook工程
二、新建程序
1.httpClient 工具类import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.net.ssl.HttpsURLConnection;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HttpClient {
private static final Log LOG = LogFactory.getLog(HttpClient.class);
private static final int TIMEOUT = 10 * 1000;
public static String get(String urlString, String xml) {
HttpURLConnection httpConn = null;
OutputStream out = null;
String returnXml = "";
if (LOG.isDebugEnabled()) {
LOG.debug("request xml==>>" + xml);
}
try {
httpConn = (HttpURLConnection) new URL(urlString).openConnection();
httpConn.setRequestProperty("Content-Type",
"application/json");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setConnectTimeout(TIMEOUT);
httpConn.setReadTimeout(TIMEOUT);
httpConn.connect();
out = httpConn.getOutputStream();
httpConn.getOutputStream().write(xml.getBytes());
out.flush();
out.close();
int code = httpConn.getResponseCode();
String tempString = null;
StringBuffer sb = new StringBuffer();
if (code == HttpsURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpConn.getInputStream(),
"UTF-8"));
while ((tempString = reader.readLine()) != null) {
sb.append(tempString);
}
if (null != reader) {
reader.close();
}
} else {
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpConn.getErrorStream(),
"UTF-8"));
while ((tempString = reader.readLine()) != null) {
sb.append(tempString);
}
if (null != reader) {
reader.close();
}
}
returnXml = sb.toString();
if (LOG.isDebugEnabled()) {
LOG.debug("return xml==>>" + returnXml);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(httpConn!=null){
httpConn.disconnect();
}
}
return returnXml;
}
}
2.UserModel 用于存放用户名,密码
import java.io.Serializable;
public class UserModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
public UserModel(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;
}
}
3.核心类 继承基类
import com.liferay.portal.security.auth.BaseAutoLogin;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.liferay.portal.kernel.json.JSONException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.model.User;
import com.liferay.portal.security.auth.BaseAutoLogin;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
public class InsecureAutoLogin extends BaseAutoLogin {
@Override
protected String[] doLogin(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String token = request.getParameter("token");
long companyId = PortalUtil.getCompanyId(request);
Map<String, String[]> parameterMap = request.getParameterMap();
String emailinputid = "_58_login";
String passwordinputid = "_58_password";
if (parameterMap.containsKey(emailinputid)
&& parameterMap.containsKey(passwordinputid)) {
String userinput_emailAddress = request.getParameter(emailinputid);
String userinput_password = request.getParameter(passwordinputid);
if (userinput_emailAddress != null) {
User user = UserLocalServiceUtil.getUserByEmailAddress(
companyId, userinput_emailAddress);
String username=user.getScreenName();
String usertoken=getusertoken(userinput_password, username);
response.addHeader("Set-Cookie", "usertoken="+usertoken+"; Path=/");
request.getSession().setAttribute("usertoken", usertoken);
return new String[] { String.valueOf(user.getUserId()),
user.getPassword(),
String.valueOf(user.isPasswordEncrypted()) };
}
}
return null;
}
private String getusertoken(String userinput_password, String username)
throws JSONException {
UserModel usermodel = new UserModel(username, userinput_password);
String json = JSONFactoryUtil.looseSerialize(usermodel);
String url = "http://192.168.1.31:8082/api/User/Token";
String result = HttpClient.get(url, json);
JSONObject objct = JSONFactoryUtil.createJSONObject(result);
JSONObject data = objct.getJSONObject("data");
String usertoken = data.getString("accesstoken");
return usertoken;
}
}
4.portal.properties
# Auto Login
auto.login.hooks=com.auth.InsecureAutoLogin
# Public Parameters
email.redirect.param=redirectfromemail=
download.asset.param=download-asset-id=
anchor.param=?anchor=
spiff.link.url=incentive-programs/spiff-program
spiff.flag.name=Spiff Flag
5.liferay-hook.xml
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>