无状态应用程序 Spring Security

Mar*_*nis 6 java spring spring-security spring-boot

我正在使用 Spring Web、Spring Security 和 Spring Social 创建一个 Spring boot 应用程序。该应用程序包含利用基本身份验证来确保安全的其余服务。我正在尝试配置 Spring 以使应用程序无状态,但是当我使用浏览器向 Web 服务发出请求时,浏览器会提示输入用户凭据,但由于会话创建,所有先前的请求都使用相同的用户凭据。我已经配置了应用程序阻止这种情况发生,但仍然存在问题。\

public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @override
    protected void configure(HttpSecurity http) throws Exception{
        http.sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/api/**")
            .hasRole("USER")
            .andBasic();
    }

    @override
    protected void configure(AuthenticatioinManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("some@gmail.com")
            .password("12345")
            .role("USER");
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该更改或添加什么才能获得此功能。

小智 -1

看这个:

    http
  .sessionManagement()
  .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  .and()
  .csrf().disable()
  .authorizeRequests()
  .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() //allow CORS option calls
  .antMatchers("/resources/**").permitAll()
  .anyRequest().authenticated()
  .and()
  .httpBasic();
Run Code Online (Sandbox Code Playgroud)

  • 欢迎来到 StackOverflow!在答案中解释您的代码可能对未来的读者或有类似问题的人更有帮助。 (12认同)