如何成功注销



我的登录页面是http://localhost:8080/nradmin/welcome我的主页(成功登录后(是http://localhost:8080/nradmin/home

我想注销,再也不能访问任何其他页面,比如主页,只能访问欢迎(登录(页面。如果我键入http://localhost:8080/nradmin/home在浏览器上注销后,我可以继续访问主页而无需登录。

我已经实现了从抽象类WebSecurityConfigurerAdapter中重写的以下方法:

@Override
protected void configure(HttpSecurity http) throws Exception {       http
.authorizeRequests()
.antMatchers("/welcome").permitAll()
.antMatchers("/home", "/account/**", "/price/**").hasRole("ADMIN")
.antMatchers("/home", "/account/**", "/price/**").access("hasRole('ROLE_ADMIN')")
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/welcome")  
.defaultSuccessUrl("/home")
.and()
.logout()
.invalidateHttpSession(true)      
.clearAuthentication(true)
.logoutSuccessUrl("/welcome")    
.logoutUrl("/welcome")          
.deleteCookies()
.permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/welcome")           
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
}
@Override   
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {        
auth.inMemoryAuthentication()           .withUser("username").password(passwordEncoder().encode("12345")).roles(LoginDataConstants.ROLE_ADMIN)
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

有人知道怎么做吗?

我想这可能是由于方法logoutUrllogoutSuccessUrl中有相同的url。

我建议您将logoutUrl更改为其他路径,例如仅更改为/logout,然后尝试通过该url执行注销。它应该完成注销任务并重定向到logoutSuccessUrl,即/welcome

添加CSRF

参考春季文档。

将使用URL执行注销/使用任何HTTP方法请求注销

@Override protected void configure(HttpSecurity http) throws Exception { 
http.
logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")); 
} 
}

你也可以在这里引用一个关于使用相同方法的堆栈溢出的好例子

当我遇到这个问题时,我使用了这条线

.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login");

何处

.logoutSuccessUrl("/login"(是用户将重定向到的页面。

您可以将您的代码重新考虑为类似的内容

@Override
protected void configure(HttpSecurity http) throws Exception {       http
.authorizeRequests()
.antMatchers("/welcome").permitAll()
.antMatchers("/home", "/account/**", "/price/**").hasRole("ADMIN")
.antMatchers("/home", "/account/**", "/price/**").access("hasRole('ROLE_ADMIN')")
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/welcome")  
.defaultSuccessUrl("/home")
.and()
.logout()
.and() // This section is re-factored.
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/welcome").deleteCookies("JSESSIONID") //Or try .logoutSuccessUrl("/nradmin/welcome") 
.invalidateHttpSession(true)
.permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/welcome")           
.and()
.sessionManagement()
.sessio hinCreationPolicy(SessionCreationPolicy.IF_REQUIRED);

更新:这是一个修改版本。这基本上是我在我的项目中使用的,除了这部分:

.and()
.exceptionHandling()
.accessDeniedPage("/welcome")
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);

所以,我认为它应该起作用;否则,我认为这可能是您项目中其他地方的设置问题/配置。请尝试下面的例子。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/nradmin/welcome", "/home", "/account/**", "/price/**").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/nradmin/welcome")
.permitAll()
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/nradmin/welcome").deleteCookies("JSESSIONID") 
.invalidateHttpSession(true)
.permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/nradmin/welcome")
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
}

最新更新