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

Spring security Spring Security+;Vaadin:如何创建自定义的非JSP登录表单?,spring-security,vaadin,Spring Security,Vaadin,在SpringSecurity中使用自定义JSP登录页面相当容易。我们的应用程序是基于Vaadin的,我不希望有JSP登录页面。我想要的是作为Vaadin小部件创建的自定义高级登录窗口 从技术上讲,我可以使用Vaadin的FormLayout和名称字段,如j_用户名和j_密码。。。但这是Java类而不是JSP文件,所以我应该在httpSpring安全元素中指定什么?我的意思是: <http auto-config='true'> <intercept-url patte

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

从技术上讲,我可以使用Vaadin的FormLayout和名称字段,如j_用户名j_密码。。。但这是Java类而不是JSP文件,所以我应该在httpSpring安全元素中指定什么?我的意思是:

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

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

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

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

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

我对SpringSecurity完全不熟悉,但是我们Vaadin的一个同事在几年前做了一个演示应用程序。我不知道它是否仍然是最新的,或者它是否回答了你的问题,但也许你可以自己看看,看看是否可以从那里得到任何答案。否则,你可能会亲自打电话给Petter,看看他对这个话题是否有什么新的想法。

我对Spring Security完全不熟悉,但我们Vaadin的一位同事几年前做了一个演示应用程序。我不知道它是否仍然是最新的,或者它是否回答了你的问题,但也许你可以自己看看,看看是否可以从那里得到任何答案。否则,你也许可以亲自去ping Petter,看看他对这个话题是否有什么新的想法。

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

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);
                }
            }
        }
    });

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

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);
                }
            }
        }
    });

好的,这似乎有效,但这足够了吗?Spring安全过滤器是否可以正常使用这种方法,或者是否还有其他事情要做?不,过滤器将不会被调用,但您始终可以自己调用所需的配置,例如,要应用会话策略,请使用sessionAuthenticationStrategy.onAuthentication(身份验证、请求、null)在我的情况下,这不是很令人满意。我会尽快在这里打开“赏金”的。好吧,这似乎有效,但这足够了吗?Spring安全过滤器是否可以正常使用这种方法,或者是否还有其他事情要做?不,过滤器将不会被调用,但您始终可以自己调用所需的配置,例如,要应用会话策略,请使用sessionAuthenticationStrategy.onAuthentication(身份验证、请求、null)在我的情况下,这不是很令人满意。我会尽快在这里打开悬赏。谢谢乔纳斯,这是@OlegYch建议的更复杂的例子。它将AuthenticationManager注入到Vaadin类中,但并没有真正使用spring的过滤器链感谢Jonas,这是@OlegYch建议的更复杂的示例。它将AuthenticationManager注入到Vaadin类中,但实际上没有使用spring的筛选器链是的,但您没有使用spring安全筛选器链,是吗?是的,我没有使用它们。实际上,我在安全方面的知识表明,如果Vaadin应用程序通过一个URL(例如主机:port/vaadinapp)工作,并且在Vaadin内部更改了不同的视图,而没有重定向到其他URL。这样的安全就足够了。可能是我错了,如果你指出可能导致的漏洞,我会很高兴。是的,这通常就足够了,但我正在将我们的Vaadin应用程序与另一个应用程序集成,Spring security将为这两个应用程序处理安全问题,我很熟悉。在我的战争中有两种应用。其中一个主机是:port/vaadinapp,正如我提到的,我使用SpringSecurity。通过另一个URL主机:port/mvcapp,我有一个SpringMVC应用程序。我在应用程序的不同部分使用不同的访问角色是的,但您没有使用Spring Security filter chain,是吗?是的,我没有使用它们。实际上,我在安全方面的知识表明,如果Vaadin应用程序通过一个URL(例如主机:port/vaadinapp)工作,并且在Vaadin内部更改了不同的视图,而没有重定向到其他URL。这样的安全就足够了。可能是我错了,如果你指出可能导致的漏洞,我会很高兴。是的,这通常就足够了,但我正在将我们的Vaadin应用程序与另一个应用程序集成,Spring security将为这两个应用程序处理安全问题,我很熟悉。在我的战争中有两种应用。其中一个主机是:port/vaadinapp,正如我提到的,我使用SpringSecurity。通过另一个URL主机:port/mvcapp,我有一个SpringMVC应用程序。在这里,我使用不同的角色访问应用程序的不同部分