Liferay Portal (三)

实现登录方式的改变

一、新建一个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>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javafanwk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值