Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关于基于URL的Spring安全性的说明_Java_Spring_Spring Security - Fatal编程技术网

Java 关于基于URL的Spring安全性的说明

Java 关于基于URL的Spring安全性的说明,java,spring,spring-security,Java,Spring,Spring Security,我有两个REST端点: /noAuth/rest/sayHi /rest/auth/getMsg 我只想登录/rest/auth/getMsg并直接访问/noAuth/rest/sayHi 当我在websecurityConfigureAdapter.configure(HttpSecurity http) @EnableWebSecurity @配置 公共类SpringSecurityConfig扩展了WebSecurity配置适配器{ @凌驾 受保护的无效配置(AuthenticationM

我有两个REST端点:

  • /noAuth/rest/sayHi
  • /rest/auth/getMsg
  • 我只想登录
    /rest/auth/getMsg
    并直接访问
    /noAuth/rest/sayHi

    当我在
    websecurityConfigureAdapter.configure(HttpSecurity http)

    @EnableWebSecurity
    @配置
    公共类SpringSecurityConfig扩展了WebSecurity配置适配器{
    @凌驾
    受保护的无效配置(AuthenticationManagerBuilder auth)引发异常{
    认证
    .inMemoryAuthentication()
    .withUser(“Java技术人员”)
    .密码(“密码”)
    .角色(“管理员”);
    认证
    .inMemoryAuthentication()
    .withUser(“Basant”)
    .密码(“密码2”)
    .角色(“用户”);
    }
    @凌驾
    受保护的无效配置(HttpSecurity http)引发异常{
    http
    .授权请求()
    .antMatchers(“/rest/**”).authenticated()
    .anyRequest().permitAll()
    .及()
    .httpBasic();}
    }
    }
    
    我只在
    /rest/auth/getMsg
    中得到登录提示,但在
    /noAuth/rest/sayHi
    中没有得到,这是预期的

    但当我使用下面的模式时,我会在
    /rest/auth/getMsg
    /noAuth/rest/sayHi
    中得到登录提示,这对我来说是非常意外的

    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .antMatchers("/rest/**").permitAll()
            .and()
        .httpBasic();`
    
    我第二次知道我做错了什么,所以我想了解为什么我不能只登录
    /rest/auth/getMsg
    并直接访问
    /noAuth/rest/sayHi

    更新

    @这是有道理的,但破坏了我对其他情况的理解。假设我使用此模式:

    http
    .授权请求()
    .anyRequest().authenticated()
    .anyRequest().hasRole(“管理员”)
    .及()
    .httpBasic()`
    
    只允许user=“Java Techie”登录,因为它是
    管理员
    并为user=“Basant”抛出
    403禁止

    但是当我使用

    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .antMatchers("/rest/**").hasRole("ADMIN")
            .and()
        .httpBasic();
    

    根据您的解释,它不应该允许user=“Basant”访问
    /rest/auth/getMsg
    ,因为它具有
    role=“user”
    。但实际上,当我使用user=“Basant”时,它允许我访问
    /rest/auth/getMsg

    执行是从左到右进行的。在第一种情况下:.antMatchers(“/rest/”).authenticated(),因此验证与rest/匹配的任何内容


    在2种情况下:.anyRequest().authenticated(),因此任何请求都将在前进之前获得所需的身份验证。

    如果没有其他规则,则默认情况下需要身份验证。您应该添加类似于
    antMatchers(“/noAuth/**”).permitAll()

    的内容,我会尽量使内容简单:http.authorizeRequests().antMatchers(“/rest/**”).access(“hasRole('ADMIN')”;应该有用。不幸的是,我不是springboot专家,所以我尽量让它简洁明了。这一个我认为它很好用。我只是想了解为什么第二个不起作用。谢谢你的时间。非常感谢您的帮助。我添加了“/noAuth/**但仍然得到了/noAuth/rest/sayHi--”http.authorizeRequests().anyRequest().authorized().antMatchers(“/rest/**”).permitAll().antMatchers(“/noAuth/**”).permitAll()和().httpBasic();因此,堆栈溢出要求我为您的答案标记“解决了我的问题”!能给你机会吗??你可以在这里发布你的ans,否则我可以接受答案,不用担心接受答案。可能的重复链接将重定向用户,或者您的问题稍后将被删除。@ASharma7不,您不能接受我在其他问题中的回答。你只能接受自己问题的答案。但你可以投票支持我的答案(我想你已经这么做了)。是的,如果我在你的问题中写下另一个你接受的答案,我会得到额外的代表。但事实并非如此。某些用户正在对同一用户的重复答案进行向下投票。筛选/安全规则的顺序很重要。您首先请求对所有内容进行身份验证,然后进行排除。您应该切换顺序,首先执行所有特定匹配,然后执行更多全局匹配。规则按其定义的顺序进行查询,第一个匹配项是将拾取的匹配项。在这种情况下,请始终进行身份验证。