<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "JForum SSO with User's first name and last name saved"]]></title>
		<link>https://forum.andowson.com/posts/list/7.page</link>
		<description><![CDATA[Latest messages posted in the topic "JForum SSO with User's first name and last name saved"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>JForum SSO with User&#39;s first name and last name saved</title>
				<description><![CDATA[ 1.Add these two columns (first_name and last_name) into jforum_users table: 
<br>
[code=sql] 
<br>
ALTER TABLE jforum_users ADD first_name VARCHAR(50); 
<br>
ALTER TABLE jforum_users ADD last_name VARCHAR(50); 
<br>
[/code] 
<br>
2.Add these keys to WEB-INF/config/jforum-custom.conf: 
<br>
[code] 
<br>
authentication.type=sso 
<br>
sso.implementation=net.jforum.sso.MyUserSSO 
<br>
sso.redirect=http\://member.andowson.com/sso/login.jsp 
<br>
cookie.name.user=username 
<br>
cookie.name.email=email 
<br>
cookie.name.first=firstname 
<br>
cookie.name.last=lastname 
<br>
sso.firstname.attribute=firstname 
<br>
sso.lastname.attribute=lastname 
<br>
sso.default.firstname=Unknown 
<br>
sso.default.lastname=User 
<br>
[/code] 
<br>
[i]member.andowson.com[/i] is where we are going to authenticate the user. Change to your real case. 
<br>
<br>
3.Modify net.jforum.util.preferences.ConfigKeys.java: 
<br>
Add these lines into ConfigKeys.java 
<br>
[code] 
<br>
 public static final String SSO_FIRSTNAME_ATTRIBUTE = "sso.firstname.attribute"; 
<br>
 public static final String SSO_LASTNAME_ATTRIBUTE = "sso.lastname.attribute"; 
<br>
 public static final String SSO_DEFAULT_FIRSTNAME = "sso.default.firstname"; 
<br>
 public static final String SSO_DEFAULT_LASTNAME = "sso.default.lastname"; 
<br>
 public static final String COOKIE_NAME_EMAIL = "cookie.name.email"; 
<br>
 public static final String COOKIE_NAME_FIRST = "cookie.name.first"; 
<br>
 public static final String COOKIE_NAME_LAST = "cookie.name.last"; 
<br>
[/code] 
<br>
4.Add net.jforum.sso.MyUserSSO.java which implements [i]net.jforum.sso.SSO[/i] interface 
<br>
[code] 
<br>
package net.jforum.sso; 
<br>
<br>
import java.io.UnsupportedEncodingException; 
<br>
<br>
import java.net.URLDecoder; 
<br>
import javax.servlet.http.Cookie; 
<br>
import net.jforum.context.RequestContext; 
<br>
import net.jforum.context.SessionContext; 
<br>
import net.jforum.ControllerUtils; 
<br>
import net.jforum.JForumExecutionContext; 
<br>
import net.jforum.entities.UserSession; 
<br>
import net.jforum.util.preferences.ConfigKeys; 
<br>
import net.jforum.util.preferences.SystemGlobals; 
<br>
import org.apache.log4j.Logger; 
<br>
<br>
public class MyUserSSO implements SSO { 
<br>
<br>
 static final Logger logger = Logger.getLogger(CookieUserSSO.class.getName()); 
<br>
<br>
 public String authenticateUser(RequestContext request) { 
<br>
 // myapp login cookie, contain logged username 
<br>
 Cookie myCookie = ControllerUtils.getCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_USER)); 
<br>
 String username = null; 
<br>
 String email = null; 
<br>
 String firstName = null; 
<br>
 String lastName = null; 
<br>
<br>
 if (myCookie != null) { 
<br>
 username = myCookie.getValue(); 
<br>
 } 
<br>
 SessionContext session = JForumExecutionContext.getRequest().getSessionContext(); 
<br>
 String encoding = "Big5"; 
<br>
 try { 
<br>
 myCookie = ControllerUtils.getCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_EMAIL)); 
<br>
 if (myCookie != null) { 
<br>
 email = myCookie.getValue(); 
<br>
 session.setAttribute(SystemGlobals.getValue(ConfigKeys.SSO_EMAIL_ATTRIBUTE), URLDecoder.decode(email, encoding)); 
<br>
 } 
<br>
 myCookie = ControllerUtils.getCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_FIRST)); 
<br>
 if (myCookie != null) { 
<br>
 firstName = myCookie.getValue(); 
<br>
 session.setAttribute(SystemGlobals.getValue(ConfigKeys.SSO_FIRSTNAME_ATTRIBUTE), URLDecoder.decode(firstName, encoding)); 
<br>
 } 
<br>
 myCookie = ControllerUtils.getCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_LAST)); 
