【问题标题】:Spring Security Java Config not generating logout urlSpring Security Java Config 未生成注销 url
【发布时间】:2023-03-03 02:00:01
【问题描述】:

我正在使用 Spring 4.0.5.RELEASE 和 Spring Security 3.2.4

我正在尝试使用 java config(基于 Spring 示例)创建一个简单的示例应用程序。应用程序启动并且身份验证正常工作,即我在访问受保护的 url /settings/profile

时被重定向到登录表单

但是没有生成 /logout url?如果我点击 localhost:8080/logout 我会得到 404。

我在以前的项目中使用过类似的代码,所以可能与版本有关?

这是我的安全配置

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
        auth.inMemoryAuthentication().withUser("admin").password("password").roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/settings/**").hasRole("ROLE_ADMIN")
                    .and()
                .formLogin()
                    .and()
                .logout()
                    .deleteCookies("remove")
                    .invalidateHttpSession(true)
                    .logoutUrl("/logout")
                    .logoutSuccessUrl("/logout-success")
                .permitAll();
    }
}

这是我用来引导应用程序的 WebAppInitializer

 public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { SecurityConfig.class , MvcConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
         return new String[] {"/"};
    }
}

最后是我的 MvcConfig

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"web"})
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

【问题讨论】:

    标签: java spring configuration spring-security spring-java-config


    【解决方案1】:

    默认情况下,注销 url 需要 POST 请求。要对 GET 请求执行注销,您需要:

    http
          .logout()
              .logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
    

    或者如果你想支持PUT或者其他方法,把这个作为参数传递:

    http
          .logout()
              .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "PUT"));
    

    请参阅文档:http://docs.spring.io/spring-security/site/docs/3.2.4.RELEASE/reference/htmlsingle/(第 6.5.3 节。注销)

    【讨论】:

    • 你知道怎么用xml配置吗?
    • 我遇到了类似的问题。我正在执行 POST /appContextRoot/logout 并传递必要的 XSRF-TOKEN 标头,但仍然得到 404。还有更多内容吗?
    • 值得注意的是,这不是最佳实践,因为它会让您面临潜在的 CSRF 攻击。来自 JavaDoc:The URL that triggers log out to occur (default is "/logout"). If CSRF protection is enabled (default), then the request must also be a POST. This means that by default POST "/logout" is required to trigger a log out. If CSRF protection is disabled, then any HTTP method is allowed. It is considered best practice to use an HTTP POST on any action that changes state (i.e. log out) to protect against CSRF attacks. ...
    • 在许多情况下,比如在旧的 JSP 中,使用普通的注销链接会发送 GET 请求,但您可以使用表单作为解决方法,如下所述:stackoverflow.com/questions/6791238/…
    • 更好的是,没有 JavaScript,但将按钮设置为链接:stackoverflow.com/a/22076149/160799
    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 2019-05-09
    • 2015-06-17
    • 2020-04-02
    • 2015-06-30
    • 2017-04-22
    • 2012-10-09
    • 2018-09-02
    相关资源
    最近更新 更多