| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.common.ui.client.security; |
| 17 | |
|
| 18 | |
import org.kuali.student.common.ui.client.application.Application; |
| 19 | |
import org.kuali.student.common.ui.client.application.ApplicationContext; |
| 20 | |
import org.kuali.student.common.ui.client.widgets.KSErrorDialog; |
| 21 | |
import org.kuali.student.common.ui.client.widgets.KSLightBox; |
| 22 | |
|
| 23 | |
import com.google.gwt.event.dom.client.ClickEvent; |
| 24 | |
import com.google.gwt.event.dom.client.ClickHandler; |
| 25 | |
import com.google.gwt.http.client.Request; |
| 26 | |
import com.google.gwt.http.client.RequestBuilder; |
| 27 | |
import com.google.gwt.http.client.RequestCallback; |
| 28 | |
import com.google.gwt.http.client.RequestException; |
| 29 | |
import com.google.gwt.http.client.Response; |
| 30 | |
import com.google.gwt.http.client.URL; |
| 31 | |
import com.google.gwt.user.client.ui.Button; |
| 32 | |
import com.google.gwt.user.client.ui.FlexTable; |
| 33 | |
import com.google.gwt.user.client.ui.Label; |
| 34 | |
import com.google.gwt.user.client.ui.PasswordTextBox; |
| 35 | |
import com.google.gwt.user.client.ui.TextBox; |
| 36 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | public class SpringSecurityLoginDialogHandler implements SessionTimeoutHandler{ |
| 46 | 0 | final static ApplicationContext context = Application.getApplicationContext(); |
| 47 | 0 | static boolean CASrequiresAPageRefreshViaJavascript = Boolean.TRUE; |
| 48 | 0 | public final String TIMEOUT_MSG = "Your session has timed out. Please login again."; |
| 49 | |
|
| 50 | |
protected KSLightBox lightbox; |
| 51 | |
protected TextBox username; |
| 52 | |
protected PasswordTextBox password; |
| 53 | |
protected Label errorLabel; |
| 54 | |
|
| 55 | |
private native void reload() |
| 56 | |
|
| 57 | |
; |
| 58 | |
|
| 59 | |
@Override |
| 60 | |
public boolean isSessionTimeout(Throwable error) { |
| 61 | 0 | boolean InvocationException = error.toString().contains("com.google.gwt.user.client.rpc.InvocationException"); |
| 62 | 0 | boolean CAS = error.toString().contains(""); |
| 63 | 0 | boolean normalLogin = error.toString().contains("Login"); |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 0 | if(CAS ) |
| 70 | |
{ |
| 71 | 0 | CASrequiresAPageRefreshViaJavascript = true; |
| 72 | |
} |
| 73 | |
|
| 74 | 0 | return CAS || normalLogin ; |
| 75 | |
} |
| 76 | |
|
| 77 | |
@Override |
| 78 | |
public void handleSessionTimeout() { |
| 79 | 0 | if(!CASrequiresAPageRefreshViaJavascript){ |
| 80 | 0 | if (lightbox == null){ |
| 81 | 0 | createLoginPanel(); |
| 82 | |
} else { |
| 83 | 0 | resetLoginPanel(); |
| 84 | |
} |
| 85 | 0 | lightbox.setSize(460, 220); |
| 86 | 0 | lightbox.show(); |
| 87 | 0 | }else{reload();} |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
private void createLoginPanel(){ |
| 91 | 0 | lightbox = new KSLightBox(); |
| 92 | 0 | VerticalPanel panel = new VerticalPanel(); |
| 93 | |
|
| 94 | 0 | FlexTable table = new FlexTable(); |
| 95 | |
|
| 96 | 0 | errorLabel = new Label(); |
| 97 | 0 | errorLabel.setText(TIMEOUT_MSG); |
| 98 | 0 | errorLabel.setStyleName("KSError"); |
| 99 | |
|
| 100 | 0 | username = new TextBox(); |
| 101 | 0 | username.setName("j_username"); |
| 102 | |
|
| 103 | 0 | password = new PasswordTextBox(); |
| 104 | 0 | password.setName("j_password"); |
| 105 | |
|
| 106 | 0 | table.setText(0, 0, "Username"); |
| 107 | 0 | table.setWidget(0, 1, username); |
| 108 | |
|
| 109 | 0 | table.setText(1,0, "Password"); |
| 110 | 0 | table.setWidget(1,1, password); |
| 111 | |
|
| 112 | 0 | table.setWidget(2,0,(new Button("Login", new ClickHandler() { |
| 113 | |
public void onClick(ClickEvent event) { |
| 114 | |
|
| 115 | 0 | StringBuffer postData = new StringBuffer(); |
| 116 | 0 | postData.append(URL.encode("j_username")).append("=").append(username.getText()); |
| 117 | 0 | postData.append("&").append(URL.encode("j_password")).append("=").append(password.getText()); |
| 118 | |
|
| 119 | 0 | RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, context.getApplicationContextUrl() + "/j_spring_security_check"); |
| 120 | 0 | builder.setHeader("Content-type", "application/x-www-form-urlencoded"); |
| 121 | |
|
| 122 | |
try{ |
| 123 | 0 | builder.sendRequest(postData.toString(), new RequestCallback(){ |
| 124 | |
|
| 125 | |
@Override |
| 126 | |
public void onError(Request req, Throwable caught) { |
| 127 | 0 | lightbox.hide(); |
| 128 | 0 | KSErrorDialog.show(caught); |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
@Override |
| 132 | |
public void onResponseReceived(Request req, Response res) { |
| 133 | 0 | if (res.getStatusCode() == Response.SC_OK && !res.getText().contains("Bad credentials")){ |
| 134 | 0 | lightbox.hide(); |
| 135 | |
} else { |
| 136 | 0 | errorLabel.setText("Your login attempt was not successful, try again."); |
| 137 | |
} |
| 138 | 0 | }}); |
| 139 | 0 | } catch (RequestException e) { |
| 140 | 0 | KSErrorDialog.show(e); |
| 141 | 0 | } |
| 142 | 0 | } |
| 143 | |
} |
| 144 | |
))); |
| 145 | |
|
| 146 | 0 | panel.add(errorLabel); |
| 147 | 0 | panel.add(table); |
| 148 | |
|
| 149 | 0 | panel.setStyleName("KSLoginPanel"); |
| 150 | 0 | lightbox.setWidget(panel); |
| 151 | 0 | } |
| 152 | |
|
| 153 | |
private void resetLoginPanel(){ |
| 154 | 0 | username.setText(""); |
| 155 | 0 | password.setText(""); |
| 156 | 0 | errorLabel.setText(TIMEOUT_MSG); |
| 157 | 0 | } |
| 158 | |
|
| 159 | |
} |