java - Why Doesn't Login Page In Vaadin Show Up When I'm Using Spring Security
问题描述
I have setup a basic Spring security configuration:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password(passwordEncoder().encode("user1Pass")).roles("USER")
.and()
.withUser("user2").password(passwordEncoder().encode("user2Pass")).roles("USER")
.and()
.withUser("admin").password(passwordEncoder().encode("adminPass")).roles("ADMIN");
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/**").hasAnyRole("USER", "ADMIN")
.antMatchers("/login*").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
And a login page as well:
@Viewport("width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes, viewport-fit=cover")
@Route("login")
@PageTitle("Login")
public class Login extends VerticalLayout {
/**
*
*/
private static final long serialVersionUID = 1L;
public Login(){
LoginOverlay login = new LoginOverlay(); //
login.setAction("login"); //
login.setOpened(true); //
login.setTitle("Spring Secured Vaadin");
login.setDescription("Login Overlay Example");
add(login);
}
}
But when I access localhost:8080
without logging in, then Spring will navigate me to localhost:8080/login
.
The problem is that I'm expecting Vaadin's built in standard login layout. But nothing is appearing. Just a white empty page. Have I forgot something?
解决方案
configure(WebSecurity web)
发生这种情况是因为您忘记覆盖SecurityConfiguration
类中的方法,如教程为 Vaadin 应用程序设置 Spring Security 中所述:
/**
* Allows access to static resources, bypassing Spring security.
*/
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(
// Vaadin Flow static resources //
"/VAADIN/**",
// the standard favicon URI
"/favicon.ico",
// the robots exclusion standard
"/robots.txt",
// web application manifest //
"/manifest.webmanifest",
"/sw.js",
"/offline-page.html",
// (development mode) static resources //
"/frontend/**",
// (development mode) webjars //
"/webjars/**",
// (production mode) static resources //
"/frontend-es5/**", "/frontend-es6/**");
}
我建议您通读整个教程系列,也许您也忽略了其他内容。
推荐阅读
- python - PyOpenGLUT 问题
- c++ - 如何在cpp文件之间切换窗口?在 Qt 中
- c# - EF Core - 无法插入具有外键的新条目
- npm - ckeditor-duplicated-modules 在本地测试 CKeditor 5 插件时出错
- python - 在 python 中,我将如何创建文本文件中列出的所有唯一单词的字典。到目前为止,我有这个代码。谢谢
- python - discord.py - FFmpegPCMAudio 使用大量 CPU
- swift - 根据开始日期和给定周数获取周范围
- html - 如何在不使用图像的情况下添加看起来呈波浪状的分区?
- amazon-web-services - (ACM) 通过 DNS 配置验证证书
- javascript - 从json数据中获取php页面中的数据