【问题标题】:Spring security vaadin Authetication manager beanSpring security vaadin 身份验证管理器 bean
【发布时间】:2012-12-31 16:09:46
【问题描述】:

我是 Vaadin 和 Spring 安全的新手。最近几天,我在谷歌上搜索如何将 Spring Security 集成到 vaadin 应用程序(而不是 servlet)中的正确示例,以便用户可以使用 LoginForm vaadin 组件进行身份验证。这是我在 onlogin 事件中使用的代码 sn-p:

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

当我尝试使用我在 applicationContext.xml 文件中描述的凭据登录时,我在此行收到错误空指针异常:

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

我知道错误是由 getBean("authenticationManager");方法,因为它找不到 "**authenticationManager"** bean。问题是:在这种情况下,"**authenticationManager"** 是什么。它是一个类还是 bean,它用特殊的方法实现了某种 Spring 安全框架接口。有没有人可以提供这样的bean的例子(类,pojo等)。我在 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>

我还提供了一个 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>

总之。有没有人可以为我提供一个工作 bean 示例,就我而言,它可以验证 applicationContext.xml 文件中描述的普通用户名和密码。如果它有帮助,我正在使用 Vaadin 6、Maven、Hibernate、GlassFish 和 Sping 安全 3。我将非常感谢帮助,因为我在过去三天一直在处理这个问题。

网上有很多例子,但都是未完成的,不清楚的,使用 Vaadin 应用程序 serverlts 或 jsp 登录表单(不是应用程序)和不同的技术。

我选择了官方 vaadin wiki https://vaadin.com/wiki/-/wiki/Main/Spring%20Integration?p%255Fr%255Fp%255F185834411%255Ftitle=Spring%2520Integration 中描述的方式。但它太差了,描述的是 Spring 框架集成而不是安全性。

我发现了另一个看起来很完美的例子 Spring Security + Vaadin: How to create custom non-JSP login form? 。但在那里我找不到关于 authenticationManager 的详细信息。

这里还有另一个很好的例子https://vaadin.com/forum/-/message_boards/view_message/373038。但它使用 HttpServletRequest 和 HttpServletResponse 来传递身份验证详细信息。我知道 vaadin 应用程序类可以使用 onRequestStart 和 onRequestEnd 方法实现 HttpServletRequestListener 接口,但是我如何将这些请求传递/使用到/使用 onLogin 事件。

所以亲爱的JAVA高手请帮助新手JAVA JEDI程序员选择正确的方式:)

Spring 上下文帮助器类

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

【问题讨论】:

    标签: java spring-security vaadin


    【解决方案1】:

    尝试使用 org.springframework.security.authentication.AuthenticationManager 接口代替 ProviderManager

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

    编辑。 在您的配置文件中将 alias="authenticationManager" 替换为 id="authenticationManager。

    【讨论】:

    • 未提供名为 "authenticationManager" 的 bean。从这个错误中我看到我必须创建 authenticationManager bean。但我不知道如何实现这样的bean。它应该使用什么样的接口和方法?
    • 它由 Spring Security 提供。查看您的配置文件:authentication-manager alias="authenticationManager"。通常您不需要自定义实现。 Spring Security 为您提供默认的一个(ProviderManager)。
    • 您需要将 alias="authenticationManager" 替换为 id="authenticationManager"。我认为它会成功。
    • 我得到了同样的错误:((没有提供名为“authenticationManager”的bean)。也许我必须在pom文件中包含一些包。但我已经将所有相关的Spring-security包添加到pom。
    • 我也把别名改成了id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多