【发布时间】:2019-12-24 12:47:39
【问题描述】:
@Route 在 Vaadin 中创建的视图很少,现在我想添加安全性和一些登录。在我的SecurityConfiguration 类中,我将antMatchers.permitAll() 设置为仅用于2 个视图,其余设置为角色ADMIN。但它并没有像我认为的那样工作。它需要登录才能访问每个视图,并且在登录后,无论用户具有什么角色,我都可以访问所有视图。
我希望本教程对我有所帮助,但是没有登录就无法访问视图。
Securing Your App With Spring Security
我的配置类:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private UserService userService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public SecurityConfiguration(UserService userService) {
this.userService = userService;
}
@Autowired
private void configureAuth(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
auth.inMemoryAuthentication()
.withUser("user")
.password(passwordEncoder().encode("user"))
.roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and()
.anonymous()
.and()
.authorizeRequests()
.antMatchers("/", "/login").permitAll()
.antMatchers("/recipe-manager", "/ingredient-manager").hasAnyRole("ADMIN")
.and()
.formLogin().loginPage("/login").permitAll()
.and()
.logout().logoutSuccessUrl("/")
.and()
.csrf().disable().cors().disable().headers().disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(
"/VAADIN/**",
"/favicon.ico",
"/robots.txt",
"/manifest.webmanifest",
"/sw.js",
"/offline-page.html",
"/icons/**",
"/images/**",
"/frontend/**",
"/webjars/**",
"/h2-console/**",
"/frontend-es5/**", "/frontend-es6/**");
}
}
我的视图有如下注释:
@Route("recipe-manager")
public class RecipeManagerView extends VerticalLayout
@Route("")
public class RecipeBrowserView extends VerticalLayout
@Route("login")
public class LoginView extends VerticalLayout
@Route("ingredient-manager")
public class IngredientManagerView extends VerticalLayout
我希望任何人都可以访问RecipeBrowserView 和LoginView,但只有登录用户才能访问RecipeManagerView 和IngredientMangerView。
【问题讨论】:
-
当您尝试访问 LoginView 时发生了什么?
-
嗯,当我登录时,我被重定向到 LoginView,当我没有登录时,什么都没有 - 我在任何地方
标签: java spring-security vaadin