【问题标题】:Logout in Spring Boot with Vaadin not possible. Why?无法使用 Vaadin 在 Spring Boot 中注销。为什么?
【发布时间】:2019-01-23 10:07:46
【问题描述】:

我创建了一个简单的 Spring Boot 应用程序。当我在 pom.xml 中添加 vaadin 依赖项时。注销是不可能的。用户仍处于登录状态,无需输入密码。

我对 Vaadin 10 和 Vaadin 12 得到相同的结果。春季版本是 2.1.2.RELEASE

@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class TestApplication {  
    @RequestMapping("/")
    String home() {return "Hello World!";}
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}    

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().and().
        .authorizeRequests().anyRequest().fullyAuthenticated();
    http.httpBasic().and().logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/")
    .invalidateHttpSession(true).deleteCookies("JSESSIONID");
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
         BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
         auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("thomas")
            .password(encoder.encode("xxx")).roles("USER");
      }
}

Dependeny:
<dependency>
     <groupId>com.vaadin</groupId>
 <artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>

我希望关注结果

我调用 localhost:xxx/ => 浏览器询问用户名/密码

我调用 localhost:xxx/logout => 浏览器再次询问用户名/密码

在我将 vaadin 依赖项添加到 pom 之后

我得到关注结果

我调用 localhost:xxx/ => 浏览器询问用户名/密码

我调用 localhost:xxx/logout => 浏览器不问密码!

【问题讨论】:

    标签: spring spring-boot spring-security vaadin


    【解决方案1】:

    问题是您使用的是基本身份验证,它并不真正支持注销。在每个请求中,浏览器都会发送用户名/密码,因此用户在注销后会立即重新登录。

    解决此问题的一种方法是将身份验证更改为表单,并提供一个带有表单的页面,用户可以在其中提交登录凭据。

    http.formLogin().loginPage("/login").permitAll()
    

    /login 中,您应该提供一个带有&lt;form&gt; 标记的html 页面,用于发布登录凭据,例如:

    <html>
    <head></head>
    <body>
       <h1>Login</h1>
       <form name='f' action="login" method='POST'>
          <table>
             <tr>
                <td>User:</td>
                <td><input type='text' name='username' value=''></td>
             </tr>
             <tr>
                <td>Password:</td>
                <td><input type='password' name='password' /></td>
             </tr>
             <tr>
                <td><input name="submit" type="submit" value="submit" /></td>
             </tr>
          </table>
      </form>
    </body>
    </html>
    

    (取自https://www.baeldung.com/spring-security-login

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 2014-03-01
      • 2016-04-01
      • 2017-08-21
      • 2014-04-16
      • 2023-04-05
      相关资源
      最近更新 更多