read credentials from URL path to add to the textfield directly

Signed-off-by: Michael Hirsch <michael.hirsch@bosch-si.com>
This commit is contained in:
Michael Hirsch
2016-05-30 15:00:11 +02:00
parent 2a6b531bff
commit fc5a22302b

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.ui.login;
import java.net.URI;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;
@@ -25,6 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.util.AntPathMatcher;
import org.vaadin.spring.security.VaadinSecurity;
import com.vaadin.event.ShortcutAction.KeyCode;
@@ -56,6 +58,12 @@ import com.vaadin.ui.themes.ValoTheme;
@SpringView(name = "")
@UIScope
public class LoginView extends VerticalLayout implements View {
private static final String TENANT_PATTERN_PLACEHOLDER = "tenant";
private static final String USER_PATTERN_PLACEHOLDER = "user";
private static final String LOGIN_TENANT_USER_URL_PATTERN = "**/#/{" + TENANT_PATTERN_PLACEHOLDER + "}/{"
+ USER_PATTERN_PLACEHOLDER + "}";
private static final String LOGIN_USER_URL_PATTERN = "**/#/{" + USER_PATTERN_PLACEHOLDER + "}";
private static final String LOGIN_TEXTFIELD = "login-textfield";
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LoggerFactory.getLogger(LoginView.class);
@@ -75,6 +83,9 @@ public class LoginView extends VerticalLayout implements View {
@Autowired
private transient MultitenancyIndicator multiTenancyIndicator;
private final transient AntPathMatcher matcher = new AntPathMatcher();
private boolean useCookie = true;
private TextField username;
private TextField tenant;
private PasswordField password;
@@ -106,17 +117,45 @@ public class LoginView extends VerticalLayout implements View {
*/
@PostConstruct
public void render() {
final URI spURI = Page.getCurrent().getLocation();
final String lookForDemoFragment = spURI.toString();
if (lookForDemoFragment.contains("?demo")) {
final String uriPath = spURI.toString();
if (uriPath.contains("?demo")) {
login(uiProperties.getDemo().getTenant(), uiProperties.getDemo().getUser(),
uiProperties.getDemo().getPassword(), false);
}
final Component loginForm = buildLoginForm();
addComponent(loginForm);
setComponentAlignment(loginForm, Alignment.MIDDLE_CENTER);
readCredentialsFromUriPath(uriPath);
}
private void readCredentialsFromUriPath(final String uriPath) {
String urlTenant = null;
String urlUser = null;
if (matcher.match(LOGIN_USER_URL_PATTERN, uriPath)) {
urlUser = matcher.extractUriTemplateVariables(LOGIN_USER_URL_PATTERN, uriPath)
.get(USER_PATTERN_PLACEHOLDER);
} else if (matcher.match(LOGIN_TENANT_USER_URL_PATTERN, uriPath)) {
final Map<String, String> extractUriTemplateVariables = matcher
.extractUriTemplateVariables(LOGIN_TENANT_USER_URL_PATTERN, uriPath);
urlTenant = extractUriTemplateVariables.get(TENANT_PATTERN_PLACEHOLDER);
urlUser = extractUriTemplateVariables.get(USER_PATTERN_PLACEHOLDER);
}
if (urlUser != null) {
useCookie = false;
filloutUsernameTenantFields(urlTenant, urlUser);
}
}
private void filloutUsernameTenantFields(final String tenantValue, final String userValue) {
if (tenant != null && tenantValue != null) {
tenant.setValue(tenantValue);
}
if (userValue != null) {
username.setValue(userValue);
}
}
private Component buildLoginForm() {
@@ -281,6 +320,9 @@ public class LoginView extends VerticalLayout implements View {
*/
@Override
public void enter(final ViewChangeEvent event) {
if (!useCookie) {
return;
}
final Cookie usernameCookie = getCookieByName(SP_LOGIN_USER);
@@ -348,7 +390,7 @@ public class LoginView extends VerticalLayout implements View {
vaadinSecurity.login(new UsernamePasswordAuthenticationToken(user, password));
}
/* set success login cookies */
if (setCookies) {
if (setCookies && useCookie) {
setCookies();
}