📄 logonaction.java
字号:
package org.redsoft.forum.web;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
import javax.security.auth.login.LoginException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.Subject;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.redsoft.forum.ForumConstants;
import org.redsoft.forum.security.SimpleCallbackHandler;
/**
* Title: XP Forum
* Description: Login User by password
* @author Peng, Luo
* @version 1.0
* March 26, 2002
*/
public class LogonAction extends Action{
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
HttpSession session = request.getSession(false);
if (session == null) session = request.getSession(true);
ActionErrors errors = new ActionErrors();
String userName = ((LogonForm)form).getUserName();
String password = ((LogonForm)form).getPassword();
boolean autoLogon = ((LogonForm)form).getAutoLogon();
//System.out.println("AutoLogon: " + autoLogon);
Subject subject = null;
try{
// let the LoginContext instantiate a new Subject
LoginContext lc = new LoginContext("XForumLogin", new SimpleCallbackHandler( userName, password ) );
lc.login();
subject = lc.getSubject();
}catch (LoginException ex) {
ex.printStackTrace();
errors.add("User login", new ActionError("error.account.notFound") );
}
// Report any errors we have discovered back to the original form
if (!errors.empty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
// Remove the obsolete form bean
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
}
//Set account Object to session
session.setAttribute( ForumConstants.USER_KEY, subject );
// if user choose auto logon, save username to cookie
if (autoLogon){
Cookie cookie = new Cookie( ForumConstants.USER_KEY, userName );
// default cookie's age is -1, indicating the cookie will persist until browser shutdown.
// so set cookie's age to 120 days: 120 * 24 * 60 * 60 * 60 seconds
cookie.setMaxAge( 622080000 );
response.addCookie( cookie );
System.out.println("Save user " + userName + " to cookie");
}else{
// Otherwise delete cookie
Cookie cookie = new Cookie( ForumConstants.USER_KEY, userName );
// set cookie's age to zero to delete it
cookie.setMaxAge( 0 );
response.addCookie( cookie );
System.out.println("Delete user " + userName + " from cookie");
}
final String destinatedUrl = (String)request.getParameter( ForumConstants.DEST_URL );
// If there is a destinated url,forward to this url
if( destinatedUrl != null && destinatedUrl.length() > 0 ){
return new ActionForward( destinatedUrl );
}else{
// Forward control to the specified success URI
return (mapping.findForward("success"));
}
}
}//EOC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -