我正在 Spring Security 中配置端点访问。我想要实现的目标:
- 每个人都可以访问资源
- 每个人都可以登录/注册
- 只有经过身份验证的用户才能访问注销和所有其他映射端点
这是我的配置,它满足前两个要求并阻止未登录用户访问/logout。
http.authorizeRequests()
.antMatchers("/register").permitAll()
.antMatchers("/register/*").permitAll()
.antMatchers("/favicon.ico").permitAll()
.antMatchers("**/*.html").permitAll()
.antMatchers("**/*.css").permitAll()
.antMatchers("**/*.js").permitAll()
.and()
.formLogin().loginPage("/login").failureUrl("/login-error").defaultSuccessUrl("/")
.usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutSuccessUrl("/login").deleteCookies("JSESSIONID").logoutUrl("/logout");
最佳答案
确保对您的应用程序的请求需要用户进行身份验证使用 .anyRequest().authenticated()
关于java - Spring Security配置访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42935208/