首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么/login?logout要重定向到/login?

为什么/login?logout要重定向到/login?
EN

Stack Overflow用户
提问于 2015-10-16 09:58:58
回答 1查看 3.3K关注 0票数 11

在我的Spring项目中,我将注销目标url设置为"/login?logout“,以显示登录页面,并带有”您现在注销“的消息。

在Security配置中,我执行了以下操作:

代码语言:javascript
复制
@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/error").permitAll()
            .anyRequest().fullyAuthenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .successHandler(loginSuccessHandler)
            .failureUrl("/login?error")
            .and()
            .httpBasic()
            .and()
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .permitAll()
            .logoutSuccessHandler(logoutSuccessHandler);
}

logoutSuccessHandler:

代码语言:javascript
复制
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    if (authentication != null) {
        Log.debug(authentication.getName() + " LOGOUT !!");
    }

    setDefaultTargetUrl("/login?logout");
    super.onLogoutSuccess(request, response, authentication);       
}

当我尝试注销时,我到达页面"/login“(没有?注销)。我不明白为什么它会在这页上重定向我。

我认为应用程序试图在"/login?logout“上重定向我,但是由于我不再连接,security希望我再次登录。

当我登录时尝试访问"/login?logout“页面时,它会显示好的页面。

通过添加以下内容,我找到了解决这个问题的方法:

代码语言:javascript
复制
            .authorizeRequests()
            .antMatchers("/error","/login").permitAll()

为什么loginPage("/login").permitAll()不这么做?我做错什么了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-26 01:14:00

为什么 loginPage("/login").permitAll() 不允许访问

因为当您在permitAll上执行FormLoginConfigurer或其他大多数配置程序时,它只允许访问这些exact URLs

那么,为什么 authorizeRequests().antMatchers("/login").permitAll() 允许访问?

因为这使用了一个AntPathRequestMatcher ( matches on the request path only )和path does not contain the query string

,但我知道我已经看到了允许我访问 /login?logout 而不需要任何显式 permitAll 的代码。怎么回事?

Security喜欢提供“合理”的默认设置,如果没有指定默认登录和注销页面,它认为提供默认登录和注销页面是“明智的”。默认的注销页面是/login?logout,所以如果您不指定任何内容,就可以使用它。这是由一个自动生成一些DefaultLoginPageGeneratingFiltershort-circuits URL authorizationshort-circuits URL authorization完成的.

那么,当我指定一个logoutSuccessHandler时,为什么不能访问默认的 /login?logout 页面呢?

当您指定自己的logoutSuccessHandlerlogoutSuccessUrl时,Security假设您正在提供自己的注销视图,因此它不会在注销页面上将DefaultLoginPageGeneratingFilter初始化为短路URL授权,并希望您自己在自己的视图上配置授权。

,但是我想保留默认的注销页面。我只想添加一些定制的额外处理。我就不能这么做吗?

如果您想指定自己的logoutSuccessHandler,但仍然保留默认的/login?logout视图,则必须告诉DefaultLoginPageGeneratingFilter继续提供它。您可以使用自定义SecurityConfigurer完成此操作,如下所示:

代码语言:javascript
复制
.logoutSuccessHandler(logoutSuccessHandler)
.and()
.apply(new SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity>() {
    @Override public void configure(HttpSecurity builder) throws Exception {
        builder.getSharedObject(DefaultLoginPageGeneratingFilter.class).setLogoutSuccessUrl("/login?logout");
    }
})
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33167609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档