<br>
 if (myCookie != null) { 
<br>
 lastName = myCookie.getValue(); 
<br>
 session.setAttribute(SystemGlobals.getValue(ConfigKeys.SSO_LASTNAME_ATTRIBUTE), URLDecoder.decode(lastName, encoding)); 
<br>
 } 
<br>
 } catch (UnsupportedEncodingException e) { 
<br>
 e.printStackTrace(); 
<br>
 } 
<br>
 return username; // jforum username 
<br>
 } 
<br>
<br>
 public boolean isSessionValid(UserSession userSession, RequestContext request) { 
<br>
 Cookie SSOCookie = ControllerUtils.getCookie( 
<br>
 SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_USER)); // myapp login cookie 
<br>
 String remoteUser = null; 
<br>
<br>
 if (SSOCookie != null) { 
<br>
 remoteUser = SSOCookie.getValue(); // jforum username 
<br>
 } 
<br>
<br>
 // user has since logged out 
<br>
 if (remoteUser == null 
<br>
 &amp;&amp; userSession.getUserId() != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) { 
<br>
 return false; 
<br>
 // user has since logged in 
<br>
 } else if (remoteUser != null 
<br>
 &amp;&amp; userSession.getUserId() == SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) { 
<br>
 return false; 
<br>
 // user has changed user 
<br>
 } else if (remoteUser != null &amp;&amp; !remoteUser.equals(userSession.getUsername())) { 
<br>
 return false; 
<br>
 } 
<br>
 return true; // myapp user and forum user the same 
<br>
 } 
<br>
} 
<br>
[/code] 
<br>
5.Modify net.jforum.ControllerUtils.java: 
<br>
edit method: protected void checkSSO(UserSession userSession) 
<br>
[code] 
<br>
 /** 
<br>
 * Checks for user authentication using some SSO implementation 
<br>
 * @param userSession UserSession 
<br>
 */ 
<br>
 protected void checkSSO(UserSession userSession) 
<br>
 { 
<br>
 try { 
<br>
 SSO sso = (SSO) Class.forName(SystemGlobals.getValue(ConfigKeys.SSO_IMPLEMENTATION)).newInstance(); 
<br>
 String username = sso.authenticateUser(JForumExecutionContext.getRequest()); 
<br>
<br>
 if (username == null || username.trim().equals("")) { 
<br>
 userSession.makeAnonymous(); 
<br>
 } 
<br>
 else { 
<br>
 SSOUtils utils = new SSOUtils(); 
<br>
<br>
 if (!utils.userExists(username)) { 
<br>
 SessionContext session = JForumExecutionContext.getRequest().getSessionContext(); 
<br>
<br>
 String email = (String) session.getAttribute(SystemGlobals.getValue(ConfigKeys.SSO_EMAIL_ATTRIBUTE)); 
<br>
 String password = (String) session.getAttribute(SystemGlobals.getValue(ConfigKeys.SSO_PASSWORD_ATTRIBUTE)); 
<br>
 String firstName = (String) session.getAttribute(SystemGlobals.getValue(ConfigKeys.SSO_FIRSTNAME_ATTRIBUTE)); 
<br>
 String lastName = (String) session.getAttribute(SystemGlobals.getValue(ConfigKeys.SSO_LASTNAME_ATTRIBUTE)); 
<br>
<br>
 if (email == null) { 
<br>
 email = SystemGlobals.getValue(ConfigKeys.SSO_DEFAULT_EMAIL); 
<br>
 } 
<br>
<br>
 if (password == null) { 
<br>
 password = SystemGlobals.getValue(ConfigKeys.SSO_DEFAULT_PASSWORD); 
<br>
 } 
<br>
<br>
 if (firstName == null) { 
<br>
 firstName = SystemGlobals.getValue(ConfigKeys.SSO_DEFAULT_FIRSTNAME); 
<br>
 } 
<br>
<br>
 if (lastName == null) { 
<br>
 lastName = SystemGlobals.getValue(ConfigKeys.SSO_DEFAULT_LASTNAME); 
<br>
 } 
<br>
<br>
 utils.register(password, email, firstName, lastName); 
<br>
 } 
<br>
<br>
 this.configureUserSession(userSession, utils.getUser()); 
<br>
 } 
<br>
 } 
<br>
 catch (Exception e) { 
<br>
 e.printStackTrace(); 
<br>
 throw new ForumException("Error while executing SSO actions: " + e); 
<br>
 } 
<br>
 } 
