其他分享
首页 > 其他分享> > 6.登出操作的配置

6.登出操作的配置

作者:互联网

1.配置内容

protected void configure(HttpSecurity http) throws Exception {
    http.logout().logoutUrl("/logout").logoutSuccessUrl("/hello").permitAll();

    http.formLogin() //表单登录
            .loginPage("/login.html") //登录页面
            .loginProcessingUrl("/user/login") //登录访问路径,这个路径默认不需要认证
            .defaultSuccessUrl("/success.html").permitAll()  //默认跳转路径,
            .and().authorizeRequests()
                .antMatchers("/hello").permitAll() //设置哪些路径可以直接访问  permitAll这个不加还报错
                //.antMatchers("/needAuth").hasAuthority("auth")
                //.antMatchers("/needAuth").hasAnyAuthority("auth","auth2")
                //.antMatchers("/needRole").hasRole("admin")
                //.antMatchers("/needRole").hasAnyRole("admin","admin2")
            .anyRequest().authenticated() //剩下的请求都要认证.这个配置不加的话,所有路径都可以访问
            .and().csrf().disable();

  }

2.success.html内容

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  登录成功
  <a href="/logout">退出</a>
</body>
</html>

  

重要说明:

1)配置里面两个url(loginProcessingUrl(),logoutUrl()),在我这里对应的是 /user/login和 /logout 这两个接口在系统里是不需要创建的,可能框架自己帮我们创建了,并且在里面实现了真正的登录和登出操作

2)defaultSuccessUrl() 这个配置是这样的

-1)如果我们访问的是具体的资源,比如/myPoint接口,那么登录成功后直接访问具体接口

-2)如果我们访问的是登录页面,比如http://localhost:8001/login.html,那么登录成功后就访问它了

标签:permitAll,登录,登出,配置,antMatchers,访问,html,操作,login
来源: https://www.cnblogs.com/johnzhao/p/14527330.html