1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
import java.security.Principal;
import java.security.PrivilegedAction;
import java.util.Hashtable;
import java.util.Map;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 com.fst.stanos.hibernate.Utilisateur;
import com.fst.stanos.security.auth.Auth;
import com.fst.stanos.security.auth.FileAuth;
import com.fst.stanos.security.auth.MyCallBackHandler;
import com.fst.stanos.security.auth.UserPrincipal;
import com.fst.stanos.struts.form.LoginForm;
////////////
import java.util.Set;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
/**
* MyEclipse Struts
* Creation date: 02-14-2010
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/slogin.jsp" scope="request" validate="true"
* @struts.action-forward name="client" path="/site/client/index.jsp" redirect="true"
* @struts.action-forward name="admin" path="/site/admin/index.jsp" redirect="true"
*/
public class LoginAction extends Action implements PrivilegedAction {
/*
* Generated Methods
*/
public Object run(){
String result ="success";
return result;
}
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
/*private LoginContext loginContext;
public boolean authenticate(String username,String password)
{
try
{
loginContext = new LoginContext("Stanos_v0.1");
loginContext.login();
}
catch (LoginException le)
{
//log.error("LoginException: " + le);
return false;
}
return true;
}
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
String username=loginForm.getJ_username();
String password=loginForm.getJ_password();
/*Auth fa=new FileAuth(username,password);
if(fa.authenticate()){
HttpSession session = request.getSession();
session.setAttribute(Auth.SUBJECT_SESSION_KEY,fa.getSubject());
//session.setAttribute("user", )
System.out.println("authentication successed..!"+fa.getSubject().hashCode());
}
else
System.out.println("authentication not successed..!");
return mapping.findForward("admin");
*/
try{
/*UserPrincipal user=new UserPrincipal(username);
MyCallBackHandler handler=new MyCallBackHandler(username,password);
//CallbackHandler handler1;
LoginContext loginContext = new LoginContext("loginmodule", handler);
loginContext.login();
Subject subject = loginContext.getSubject();
*/
// Set principals = subject.getPrincipals();
//principals.add(user);
/*request.getSession().setAttribute("user", user);
System.out.println("%%%%%%..."+user.getName());
System.out.println("%%%%%%..."+subject.toString());
HttpSession session = request.getSession();
session.setAttribute(Auth.SUBJECT_SESSION_KEY,subject);
request.getSession().setAttribute("login-context", loginContext);
request.getSession().setAttribute("subject", subject);
String result=(String)Subject.doAs(subject, this);
*/
/*FileAuth fa=new FileAuth(username,password);
fa.authenticate();
HttpSession session = request.getSession(true);
session.setAttribute(Auth.SUBJECT_SESSION_KEY,fa.getSubject());
session.setAttribute("username",username);
session.setAttribute("password",password);
session.setAttribute("javax.security.auth.subject", fa.getSubject());
*/
// Save our logged-in user in the session
// session = request.getSession();
//session.setAttribute(Constants.USER_KEY, user);
//loginContext.logout();
// Remove the obsolete form bean
/**********************/
HttpSession session = request.getSession(true);
Subject subject = (Subject)session.getAttribute("javax.security.auth.subject");
System.out.println(request.getAttribute("logged-in")+" <---> "+ session.getAttribute("logged-in"));
if (subject == null) {
subject = new Subject();
}
session.setAttribute("javax.security.auth.subject", subject);
LoginContext lc = null;
try {
lc = new LoginContext("loginmodule", subject, new MyCallBackHandler(username,password));
System.out.println("established new logincontext");
} catch (LoginException le) {
le.printStackTrace();
response.sendError(HttpServletResponse.SC_FORBIDDEN, request.getRequestURI());
//return;
}
try {
lc.login();
UserPrincipal user=new UserPrincipal(username);
//Map session1 =
//session1.put("logged-in", "true");
Subject subject2 = lc.getSubject();
Set principals = subject.getPrincipals();
principals.add(user);
session.setAttribute("javax.security.auth.subject", subject2);
session.setAttribute("logged-in", "true");
System.out.println(request.getAttribute("logged-in")+" <---> "+ session.getAttribute("logged-in"));
session.setAttribute("userName", username);
session.setAttribute("password", password);
session.setAttribute("javax.security.auth.subject", subject);
// if we return with no exception, authentication succeeded
} catch (Exception e) {
System.out.println("Login failed: " + e);
response.sendError(HttpServletResponse.SC_FORBIDDEN, request.getRequestURI());
//return;
}
/***********************/
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
}
}
//catch(LoginException e)
//{ //errors.add("loginerror", new ActionError("Wrong Username or Password")); saveErrors(request, errors);
//}
catch (Exception e)
{
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.login.authfailure"));
saveErrors(request, errors);
}
return mapping.findForward("admin");
/*try
{
SecurityAssociationHandler handler = new
SecurityAssociationHandler();
user = new SimplePrincipal(username);
handler.setSecurityInfo(user, password.toCharArray());
LoginContext loginContext = new LoginContext("loginmodule",
(CallbackHandler)handler);
loginContext.login();
Subject subject = loginContext.getSubject();
Set principals = subject.getPrincipals();
principals.add(user);
}catch(LoginException e)
{ errors.add("loginerror", new ActionError("Wrong Username or Password")); saveErrors(request, errors);
}*/
}
}
/************ page login.jsp****************/
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for LoginForm form</title>
</head>
<body>
<html:form action="/login">
j_username : <html:text property="j_username"/><html:errors property="j_username"/><br/>
j_password : <html:password property="j_password"/><html:errors property="j_password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html> |
Partager