当前位置: 首页 > 知识库问答 >
问题:

Spring Security Vaadin:如何创建自定义的非JSP登录表单?

谭新知
2023-03-14

在Spring Security中使用自定义JSP登录页面相当容易。不过,我们的应用程序是基于Vaadin的,我不想使用JSP登录页面。我想要的是作为Vaadin小部件创建的自定义高级登录窗口。

从技术上讲,我可以使用Vaadin的FormLayout和名称字段,比如j_用户名和j_密码……但这是Java类,而不是JSP文件,那么我在http Spring Security元素中指定了什么?我的意思是:

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

共有3个答案

彭梓
2023-03-14

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

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);
                }
            }
        }
    });
宦博超
2023-03-14

使用LoginForm,在LoginListener中使用如下内容

try {
  val authentication = new UsernamePasswordAuthenticationToken(name, pass)
  SecurityContextHolder.getContext.setAuthentication(authenticationManager.authenticate(authentication))
} catch {
  case e: AuthenticationException => {
    SecurityContextHolder.clearContext()
  }
}
子车心思
2023-03-14

我对Spring Security完全不熟悉,但我们Vaadin的一个员工几年前做了一个演示应用程序https://github.com/peholmst/SpringSecurityDemo.我不知道它是否仍然是最新的,或者它是否回答了你的问题,但如果你能从那里得到任何答案,也许你可以自己看看。否则你也许可以亲自去平·佩特那里,看看他对这个话题有没有什么新的想法。

 类似资料:
  • 我有两个不同的网站。一个是非wordpress的网站,另一个是基于wordpress框架制作的博客。如果您的登录信息有效,是否可以设置一个非wordpress站点的登录表单,将您重定向到wordpress博客仪表板? 我尝试在我的网站中创建一个表单,并将操作设置为我的博客wp-login.php,但它没有将我重定向到仪表板,而是显示主wordpress登录窗口,但我已经登录了。

  • 我学习了spring教程“SSOwith OAuth2:Angular JS和spring Security Part V”。 2016-05-18 11:14:53.667 DEBUG 22312---[nio-9999-exec-9]O.security.web.filterchainproxy:/login位于附加筛选器链中14的位置5;正在激发筛选器:“logoutfilter”2016-

  • 问题内容: 我正在http://www.cafeaulait.org/javafaq.html上阅读#6.10项,然后我开始怀疑大型企业如何创建自己的JVM实现。一个人会尝试(或可行)实验性的东西吗? 问题答案: 从技术上讲,创建该新JVM所需的所有信息都是该语言和目标平台的公共规范。即使字节码解释在很大程度上相同,JVM还是需要根据其是要在台式机还是手机上运行而有所不同。 一些开始寻找信息的地方

  • 本文向大家介绍Android如何创建自定义ActionBar,包括了Android如何创建自定义ActionBar的使用技巧和注意事项,需要的朋友参考一下 当多个界面都有很多相似部分时,可以考虑创建一个功能较全的模板。而在需要时,可以通过引用模板来实现自己想要实现的功能。比如适配器 Adapter,当很多的适配器都差不多时,就可以通过打造一个通用的适配器来实现。本例中主要是如何创建自定义的 Act

  • 标题说明了一切。我想创建一个自定义的prestashop页面,但我不知道如何创建。我真正想做的是:创建一个按钮,打开一个自定义页面。我在网上找不到任何有用的东西,所以我来这里寻求帮助。有人能告诉我怎么做吗?

  • “入门Spring Security and Angular JS系列”中的oauth2 JWT项目有一个自定义登录名。为自定义登录页面添加相同的代码到oauth2-vanilla项目失败,因为登录响应中的授权代码始终为空。我还尝试将Sparklr2(https://github.com/spring-projects/spring-security-oauth/tree/master/sampl