<br>
[/code] 
<br>
6.Modify net.jforum.sso.SSOUtils.java 
<br>
add a new method: public void register(String password, String email, String firstName, String lastName) 
<br>
[code] 
<br>
 /** 
<br>
 * Registers a new user. 
<br>
 * This method should be used together with {@link #userExists(String)}. 
<br>
 * 
<br>
 * @param password the user's password. It &lt;em&gt;should&lt;/em&gt; be the real / final 
<br>
 * password. In other words, the data passed as password is the data that'll be 
<br>
 * written to the database 
<br>
 * @param email the user's email 
<br>
 * @param firstName the user's first name 
<br>
 * @param lasstName the user's last name 
<br>
 * @see #getUser() 
<br>
 */ 
<br>
 public void register(String password, String email, String firstName, String lastName) 
<br>
 { 
<br>
 if (this.exists) { 
<br>
 return; 
<br>
 } 
<br>
<br>
 // Is a new user for us. Register him 
<br>
 this.user = new User(); 
<br>
 user.setUsername(this.username); 
<br>
 user.setPassword(password); 
<br>
 user.setEmail(email); 
<br>
 user.setActive(1); 
<br>
 user.setFirstName(firstName); 
<br>
 user.setLastName(lastName); 
<br>
<br>
 this.dao.addNew(user); 
<br>
 } 
<br>
[/code] 
<br>
7.Modify net.jforum.dao.generic.GenericUserDAO.java 
<br>
store firstName and lastName to database 
<br>
[code] 
<br>
 protected void initNewUser(User user, PreparedStatement p) throws SQLException 
<br>
 { 
<br>
 p.setString(1, user.getUsername()); 
<br>
 p.setString(2, user.getPassword()); 
<br>
 p.setString(3, user.getEmail()); 
<br>
 p.setTimestamp(4, new Timestamp(System.currentTimeMillis())); 
<br>
 p.setString(5, user.getActivationKey()); 
<br>
 p.setString(6, user.getFirstName()); 
<br>
 p.setString(7, user.getLastName()); 
<br>
 } 
<br>
[/code] 
<br>
8.Modify WEB-INF/config/database/generic/generic_queries.sql 
<br>
[code=sql] 
<br>
UserModel.addNew = INSERT INTO jforum_users (username, user_password, user_email, user_regdate, user_actkey, rank_id, first_name, last_name) VALUES (?, ?, ?, ?, ?, 0, ?, ?) 
<br>
[/code] 
<br>
Oracle Database user have to edit WEB-INF/config/database/oracle/oracle.sql 
<br>
[code] 
<br>
UserModel.addNew = INSERT INTO jforum_users (user_id, username, user_password, user_email, user_regdate, user_actkey, rank_id, first_name, last_name) VALUES (jforum_users_seq.nextval, ?, ?, ?, ?, ?, 0, ?, ?) 
<br>
[/code] 
<br>
9.Edit /sso/login.jsp on member.andowson.com 
<br>
[code] 
<br>
&lt;%@ page contentType="text/html;charset=big5" %&gt; 
<br>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; 
<br>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; 
<br>
&lt;head&gt; 
<br>
&lt;meta http-equiv="Content-Type" content="text/html; charset=big5" /&gt; 
<br>
&lt;title&gt;JForum SSO Login&lt;/title&gt; 
<br>
&lt;/head&gt; 
<br>
&lt;body&gt; 
<br>
&lt;form name="loginform" method="post" action="proc_login.jsp"&gt; 
<br>
 &lt;input type="hidden" name="redirect" value="&lt;%=request.getParameter("returnUrl")%&gt;" /&gt; 
<br>
 &lt;div align="center"&gt; 
<br>
 Username: &lt;input type="text" name="username" /&gt;
<br>
<br>
 Password: &lt;input type="password" name="password" /&gt;
<br>
<br>
 &lt;input type="submit" value="Login" /&gt; 
<br>
 &lt;/div&gt; 
<br>
&lt;/form&gt; 
<br>
&lt;/body&gt; 
<br>
&lt;/html&gt; 
<br>
[/code] 
<br>
10.Edit /sso/proc_login.jsp on member.andowson.com 
<br>
[code] 
<br>
&lt;%@ page contentType="text/html;charset=big5" %&gt; 
<br>
&lt;%@ page import="java.sql.*" %&gt; 
<br>
&lt;%@ page import="java.util.Properties" %&gt; 
<br>
&lt;%@ page import="com.oreilly.servlet.ParameterParser" %&gt; 
<br>
&lt;% 
<br>
 ParameterParser parser = new ParameterParser(request); 
