更新时间:2023-09-20 22:03:46
plz checkout configure
method
@Override公共无效配置(HttpSecurity http)抛出异常{http.authorizeRequests().antMatchers("/resources/**").permitAll().antMatchers("/login*").permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login");}
and implements WebMvcConfigurer
类如下
@Configuration@EnableWebMvc公共类 WebMvcConfiguration 实现 WebMvcConfigurer {@覆盖public void addResourceHandlers(最终ResourceHandlerRegistry注册表){registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");}}
addResourceHandlers
表示在/static 中查找资源.
I have a Spring boot application with Spring security.
My problem is similar to this one, but in my case I want to redirect the user to the login
page if he's not authenticated when he tries to access any page of the application.
The following image shows the architecture of the application:
My config class looks like this:
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/**").hasAnyRole("USER")
.and().formLogin().loginPage("/login").permitAll()
.and().authorizeRequests().antMatchers("/resources/**").permitAll().anyRequest().permitAll();
}
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
}
With this configuration, no resource will be loaded. How can I configure my project to redirect the user to the login page if he's not authenticated and at the same time having my resources folder loaded?
plz checkout configure
method
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.antMatchers("/login*").permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage("/login");
}
and implements WebMvcConfigurer
Class like below
@Configuration
@EnableWebMvc
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}
}
addResourceHandlers
means find resources in /static.