【发布时间】: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