在Spring Security中使用自定义JSP登录页面相当容易。我们的应用程序基于Vaadin,但我不想拥有JSP登录页面。我想要的是创建为Vaadin小部件的自定义花式登录窗口。
技术上我可以使用Vaadin的 FormLayout 和名称字段,如 j_username 和 j_password ...但这是Java类而不是JSP文件,那么我在 http Spring Security元素中指定了什么?我的意思是:
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page='MyLoginWindow.java or what?' />
</http>
答案 0 :(得分:1)
使用LoginForm并在LoginListener中使用类似
的内容try {
val authentication = new UsernamePasswordAuthenticationToken(name, pass)
SecurityContextHolder.getContext.setAuthentication(authenticationManager.authenticate(authentication))
} catch {
case e: AuthenticationException => {
SecurityContextHolder.clearContext()
}
}
答案 1 :(得分:1)
我对Spring Security完全不熟悉,但几年前我们Vaadin的一个人做了一个演示应用程序https://github.com/peholmst/SpringSecurityDemo。我不知道它是否仍然是最新的,或者它甚至可以回答你的问题,但也许你可以看一看,如果你可以从那里得到任何答案。否则你可能会亲自判断Petter,看看他是否对这个话题有任何新意见。
答案 2 :(得分:1)
请参阅下面我的方法。该代码显示单击登录按钮时发生的功能。
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);
}
}
}
});