我需要通過對外部系統進行身份驗證來登錄Liferay中的用戶。不過,我仍然需要有一個有效的Liferay會話。所以,我需要通過在沒有密碼的情況下登錄Liferay用戶
- 登錄到Liferay從用戶詢問用戶名/密碼,
- 使用它們來對外部系統進行身份驗證。
- 只使用用戶名(而不是密碼)對Liferay登錄進行身份驗證。
- 如果外部系統登錄成功,則登錄用戶。
我做了幾件事情:
- 自動登錄
- 掛鉤UserLocalService.authenticateByScreenName覆蓋
- auth.pipeline前和檢查=假
- LoginFilter。
這些沒有奏效。這是對這些方法的解釋。這個想法是包括 //如果這些方法的工作,調用外部系統進行身份驗證將被包括在內。 請糾正我犯錯的地方,如果某種方法比其他方法更好。
1.自動登錄:
一個。設置portal-ext.properties
auto.login.hooks=com.poc.AutoLoginFil
b。創建班級
public class AutoLoginFilter implements AutoLogin {
public AutoLoginFilter() {
super();
}
@Override
public String[] login(HttpServletRequest req, HttpServletResponse arg1) throws AutoLoginException {
//Call external system to authenticate
User user = UserLocalServiceUtil.getUserByScreenName(company.getCompanyId(), login);
credentials[0] = String.valueOf(user.getUserId());
credentials[1] = "undefined";
credentials[2] = Boolean.TRUE.toString();
return credentials;
}
}
c。部署插件項目,重新啓動服務器並轉至http:// localhost:8080/web/guest/home。這應該登錄爲joebloggs
這不起作用
2.掛鉤UserLocalService.authenticateByScreenName覆蓋
一個。在liferay-hook.xml中
<service>
<service-type>
com.liferay.portal.service.UserLocalService
</service-type>
<service-impl>
com.test.UserService
</service-impl>
</service>
b。擴展UserLocalServiceWrapper並使用自定義類。
public class UserService extends UserLocalServiceWrapper
{
@Override
public int authenticateByScreenName(long companyId, String screenName, String password, Map headerMap, Map parameterMap, Map resultsMap)
{
//Call external system to authenticate
String name = "";
log.info(screenName);
return SUCCESS;
}
}
當我登錄時,它應該使用任何密碼。它不是。
3. auth.pipeline前和檢查=假
一個。在portal-ext.properties中
auth.pipeline.enable.liferay.check=false
auth.pipeline.pre=com.test.AutoLoginCustom
b。然後,在
public class AutoLoginCustom implements AutoLogin
{
@Override
public String[] login(HttpServletRequest arg0, HttpServletResponse arg1)
throws AutoLoginException {
@Override
public String[] login(HttpServletRequest arg0, HttpServletResponse arg1)
{
//Call external system to authenticate
credentials[0] = "joebloggs";
credentials[1] = "undefined";
credentials[2] = Boolean.TRUE.toString();
return credentials;
}
c。部署項目並重新啓動服務器。請轉至http://localhost:8080/web/guest/home。使用用戶名和不同的密碼登錄。它不會登錄。它甚至沒有打到AutoLoginCustom java中的調試點。
4. LoginFilter 在liferay的-hook.xml,
<servlet-filter>
<servlet-filter-name>Login</servlet-filter-name>
<servlet-filter-impl>com.test.AutoLoginFilter</servlet-filter-impl>
</servlet-filter>
在AutoLoginFilter
public class AutoLoginFil implements Filter
{
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
//Call external system to authenticate
log.debug("doFilter");
}
}
調試濾波器不被調用。
在這些方法中是否有錯誤,如果是的話,它是什麼,是否有不同的方法來做到這一點? 我已經看過以下參考資料。
How do I use autologin in liferay?
Liferay - AutoLogin + Authenticator - Get Credentials From Request Header
嗨,我正在尋找一個類似的項目來驗證外部系統。你還需要自動登錄定製嗎?如果你可以發佈你的外部系統調用,並且在這裏清除了敏感的細節,這將是非常好的。非常感謝! – VC1
外部系統調用可以由任何東西(Db,LDAP等)。下面的示例顯示了ldap調用 Hashtable env = new Hashtable (); env.put(Context.INITIAL_CONTEXT_FACTORY,「com.sun.jndi.ldap.LdapCtxFactory」); env.put(Context.PROVIDER_URL,「ldap:// host:port」); env.put(Context.SECURITY_AUTHENTICATION,「simple」); env.put(Context.SECURITY_PRINCIPAL,screenName); //可能需要域 env.put(Context.SECURITY_CREDENTIALS,password); DirContext ctx = new InitialDirContext(env); externalAuthenticationSuccess =(ctx!= null); –
user1592521
抱歉格式化。我不知道如何使代碼作爲評論,即使我縮進空間後。 – user1592521