<br>
 parser.setCharacterEncoding("Big5"); 
<br>
 String username = parser.getStringParameter("username", null); 
<br>
 String password = parser.getStringParameter("password", null); 
<br>
 String redirect = parser.getStringParameter("redirect", null); 
<br>
<br>
 Connection con = null; 
<br>
 PreparedStatement pstmt = null; 
<br>
 ResultSet rs = null; 
<br>
 String sql = null; 
<br>
 String email = null; 
<br>
 String firstName = null; 
<br>
 String lastName = null; 
<br>
 boolean login = false; 
<br>
<br>
 if (username != null &amp;&amp; password != null) { 
<br>
 try { 
<br>
 final String url = "jdbc:postgresql://127.0.0.1:5432/member"; 
<br>
 final Properties info = new Properties(); 
<br>
 info.setProperty("user", "member"); 
<br>
 info.setProperty("password", "member"); 
<br>
 Class.forName("org.postgresql.Driver"); 
<br>
 con = DriverManager.getConnection(url, info); 
<br>
 sql = "select * from users where username = ? and password = ?"; 
<br>
 pstmt = con.prepareStatement(sql); 
<br>
 pstmt.setString(1, username); 
<br>
 pstmt.setString(2, password); 
<br>
 rs = pstmt.executeQuery(); 
<br>
 if (rs.next()) { 
<br>
 email = rs.getString("email"); 
<br>
 firstName = rs.getString("first_name"); 
<br>
 lastName = rs.getString("last_name"); 
<br>
 login = true; 
<br>
 } 
<br>
 rs.close(); 
<br>
 rs = null; 
<br>
 pstmt.close(); 
<br>
 pstmt = null; 
<br>
 con.close(); 
<br>
 con = null; 
<br>
 } catch (SQLException se) { 
<br>
 out.println(se.getMessage()); 
<br>
 } finally { 
<br>
 // Always make sure result sets and statements are closed, 
<br>
 // and the connection is returned to the pool 
<br>
 if (rs != null) { 
<br>
 try { 
<br>
 rs.close(); 
<br>
 } catch (SQLException e) { 
<br>
 out.println(e.getMessage()); 
<br>
 } 
<br>
 rs = null; 
<br>
 } 
<br>
 if (pstmt != null) { 
<br>
 try { 
<br>
 pstmt.close(); 
<br>
 } catch (SQLException e) { 
<br>
 out.println(e.getMessage()); 
<br>
 } 
<br>
 pstmt = null; 
<br>
 } 
<br>
 if (con != null) { 
<br>
 try { 
<br>
 con.close(); 
<br>
 } catch (SQLException e) { 
<br>
 out.println(e.getMessage()); 
<br>
 } 
<br>
 con = null; 
<br>
 } 
<br>
 } 
<br>
 } 
<br>
 if (login) { 
<br>
 Cookie cookieUsername = new Cookie("username", username); 
<br>
 cookieUsername.setMaxAge(-1); 
<br>
 cookieUsername.setPath("/"); 
<br>
 response.addCookie(cookieUsername); 
<br>
<br>
 Cookie cookieEmail = new Cookie("email", java.net.URLEncoder.encode(email, "Big5")); 
<br>
 cookieEmail.setMaxAge(-1); 
<br>
 cookieEmail.setPath("/"); 
<br>
 response.addCookie(cookieEmail); 
<br>
<br>
 Cookie cookieFirstName = new Cookie("firstname", java.net.URLEncoder.encode(firstName, "Big5")); 
<br>
 cookieFirstName.setMaxAge(-1); 
<br>
 cookieFirstName.setPath("/"); 
<br>
 response.addCookie(cookieFirstName); 
<br>
<br>
 Cookie cookieLastName = new Cookie("lastname", java.net.URLEncoder.encode(lastName, "Big5")); 
<br>
 cookieLastName.setMaxAge(-1); 
<br>
 cookieLastName.setPath("/"); 
<br>
 response.addCookie(cookieLastName); 
<br>
<br>
 if (redirect != null &amp;&amp; redirect.trim().length() &gt; 0 &amp;&amp; !"null".equals(redirect)) { 
<br>
 response.sendRedirect(redirect); 
<br>
 } 
<br>
 } else { 
<br>
 out.println("Login failed!"); 
<br>
 } 
<br>
%&gt; 
<br>
[/code] 
<br>]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/227/343.page</guid>
				<link>https://forum.andowson.com/posts/preList/227/343.page</link>
				<pubDate><![CDATA[Fri, 14 Mar 2008 23:13:34]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>