【问题标题】:Spring: BasicHttp doesn't work with jdbcAuthentication when hitting requests from PostmanSpring:BasicHttp 在点击来自 Postman 的请求时无法与 jdbcAuthentication 一起使用
【发布时间】:2017-02-27 20:04:47
【问题描述】:

我正在尝试在我的项目中实现jdbcAuthentication。问题是当我从browser 发出请求时它可以工作,但是当我从Postman 发出请求时它不起作用,我的意思是身份验证不会发生,甚至随机凭据也可以工作。

但是,如果我使用 inMemoryAuthentication,它对浏览器和 Postman 都适用。这是我在SecurityConfig 中的代码。

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("select username,password, enabled from users where username=?")
        .authoritiesByUsernameQuery("select username, role from user_roles where username=?");    

   // This works -->    auth.inMemoryAuthentication().withUser("user").password("pass").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic().and().csrf().disable();
    }

在邮递员中,我发出POST 请求。

Authorization 类型中,我选择基本身份验证并输入凭据,然后将其填充到下面的授权标题中

这些是标题

Content-Type:application/json
X-XSRF-TOKEN:{{X-CSRF-TOKEN}}
Authorization:Basic ajhsjajywshhshshsssss

如果我注释 jdbcAUthentication 部分并取消注释 inMomeryAuthentication 代码,则身份验证适用于浏览器和邮递员。有人可以帮我吗,为什么会这样?我错过了什么吗?谢谢!!

【问题讨论】:

  • 请在使用邮递员时提及请求规范(类型、内容等)。

标签: java spring spring-security postman


【解决方案1】:

您正在使用.authenticated(),这意味着用户已经通过身份验证,并且您希望允许已经通过身份验证的用户(也称为“记住”)。

http.authorizeRequests().anyRequest()
    .authenticated().and().httpBasic().and().csrf().disable();

但实际上,您要做的是第一次进行身份验证,因此您需要将其更改为fullyAuthenticated

http.authorizeRequests()
            .anyRequest().fullyAuthenticated()
            .and().httpBasic()
            .and().csrf().disable();

如果您查看 AuthorizedUrl 文档,您会发现:

authenticated():指定任何经过身份验证的用户都允许 URL。

fullyAuthenticated(): 指定已通过身份验证且未被“记住”的用户允许使用 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-19
    • 2020-09-15
    • 2019-07-14
    • 2021-11-29
    • 2017-10-23
    • 2019-01-04
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多