【问题标题】:Spring Security mapping uppercase in URLURL中的Spring Security映射大写
【发布时间】:2017-03-24 21:29:55
【问题描述】:

我在一个 JEE 项目上工作,使用 Spring Boot 框架。 对于身份验证,我使用 Spring Security,并在模板中指定了页面。

protected void configure(HttpSecurity http) throws Exception {  
    http
        .authorizeRequests()
            .antMatchers("/login").permitAll()
            .antMatchers("/token", "/index", "/index.html", "/main", "/main.html", "/main2", "/main2.html", "/recent1", "/recent1.html", "/recent2", "/recent2.html").hasRole("USER");

    http
        .csrf()
            .disable()
        .formLogin()
            .loginPage("/login")
            .failureUrl("/login?error=true")
            .defaultSuccessUrl("/index");

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

问题是,当我运行应用程序并使用大写字母(例如:localhost:8080/INDEX.HTML 或添加两个字母 localhost/index.httml)编写 URL 时,页面确实会出现而没有经过身份验证。

【问题讨论】:

  • 你可以用这个解决方案 .antMatchers("/login","/LOGIN")。另一个像这样。

标签: spring url spring-boot spring-security


【解决方案1】:

如果我理解正确,以下是所需的逻辑:

  1. /login 不需要 Spring Security 保护(不需要角色)
  2. 必须使用“USER”角色保护所有其他页面。

要实现这一点,您可以尝试以下方法:

@Override
public void configure(WebSecurity web) throws Exception {
    // configuring here URLs for which security filters
    // will be disabled (this is equivalent to using
    // security="none")
    web
        .ignoring()
            .antMatchers(
                    "/login"
            )
    ;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().hasRole("USER");

    http.csrf().disable().formLogin()
            .loginPage("/login").failureUrl("/login?error=true")
            .defaultSuccessUrl("/index");

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

所以第 1 项(/login 上没有安全性)被移动到configure(WebSecurity),第 2 项留在原来的configure(HttpSecurity) 中。

【讨论】:

  • 感谢您的回复,是的,所有页面都是安全的,问题是当我输入/INDEX(大写字母)时,登录没有出现,它直接调用索引而不进行身份验证!跨度>
  • 在您的配置中列出所有页面(包括/index/index.html)并为它们设置安全属性。但是您没有为“所有其他”案例设置任何属性。 INDEX.HTMLindex.httml/index 匹配器不匹配,因此不会为它们应用任何安全性。我的回答中的anyRequest() 配置指示 Spring Security 要求对 所有 URL(但 /login)进行身份验证。
猜你喜欢
  • 2016-02-16
  • 2011-08-09
  • 2016-06-25
  • 2012-12-23
  • 2012-03-29
  • 2018-02-08
  • 2013-05-24
  • 2016-05-17
  • 2016-01-02
相关资源
最近更新 更多