Spring security Spring security的下一步

Spring security Spring security的下一步,spring-security,Spring Security,我使用的是通过java配置的spring安全性。这是一个非常简单的配置。这个世界应该能够在没有证书的情况下取得成功。我只希望管理员用户从一个特定的子网发布 @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception

我使用的是通过java配置的spring安全性。这是一个非常简单的配置。这个世界应该能够在没有证书的情况下取得成功。我只希望管理员用户从一个特定的子网发布

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers(HttpMethod.POST, "/status", "/configure")
//                .access("hasRole('ROLE_ADMIN')")
                .access("hasIpAddress('192.168.4.0/24')")
            .antMatchers(HttpMethod.GET, "/**")
                .permitAll()
            .anyRequest()
                .denyAll()
            .and()
                .csrf().disable()
            ;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("dashboard.xxxx.yyy.zzz").password("password").roles("ADMIN");
    }
}
hasIpAddress()可以工作。hasRole()没有。当它发生时,我会“和”他们。 我花了三天的时间在谷歌上搜索每一个页面,但没有一个页面能让我成功

我正在使用
curl-u dashboard.xxx.yyy.zzz:password进行测试https://xxx.yyy.zzz:8443/status -d''

注意:我计划最终从x509证书获取用户名,但我想从简单开始。

首先您需要添加:

http.httpBasic();
我认为您应该编写一个简单的
.hasRole(“ADMIN”)
。 实际上,在使用
authorities()
方法时,需要包含
角色前缀。
当使用
roles()
方法时,不要包含
ROLE\uu
前缀

除了您的代码之外,您还需要一个密码编码器(但可能您已经有了),例如:

@Bean
公共密码编码器PasswordEncoder(){
返回NoOpPasswordEncoder.getInstance();
}

最后,您注意到内存中的用户是
dashboard.xxxx.yyy.zzz
,命令行中的用户是
dashboard.xxx.yyy.zzz
?(缺少一个
X

我将得到一个403禁止的错误。该职位将不会出现。日志中没有任何内容。您未登录。如果没有身份验证,你是匿名的。我以为curl-u参数处理了这个问题。但这肯定能解释问题。您没有配置任何身份验证类型。我想您将使用basic auth,然后必须将
httpBasic()
添加到您的配置中。谢谢。所有这些页面,没有一个提到httpBasic。再加上将密码改为{noop}密码,我已经越过了这个障碍。这个额外的x是一个输入错误,我猜密码前面的{noop}前缀告诉Spring使用密码编码器。最大的赢家是httpBasic。为什么这么多Spring配置网站的页面都没有提到这一点?这看起来很基本。我连续三天把头撞在墙上,