Spring启动完全身份验证问题 - 不确定我哪里出错

时间:2017-04-09 18:19:35

标签: spring-boot spring-security

我无法在使用Spring Boot和maven放在一起的rest API上进行身份验证。我根本无法进行身份验证。

这是我目前的安全配置:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/", "/person/getAll").permitAll()
            .anyRequest().authenticated();
    }

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

这就是" / person / getAll"端点...

@RequestMapping("/getAll")
List<Person> getAllPeople()
{
    log.info("Getting all users in system.");
    return repository.findAll();
}

现在,我所拥有的是一个实际可行的端点 - 但它是未经身份验证的(因为我将它列在安全配置的perminAll部分中) -

但是现在我想要这样做,所以我必须拥有&#34; ROLE_USER&#34;角色 - 为了取回我的人员名单。

我已尝试将其从许可证中删除,并在方法上方添加@PreAuthorise(&#34; hasRole(&#39; ROLE_USER&#39;)&#34;)但这只会导致403响应 - 我根本没想到。我想这将允许我运行以下任一curl命令并查看数据:

curl --user user:password localhost:8080/person/getAll

curl user:password@localhost:8080/person/getAll

对于每个人的理智 - 当我使用当前问题中列出的代码时(我得到数据 - 即使密码错误,哈哈)

长期来看,我的目标实际上是删除内存中的内容并执行常规查询以获取凭据,但我只是想确保我的内容现在正常工作。

1 个答案:

答案 0 :(得分:0)

  

我无法在使用Spring Boot和maven放在一起的rest API上进行身份验证。 我根本无法进行身份验证。

我认为您根本无法进行身份验证,因为您未在Spring Security配置文件中指定在访问页面时需要进行哪种安全保护。

例如:您可以选择具有httpBasic或formLogin安全性。

  

现在,我所拥有的是一个实际可行的端点 - 但未经身份验证(因为我已将其列在安全配置的perminAll部分中) -

例如,如果您要在Spring Security配置文件中添加httpBasic,您将获得http基本身份验证,但是当您未指定任何安全方法时,您将无法获得任何身份验证

anchor
相关问题