简体   繁体   English

Spring Security Vaadin认证管理器bean

[英]Spring security vaadin Authetication manager bean

I am new to Vaadin and Spring security. 我是Vaadin和Spring安全人员的新手。 Last few days I was googling for the right example how to integrate spring security into vaadin application (not the servlet) so that the users can authenticate themselves using LoginForm vaadin component. 最近几天,我一直在寻找正确的示例,说明如何将Spring Security集成到vaadin应用程序(而不是servlet)中,以便用户可以使用LoginForm vaadin组件进行身份验证。 Here is the code snippet that I used inside onlogin event: 这是我在onlogin事件中使用的代码段:

login = new LoginForm();
login.addListener(new LoginForm.LoginListener() {
    private static final long serialVersionUID = 1L;

    @Override
    public void onLogin(LoginEvent event) {
        if (event.getLoginParameter("username").isEmpty() || event.getLoginParameter("password").isEmpty()) {
            getWindow().showNotification("Please enter username and/or password", Notification.TYPE_ERROR_MESSAGE);
        } else {
            SpringContextHelper helper = new SpringContextHelper(getApplication());
            authenticationManager = (ProviderManager)helper.getBean("authenticationManager");

            try {
                UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(event.getLoginParameter("username"), event.getLoginParameter("password"));   
                Authentication authentication = authenticationManager.authenticate(token);
                SecurityContextHolder.getContext().setAuthentication(authentication);
                authentication.getDetails();
            } catch (Exception e) {
                getWindow().showNotification(e.getMessage(), Notification.TYPE_ERROR_MESSAGE);
            }
        }                   
    }
});

When I try to login with my credentials that I described in applicationContext.xml file I got an error Null Pointer exception at this line: 当我尝试使用在applicationContext.xml文件中描述的凭据登录时,在此行出现错误Null Pointer异常:

 authenticationManager = (ProviderManager)helper.getBean("authenticationManager");

I know that the error is thrown by the getBean("authenticationManager"); 我知道该错误是由getBean(“ authenticationManager”);引发的。 method because it can't locate "**authenticationManager "** bean. 方法,因为它无法找到“ ** authenticationManager ” ** bean。 The question would be: what is the "**authenticationManager "** in this context. 问题是:在这种情况下, “ ** authenticationManager ” **是什么。 Is it a class or bean that implements some kind of Spring security framework interface with special methods. 是使用特殊方法实现某种Spring安全框架接口的类还是bean。 Does anyone could provide an example of such bean (class, pojo and etc.). 是否有人可以提供此类bean的示例(类,pojo等)。 I descriped listeners and context-params in my web.xml as I found on the examples in the internet. 我在互联网上的示例中找到了在web.xml中描述的侦听器和上下文参数。

WEB.XML WEB.XML

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/applicationContext*.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

Also I am providing a applicationContext.xml in which I describe authentication manager with plain username and password. 我还提供了applicationContext.xml,其中用简单的用户名和密码描述了身份验证管理器。

APPLICATIONCONTEXT.XML APPLICATIONCONTEXT.XML

<beans:beans xmlns="http://www.springframework.org/schema/security"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http>
   <intercept-url pattern="/**" access="ROLE_USER" />
   <form-login />
   <logout />
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider>
      <user-service>
        <user name="userrrr" password="passsssword" authorities="ROLE_USER, ROLE_ADMIN" />
        <user name="otheruser" password="otherpasss" authorities="ROLE_USER" />
      </user-service>
    </authentication-provider>
</authentication-manager>

So In short. 总之。 Does anyone could provide me a working bean example, for my case, which can authenticate plain usernames and passwords that are described in applicationContext.xml file. 对于我的情况,是否有人可以为我提供一个有效的bean示例,该示例可以验证applicationContext.xml文件中描述的纯用户名和密码。 If it helps I am using Vaadin 6, Maven, Hibernate, GlassFish and Sping security 3. I will greatly appreciate help because I am working on this issue for the last three days. 如果有帮助,我正在使用Vaadin 6,Maven,Hibernate,GlassFish和Sping安全3。我将非常感谢您的帮助,因为在过去三天中,我一直在致力于解决此问题。

There are plenty examples on the internet but they are all unfinished, unclear, uses Vaadin application servelts or jsp login forms (not the application) and different techniques. 互联网上有很多示例,但是它们都未完成,不清楚,使用Vaadin应用程序服务或JSP登录表单(而不是应用程序)以及不同的技术。

I choosed the way that is described in official vaadin wiki https://vaadin.com/wiki/-/wiki/Main/Spring%20Integration?p%255Fr%255Fp%255F185834411%255Ftitle=Spring%2520Integration . 我选择了官方vaadin Wiki https://vaadin.com/wiki/-/wiki/Main/Spring%20Integration?p%255Fr%255Fp%255F185834411%255Ftitle=Spring%2520Integration中描述的方式。 But it is so poor and describes spring framework integration not security. 但这太糟糕了,它描述了Spring框架集成而不是安全性。

I found another example which looked me perfect Spring Security + Vaadin: How to create custom non-JSP login form? 我找到了另一个看起来对我来说很完美的示例Spring Security + Vaadin:如何创建自定义的非JSP登录表单? . But there I can't find detailed info about authenticationManager. 但是我找不到有关authenticationManager的详细信息。

Also here is another nice example https://vaadin.com/forum/-/message_boards/view_message/373038 . 这也是另一个不错的示例https://vaadin.com/forum/-/message_boards/view_message/373038 But it is using HttpServletRequest and HttpServletResponse for passing authetication details. 但是它使用HttpServletRequest和HttpServletResponse传递身份验证详细信息。 I know that vaadin application class can implement HttpServletRequestListener interface with onRequestStart and onRequestEnd methods but how I can pass/use these requests to/with onLogin event. 我知道vaadin应用程序类可以使用onRequestStart和onRequestEnd方法实现HttpServletRequestListener接口,但是我如何将这些请求传递给onLogin事件或将其用于onLogin事件。

So dear JAVA masters please help for a newbie JAVA JEDI programmer to choose a right way:) 因此,亲爱的JAVA大师请帮助新手JAVA JEDI程序员选择正确的方法:)

Spring Context helper class Spring Context助手类

public class SpringContextHelper {  
    private ApplicationContext context;

    public SpringContextHelper(Application application) {
        ServletContext servletContext = ((WebApplicationContext) application.getContext()).getHttpSession().getServletContext();
        context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    }

    public Object getBean(final String beanRef) {
        return context.getBean(beanRef);
    }    
}

Try org.springframework.security.authentication. 尝试org.springframework.security.authentication。 AuthenticationManager interface instead of ProviderManager : AuthenticationManager接口,而不是ProviderManager

authenticationManager = (AuthenticationManager)helper.getBean("authenticationManager");

EDIT. 编辑。 Replace alias="authenticationManager" by id="authenticationManager in your conf. 在您的conf中,将alias =“ authenticationManager”替换为id =“ authenticationManager。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM