Spring Security映射URL中的大写

时间:2017-03-24 21:29:55

标签: spring url spring-boot spring-security

我使用Spring Boot框架处理JEE项目。 对于身份验证,我使用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");
}

问题在于,当我运行应用程序并使用大写字母编写URL时:localhost:8080/INDEX.HTML或者我添加了两个字母localhost/index.httml,页面确实显示没有自动识别。

1 个答案:

答案 0 :(得分:-1)

如果我理解正确,这是理想的逻辑:

  1. /login不需要由Spring Security保护(不需要任何角色)
  2. 所有其他页面必须使用" USER"作用。
  3. 要实现此目的,您可以尝试以下方法:

    @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)中。