2012-08-16 30 views
0

在Spring Security中使用自定义JSP登录页面相当简单。我们的应用程序基于Vaadin,但我不想拥有JSP登录页面。我想要的是作为Vaadin小部件创建的自定义花哨登录窗口。Spring Security + Vaadin:如何创建自定义的非JSP登录表单?

那么在技术上,我可以使用Vaadin的的FormLayout和名称字段,如为j_username为j_password ...但是这是Java类,而不是JSP文件,所以我该怎么在HTTP春季安全元素指定?我的意思是:

<http auto-config='true'> 
    <intercept-url pattern="/**" access="ROLE_USER" /> 
    <form-login login-page='MyLoginWindow.java or what?' /> 
</http> 

回答

1

我使用Spring Security完全陌生的,但是我们在Vaadin球员之一制成的演示应用程序几年之前https://github.com/peholmst/SpringSecurityDemo。我不知道它是否仍然是最新的,或者它甚至可以回答你的问题,但也许你可以看一看,看看你是否能从那里得到答案。否则,你也许可以亲自看看Petter,看看他是否对这个话题有了新的想法。

+0

感谢Jonas,它是@OlegYch建议的更复杂的例子。它将AuthenticationManager注入到Vaadin类中,但并未真正使用spring的过滤器链 – Xorty 2012-08-20 15:02:55

1

使用LoginForm的和LoginListener使用这样的

try { 
    val authentication = new UsernamePasswordAuthenticationToken(name, pass) 
    SecurityContextHolder.getContext.setAuthentication(authenticationManager.authenticate(authentication)) 
} catch { 
    case e: AuthenticationException => { 
    SecurityContextHolder.clearContext() 
    } 
} 
+0

好吧,所以这似乎工作,但它够了吗? Spring Security过滤器是否正常使用这种方法或者还有其他的事情要做? – Xorty 2012-08-16 18:55:02

+0

nope,过滤器将不会被调用,但您总是可以自己调用所需的配置,例如,应用会话策略使用sessionAuthenticationStrategy.onAuthentication(身份验证,请求,null) – OlegYch 2012-08-17 13:24:44

+0

ufff在我的情况下不是很令人满意。我会尽快在这里开放赏金。 – Xorty 2012-08-17 17:51:55

1

请看下面我的方法。该代码显示单击登录按钮时发生的功能。

loginBtn.addListener(new Button.ClickListener() {    
     @Override 
     public void buttonClick(ClickEvent event) { 
      // Getting the helper for working with spring context 
      // found here https://vaadin.com/wiki/-/wiki/Main/Spring%20Integration 
      SpringContextHelper helper = new SpringContextHelper(getApplication()); 

      // Get the providerManagerBean 
      ProviderManager authenticationManager = (ProviderManager)helper 
        .getBean("authenticationManager"); 

      // Get entered data for name and password 
      String name = usernameEntered; 
      String password = passwordEntered; 

      // Validation 
      if (StringUtils.isBlank(name) || StringUtils.isBlank(password)) { 
       getWindow().showNotification("Username or password cannot be empty", 
         Notification.TYPE_ERROR_MESSAGE); 
      } else { 
       try { 
        // Security functionality goes here 
        UsernamePasswordAuthenticationToken token = 
          new UsernamePasswordAuthenticationToken(name, password); 

        Authentication authentication = authenticationManager.authenticate(token); 

        // Set the authentication info to context  
        SecurityContextHolder.getContext().setAuthentication(authentication); 

        // During the authentification the AppUser instance was set as 
        // details, for more info about the user 
        AppUser user = (AppUser) authentication.getDetails();       

        if (user != null) { 
         // Switch the view after succesfull login 
         getApplication().getMainWindow().setContent(new ComboBoxUserStartsWith()); 

        } 
       } catch (AuthenticationException e) { 
        // Display error occured during logining 
        getWindow().showNotification(e.getMessage(), Notification.TYPE_ERROR_MESSAGE); 
       } 
      } 
     } 
    }); 
+0

是的,但是您并未使用Spring Security过滤器链,对吗? – Xorty 2012-08-22 14:23:44

+0

是的,我不使用它们。实际上,我的安全知识表明,如果Vaadin应用程序通过一个URL(例如host:port/vaadinapp)工作,并且Vaadin内部的不同视图在不重定向到其他URL的情况下发生更改。这种安全性就足够了。可能我错了,如果你指出可能导致的漏洞,我会很高兴。 – orangegiraffa 2012-08-23 12:45:11

+0

是的,它通常是足够的,但我正在将我们的Vaadin应用程序与另一个应用程序集成在一起,并且安全性将由Spring Security为它们两者处理 – Xorty 2012-08-23 12:55:41

相关问题