之前的博客中我给过如何在springboot中整合security,当时写的界面很简单,没有CSS样式,更谈不上静态资源,而现在在实际开发过程中经理让我们用security来开发,界面肯定不可能就是两个输入框,一个按钮就完事啊,当加上CSS样式的时候问题就来了,首先是CSS样式没办法被加载,其次登录之后跳转的路径出错,随机跳转到一个CSS文件中,这让我很烦恼,查了很多资料,也问了很多前辈之后终于解决了这个问题,下面我给出具体的代码
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsServiceImpl uds;
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login","/css/**","/image/*").permitAll()
.anyRequest().authenticated().and().formLogin()
.loginPage("/login").defaultSuccessUrl("/index").permitAll().and().logout().permitAll();
}
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(uds);
}
}
上面给出了不被拦截的一些静态资源的路径 **表示可以跨文件夹

在SpringBoot项目中使用Security进行权限管理时,遇到静态资源(如CSS)被拦截的问题,导致页面样式无法正常加载和登录后路径错误。通过查阅资料和请教前辈,了解到需要配置WebSecurityConfig以允许静态资源不受拦截。代码中展示了@Configuration、@EnableWebSecurity和@EnableGlobalMethodSecurity注解的使用,并通过WebSecurityConfigurerAdapter扩展配置了静态资源路径。
最低0.47元/天 解锁文章
2990

被折叠的 条评论
为什么被折叠?



